« Back to Index

Terminal Resize Images with ImageMagick

View original Gist on GitHub

Terminal Resize Images with ImageMagick.md

Installation

brew install imagemagick

Commands

The ImageMagick commands that modify images are mogrify and convert.

By default, the mogrify command overwrites the existing image with the modified image unless you specify an output folder into which modified image will be saved.

The convert command saves a new modified image and leaves the original unchanged so it is safer to use.

Add Border

mogrify -path Newimage/ -border 1x1 -bordercolor "#000000" image.png

or

convert -border 1x1 -bordercolor "#000000" image.png Newimage/new-image.png

Resize

To reduce an image’s width to 790 pixels while maintaining the aspect ratio, and to avoid increasing the size of the image if it is already smaller than 790 pixels wide, use either of the commands:

mogrify -path Newimage/ -resize "790>" image.png

or

convert -resize "790>" image.png Newimage/image.png

Change File Type

convert image.png Newimage/image.jpg

Quality

The default JPEG qualilty is 92% but you may set it to any value from 0% to 100%.

For example, to convert all the PNG images in a directory to JPEG with 70% quality, enter the command:

convert -quality 70 image.png Newimage/image.jpg

Combining Transformations

convert -resize "790>" -border 1x1 -bordercolor "#000000" -quality 70 image.png converted.jpg

You can also use wildcards to batch process (but not change format or rename):

mogrify -path Newimage/ -resize "790>" -border 1x1 -bordercolor "#000000" *.*

If you want to change the format or rename a file in a batch, then you need the convert command, but that doesn’t handle wildcards so you need to do something like:

mkdir Newimage
for i in *.png; do convert "$i" "Newimage/${i%.*}.jpg"; done

To then combine those transformations:

for i in *.png; do convert -resize "790>" -border 1x1 -bordercolor "#000000" -quality 70 "$i" "Newimage/${i%.*}.jpg"; done