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 imagemagick
To install on Mac:
brew update && brew install imagemagick
There 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.jpg
It might be useful to add in the -gravity center
flag as well with the above command.
mogrify -resize 50% *.jpg
Trim 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.jpg
To reduce the quality and the image size you can pass in the quality
flag:
mogrify image.jpg -quality 75
Convert a PNG to a JPG:
convert myimage.png myimage.jpg
Create a collage of images by using the montage command:
montage image1.jpg image2.jpg image3.jpg image4.jpg output-montage.jpg -geometry +2+2
Change colors in an image:
convert white.png -fill '#000' -opaque '#fff' black.png
Create a favicon
convert source.png -define icon:auto-resize favicon.ico
We used this on Medium and this guide on Hackernoon to compile these commands. Read more about ImageMagick in the docs.