Posted by CANbike on Thu, 21 Feb 2013

Images: Batch Resize by Largest Dimension and Set Compression Quality

After a bike ride, I often have a bunch of photos to post on web. To save on file sizes and upload times, it’s nice to resize the images and increase the compression level. Of course, it’s even nicer to batch process everything.


ImageMagick is a free command-line image manipulation program that is very useful for scripting. In this case, scripts are unnecessary, as ImagaeMagick provides a batch command processor know as mogrify. However, mogrify will overwrite the original file by default, unless instructed otherwise. Best practice is to make a copy of the images to be processed.

Batch Resize Images

To batch resize images in a folder, use the mogrify command with the resize switch. The switch requires a geometry specification. I normally use the “scale%”.

mogrify -resize 33% *.jpg

The above command will resize all jpg files in the current directory by 33 percent.

Batch Resize Images and Set Compression Quality

The resized image works well but the file size may be too large for web use. Increasing the compression level would help. That’s were the quality switch is used.

mogrify -resize 33% -quality 75 *.jpg

The above command will resize all jpg files in the current directory by 33 percent and set the compression quality to 75 percent.

Batch Resize by Largest Dimension

Resizing by percentage works well, but sometimes it’s easier or necessary to specify an exact dimension. Unfortunately some of the photos are in portrait mode or landscape mode. For this situation, I want the resized images to retain their original proportions and have the largest side resized to a specified length. Moreover, if this is a set from a photo shoot (same camera), the resized images should have the same number of pixels. i.e. the resized photos are either 600×800 or 800×600.

According to ImageMagick,

The geometry argument “widthxheight>” will proportionally shrink an image dimension(s) larger than the corresponding width and/or height argument(s).

Therefore, if the desired largest dimension is 900px, then the following batch command can be used.

mogrify -resize "900x900>" *.jpg

The original proportions of the images will be retained. Largest side will be 900px.

Final Example: Batch Resize Largest Side Proportionally and Set Compression

mogrify -resize "900x900>" -quality 75 *.jpg

For all jpg files in the same folder, this batch command proportionally reduces the largest side to 900px, and sets the compression quality to 75 percent.

Related Item(s):