Image Manipulations With Imagemagick
We’ve mentioned it before when we talked about progressive jpegs. Knowing some basic commands can save you time and allow you to stay in the terminal.
To install on Linux:
sudo apt-get install imagemagickTo install on Mac:
brew update && brew install imagemagickThere are two commands that come out of the box when you install ImageMagick, convert mogrify and they both generally take the same arguments. Convert transforms the image and creates a new one and mogrify replaces the image in place. With these examples we’ll show them being used with both commands, but you can use them interchangeably.
To resize an image
convert image.jpg -resize 1080x1080 updated-file.jpgIt might be useful to add in the -gravity center flag as well with the above command.
mogrify -resize 50% *.jpgTrim whitespace from an image
mogrify -trim *.{jpg,png}Note that ImageMagick will maintain aspect ratio so to enforce the resolution strictly you can add in a exclamation symbol:
convert image.jpg -resize 1080x1080! updated-file.jpgTo reduce the quality and the image size you can pass in the quality flag:
mogrify image.jpg -quality 75Convert a PNG to a JPG:
convert myimage.png myimage.jpgCreate a collage of images by using the montage command:
montage image1.jpg image2.jpg image3.jpg image4.jpg output-montage.jpg -geometry +2+2Change colors in an image:
convert white.png -fill '#000' -opaque '#fff' black.pngCreate a favicon
convert source.png -define icon:auto-resize favicon.icoWe used this on Medium and this guide on Hackernoon to compile these commands. Read more about ImageMagick in the docs.