

For other image types (such as PNG), there are a lot of other options to use with the -quality option. You can determine the quality of the input file, if not, the default value is 92.Įxperts also says that 75 is the best trade-off between image quality and compression, and in most cases, it gets the work done. 1 will ensure highest compression but lowest image quality, and 100 will ensure highest image quality but doesn’t provide great compression. The quality option 75 provided in the command could be thought of as reducing the quality of the image to 75%, or by 25%.įor JPG (JPEG) images, the quality ranges from 1 to 100. This command will reduce a 3MB file to less than 300KB. You can do it like this: convert image.jpg -quality 75 output_file.jpg ImageMagick provides an easy option with it’s convert command to set the quality of an 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. This can be done in two way - reduce the image resolution (which we already discussed), and reduce the “quality” of the image. When it comes to images, “compressing” an image is the most common technique to achieve a smaller file size. The solution is to keep the file size as small as possible. This will for a horrible user experience. You can’t have a 5MB image on your webpage, which will take ages to download. If you a developer who builds web app with a lot of images, you know the importance of file size. Reducing an image’s file size, or reducing the quality of an image If your input file image.jpg is not corrupt, you’ll now have a new image with the resolution that you specified. You can do that by adding a ‘\’ in front of the ! like this: convert image.jpg -resize 1024x768 \! output_file.jpg To make sure nothing funny happens and this works as expected, you need to “escape” the ! in the command. Because *nix systems have a special meaning for the symbol !. So the command would now look like this: convert image.jpg -resize 1024x768 ! output_file.jpgīut this might not always work the way you expect. You can use the ‘ !’ symbol with the resolution to tell ImageMagick that the resolution has to be maintained strictly.

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.īut this default behaviour is easy to override. I say close to that resolution because by default, ImageMagick tries to maintain the aspect ration. You’ll have a new image called output_file.jpg whose resolution is somewhere close to 1024x768 pixels. If you want to reduce that to say around 1024x768 pixels, you just do it like this: convert image.jpg -resize 1024x768 output_file.jpgĪnd that’s it. And therefore, the command is also pretty simple to understand and remember, even for a non-techie.Ĭonsider we have an image called image.jpg which is around 2816x2112 pixels, that’s like a 6MP image. This is I think one of the most basic image manipulation we’d do. So the commands given below are all tested on Ubuntu (16.04 to be precise). 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. Before you read any further, if you are not aware of ImageMagick and the features it offers, go through their website first.Īlso, 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. I wrote the script in PHP and ImageMagick was the weapon of choice. 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. In this post, I’m going to list out a few of the commands which come in very handy in a variety of situations. 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.
