Batch Crop Images using ImageMagick

July 18, 2015

Today, one of the tasks I performed involved batch cropping numerous pictures. I found ImageMagick to be very useful for scaling and cropping images. Mogrify, a command within the ImageMagick package, enables us to perform various operations on multiple images. I am posting this guide as a future reference for myself and perhaps it will be helpful for others as well.

Step 1: Install MacPorts

https://www.macports.org/install.php

After completing the installation, if you encounter the following error:

> _sudo: port: command not found_

The issue likely arises because MacPorts binaries are installed in /opt/local/bin. You'll need to manually update your shell’s environment to work with MacPorts:

> _export PATH=$PATH:/opt/local/bin_
> _source .profile_
> _sudo port -v selfupdate_

Step 2: Install ImageMagick

http://www.imagemagick.org/script/binary-releases.php

To install, run:

> _sudo port install ImageMagick_

The port command will download ImageMagick and many of its delegate libraries. If you encounter an error like:

> _convert: command not found_

Set the MAGICK_HOME environment variable to the path where you extracted the ImageMagick files:

> _export MAGICK_HOME="$HOME/ImageMagick-6.9.1"_

If the bin subdirectory of the extracted package isn't already in your executable search path, add it:

> _export PATH="$MAGICK_HOME/bin:$PATH"_

Set the DYLD_LIBRARY_PATH environment variable:

> _export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"_

Step 3: Add Missing Decoding Library

If you try to convert JPEG images and get the following error message:

> _“convert: no decode delegate for this image format”_
  1. Visit http://www.imagemagick.org/download/delegates/ and download the required or missing delegate library, such as jpegsr9a.zip.
  2. Unzip the file.
  3. Change directory to the unzipped folder:
> _cd jpeg-9a_
  1. Then run:
> _./configure; make; make test; make -n install_

Step 5: Usage

To avoid overwriting the original image files, create a new folder and backup the images there.

To resize a single image to a height of 600px while maintaining the same aspect ratio, run:

> _convert input.png -geometry x600 output.png_

If you'd like to convert all images in a folder, change to that directory and use:

> _mogrify -geometry x600 *.png_

To scale down an image to 200 pixels:

> _convert myPhoto.jpg -resize 200x200^_

To crop the image from the center:

> _convert myPhoto.jpg -gravity Center -crop 200x200+0+0 +repage newPhoto.jpg_

The -gravity south option specifies that the crop should start at the bottom of the image. The -chop 0x135 option cuts 135 pixels from the height:

> _mogrify -gravity south -chop 0x135 *.jpg_

To resize all images in the current directory to a width of 800 (height will be reduced proportionally):

> _mogrify -resize 800 *.jpg_

To rotate images 90 degrees:

> _mogrify -rotate 90 *.jpg_

For More Information:

Visit http://www.imagemagick.org.


Profile picture

Software development professional with expertise in application architecture, cloud solutions deployment, and financial products development. Possess a Master's degree in Computer Science and an MBA in Finance. Highly skilled in AWS (Certified Solutions Architect, Developer and SysOps Administrator), GCP (Professional Cloud Architect), Microsoft Azure, Kubernetes(CKA, CKAD, CKS, KCNA), and Scrum(PSM, PSPO) methodologies. Happy to connect