One of the tasks I perform today is batch cropping a lot of pictures. I have found ImageMagick very useful to scales and crop the images. Mogrify is a command of the ImageMagick package and allows us to perform many operations on multiple images. I’ll post it here as a future reference for myself and perheps it will help others as well.
Step 1: Install MacPorts:
After the installation, if you got an error:
> _sudo: port: command not found_
Part of the problem is that MacPorts binaries are installed in /opt/local/bin, so you will need to manually adapt your shell’s environment to work with MacPorts:
> _export PATH=$PATH:/opt/local/bin_ > _source .profile_ > _sudo port -v selfupdate_
Step 2: Install ImageMagick
sudo port install ImageMagick
The port command downloads ImageMagick and many of its delegate libraries. If you got an error:
> _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 is not already in your executable search path, add it to your PATH environment variable.
> _export PATH=”$MAGICK_HOME/bin:$PATH”_
Set the DYLDLIBRARYPATH environment variable:
> *export DYLD*LIBRARY*PATH=”$MAGICK_HOME/lib/*
Step 3: Add the missing decoding library
When trying to convert jpeg images, it came up with this error message:
> _“convert: no decode delegate for this image format “_
- Go to: http://www.imagemagick.org/download/delegates/ and download the required/missing delegate library. For example jpegsr9a.zip.
- Unzip the file
- Change directory to the folder
> _cd jpeg-9a_
- And run
> _./configure make make test make -n install_
Step 5: Usage
To avoid overrides the original image files, create a new folder, copy the images and backup in that folder.
For resize a single image to height of 600px with the same aspect ratio, you could run this command:
> _convert input.png -geometry x600 output.png_
If you prefer to covert all images of a folder, change directory to the folder and use
> _mogrify -geometry x600 _.png\*
To scale down an image to 200 pixels:
> _convert myPhoto.jpg -resize 200x200^_
To crop the image from center:
> _convert myPhoto.jpg -gravity Center -crop 200x200+0+0 +repage newPhoto.jpg_
The -gravity south option states that I want the crop to start at the bottom of my image. The -chop 0x25 states that I want to cut 25 pixels from the height.
> _mogrify -gravity south -chop 0x135 _.jpg\*
To resize all images in current directory to 800 width (and height reduced proportionally):
> _mogrify -resize 800 _.jpg\*
To rotate the images 90 degress:
> _mogrify -rotate 90 _.
For more information:
Experience in software development, consulting services, and technical product management. Understanding of business and technology with an MBA in Finance and a Master's degree in Computer Science. Certified in AWS, GCP and Kubernetes with experience in building banking products from scratch and serving as a charismatic leader.