Batch Crop Images using ImageMagick
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:
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:
Step 2: Install ImageMagick
http://www.imagemagick.org/script/binary-releases.php
To install, run:
The port
command will download ImageMagick and many of its delegate libraries. If you encounter an error like:
Set the MAGICK_HOME
environment variable to the path where you extracted the ImageMagick files:
If the bin
subdirectory of the extracted package isn't already in your executable search path, add it:
Set the DYLD_LIBRARY_PATH
environment variable:
Step 3: Add Missing Decoding Library
If you try to convert JPEG images and get the following error message:
- Visit http://www.imagemagick.org/download/delegates/ and download the required or missing delegate library, such as
jpegsr9a.zip
. - Unzip the file.
- Change directory to the unzipped folder:
- Then run:
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:
If you'd like to convert all images in a folder, change to that directory and use:
To scale down an image to 200 pixels:
To crop the image from the center:
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:
To resize all images in the current directory to a width of 800 (height will be reduced proportionally):
To rotate images 90 degrees:
For More Information:
Visit http://www.imagemagick.org.