If you’re not sure what ImageMagick is, it’s one of the greatest tools you could have on your computer, to manipulate images and a few other types of files. In this post, I’m going to list out a few of the commands which come in very handy in a variety of situations.
Recently I got the opportunity to work on a project where a lot of images had to be manipulated — changing the resolution and keeping the file size in check. I wrote the script in PHP and ImageMagick was the weapon of choice. Before you read any further, if you are not aware of ImageMagick and the features it offers, go through their website first.
Also, I’d like to point out that this is more for personal reference, so that after a few months when I forget the commands or need to manipulate more images, I can come back here and get all the commands I need.
One more thing, I work with Ubuntu — my personal laptop runs Ubuntu, my work laptop runs Ubuntu, my personal AWS EC2 instance runs Ubuntu, and almost all of the cloud computers I work on run Ubuntu. So the commands given below are all tested on Ubuntu (16.04 to be precise).
Let’s get started then, shall we?
Change image resolution
This is I think one of the most basic image manipulation we’d do. And therefore, the command is also pretty simple to understand and remember, even for a non-techie.
Consider we have an image called image.jpg which is around 2816×2112 pixels, that’s like a 6MP image. If you want to reduce that to say around 1024×768 pixels, you just do it like this:
convert image.jpg -resize 1024x768 output_file.jpg
And that’s it. You’ll have a new image called output_file.jpg whose resolution is somewhere close to 1024×768 pixels. I say close to that resolution because by default, ImageMagick tries to maintain the aspect ration. So if the resolution that you specify doesn’t maintain the original aspect ratio of the image, ImageMagick will use a resolution which is close to what you specified while maintaining the aspect ratio.
But this default behaviour is easy to override. You can use the ‘!’ symbol with the resolution to tell ImageMagick that the resolution has to be maintained strictly. So the command would now look like this:
convert image.jpg -resize 1024x768! output_file.jpg
But this might not always work the way you expect. Because *nix systems have a special meaning for the symbol !. To make sure nothing funny happens and this works as expected, you need to “escape” the ! in the command. You can do that by adding a ‘\’ in front of the ! like this:
convert image.jpg -resize 1024x768\! output_file.jpg
That’s it. If your input file image.jpg is not corrupt, you’ll now have a new image with the resolution that you specified. It’s that simple.
Reducing an image’s file size, or reducing the quality of an image
If you a developer who builds web app with a lot of images, you know the importance of file size. You can’t have a 5MB image on your webpage, which will take ages to download. This will for a horrible user experience. The solution is to keep the file size as small as possible. When it comes to images, “compressing” an image is the most common technique to achieve a smaller file size. This can be done in two way — reduce the image resolution (which we already discussed), and reduce the “quality” of the image.
Since we already discussed how to reduce the resolution of an image, let’s see how you can reduce the quality of an image. ImageMagick provides an easy option with it’s convert command to set the quality of an image. You can do it like this:
convert image.jpg -quality 75 output_file.jpg
This command will reduce a 3MB file to less than 300KB. The quality option 75 provided in the command could be thought of as reducing the quality of the image to 75%, or by 25%.
For JPG (JPEG) images, the quality ranges from 1 to 100. 1 will ensure highest compression but lowest image quality, and 100 will ensure highest image quality but doesn’t provide great compression. You can determine the quality of the input file, if not, the default value is 92.
Experts also says that 75 is the best trade-off between image quality and compression, and in most cases, it gets the work done. For other image types (such as PNG), there are a lot of other options to use with the -quality option. For more on that, take a look at the official documentation, and you’ll be amazed at the possibilities.
Create a collage of images (montage)
Another use case is creating collages of images. We see this kind of images a lot on social networks these days. If you are the kind of person who makes collages, you’ll love this command. No more uploading images to websites and waiting for them to convert and then downloading them again. You don’t even have to give your images to anybody else now.
Say you have four images and you want to create a 2×2 collage of those four images. You use the following command:
montage image1.jpg image2.jpg image3.jpg image4.jpg output_montage.jpg
This will create a new image called output_montage.jpg, but it might not look how you expected it to look — it’ll have four very small images arranged in a 2×2 matrix. The images will be resized because of the default options that the montage command uses, which is “-geometry 120×120>+4+3.” Now what does that mean? It means, no matter what the input image resolution is, resize it to a 120×120 pixel image and use that to generate the collage. To override this behaviour, you can just provide the -geometry option yourself with the command.
There’s also a -tile command, using which you can specify how many tiles you want. But if you aren’t specific about this and just want to have a collage of your images, you can let ImageMagick figure out the best possible tile arrangement.
So, to get the best possible collage of your four images, use this command with the -geometry option:
montage image1.jpg image2.jpg image3.jpg image4.jpg -geometry +2+2 output_montage.jpg
I’m sure you’ll like the output. Let me know. 🙂
Convert an image to a PDF
This is another most common task that we come across frequently. And I must say, conversion of images to PDFs is one of the most simplest commands ImageMagick provides. Let’s suppose you want to convert that awesome 2×2 collage you created earlier to a PDF. How do you do it? Like this:
convert output_montage.jpg output_montage.pdf
Wasn’t that easy? Go ahead, try it.
Bonus: Merging PDF files
You might think that you could easily merge several PDF files into one using the convert command like this:
convert pdf1.pdf pdf2.pdf output_pdf.pdf
You actually can. But don’t! This is because if your input files are 10MB each, your output file will be close to 1GB big. This is very, very bad. So to merge several PDF files, I’ll give you another command using another tool called GhostScript. Most *nix systems come pre-installed with GhostScript. If you don’t have it already installed, head over to their website to get install instructions.
Now, you can easily merge PDF files into one file using the command:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=temp.pdf pdf1.pdf pdf2.pdf
Don’t bother what each option means, unless you’re a techie and interested to know how it works. The website has all the information you’d need.
Most programming languages come with ImageMagick support, either built in or with a few third party packages. But you can be sure to make use of these commands in your project the next time you have to work with images.
ImageMagick has capabilities with work with blobs as well. So you can just get the base64 string of an image and work with that. It’s really very versatile and handy.
I hope this was of some use. Let me know in the comments if I made any mistake here, or if you have any more cool commands.