logo
menu

How to Compress an Image in MATLAB

By Janet | July 29, 2026

If you want to compress an image in MATLAB, the most practical method is to read the image, optionally resize it, then save it with a compression setting such as JPEG quality. MATLAB is useful when you need repeatable code, batch processing, or image-processing experiments.

How to Compress an Image in MATLAB

Quick Answer

Use imread to load the image and imwrite to save a compressed copy. For JPEG output, MATLAB’s Quality parameter uses a value from 0 to 100. Lower values create stronger compression and smaller files, while higher values preserve more visual detail.

img = imread("input.jpg");
imwrite(img, "compressed.jpg", "jpg", "Quality", 70);

If you only need a quick browser workflow and do not want to write code, use Lynote’s image compressor instead.

Method 1: Compress a JPEG with imwrite Quality

JPEG is the easiest format for file-size reduction because it supports lossy compression.

img = imread("photo.jpg");
imwrite(img, "photo-compressed.jpg", "jpg", "Quality", 65);

Try a quality value between 60 and 80 first. Then inspect the output. Very low values may create visible artifacts around text, faces, edges, or high-contrast areas.

Method 2: Resize Before Saving

If the image dimensions are much larger than needed, resizing can reduce file size more cleanly than lowering JPEG quality too far.

img = imread("photo.jpg");
smaller = imresize(img, 0.5);
imwrite(smaller, "photo-small.jpg", "jpg", "Quality", 75);

This keeps the image readable while reducing the number of pixels MATLAB needs to store.

Method 3: Check File Size After Compression

Do not guess whether the image is small enough. Check the output file size.

info = dir("photo-compressed.jpg");
sizeKB = info.bytes / 1024;
fprintf("Compressed size: %.1f KB\n", sizeKB);

If the file is still too large, reduce the quality value a little or resize the image slightly more.

Method 4: Batch Compress Images in a Folder

For datasets or assignment folders, loop through images and save compressed copies.

files = dir("*.jpg");

for k = 1:length(files)
    img = imread(files(k).name);
    outputName = "compressed_" + files(k).name;
    imwrite(img, outputName, "jpg", "Quality", 70);
end

Before running a batch job on important files, test the settings on one image and keep your originals.

JPEG Quality Settings Explained

The Quality value controls the tradeoff between file size and visual fidelity:

Quality valueTypical result
90-100Large file, high visual quality
70-85Balanced compression for photos
50-70Smaller file with possible artifacts
Below 50Strong compression, often visibly degraded

For documents, screenshots, diagrams, and images with small text, avoid compressing too aggressively.

MATLAB Compression Tips

  • Save a copy instead of overwriting the original.
  • Use JPEG for photos and PNG for screenshots or graphics when quality matters.
  • Resize oversized images before lowering quality too much.
  • Compare the output visually, especially around text and edges.
  • Use a loop only after testing one image.

FAQ

How do I compress an image in MATLAB?

Load the image with imread, then save it with imwrite. For JPEG, use the Quality parameter to control compression.

What does JPEG Quality mean in MATLAB?

It is a 0-100 setting. Lower values usually create smaller files with lower visual quality. Higher values preserve more detail but produce larger files.

Can MATLAB compress PNG images?

Yes, but PNG compression behaves differently from JPEG. For photos, converting to JPEG often reduces file size more. For screenshots or diagrams, PNG may preserve sharper edges.

How can I compress many images at once?

Use dir to list files, loop through them, and save compressed copies with imwrite.

Final Check

Good image compression is not just a smaller number. The file should still be readable, usable, and appropriate for the upload or analysis task.