Bulk reduce the size of images with ImageMagick

May 22, 2023 Author: markovk

With the rapid growth of the resource, it can be difficult to keep track of the correct processing of images: images are loaded randomly, which negatively affects page loading speed and takes up extra space on the server. You can fix this situation using the ImageMagick utility. Its toolkit is very extensive, but we will focus on the most important points. It will not be superfluous to also recall that before starting processing, just in case, it is worth making a backup of the images.

  1. Install the package on the production server: sudo apt install ImageMagick (Use yum instead of apt in CentOS)
  2. To make sure that the installation was successful, we execute the command identify -version and look at the version of ImageMagick

For the most effective tool for reducing the weight of images, let’s take their quality reduction-quality, set the quality to 50% of the original. It should be borne in mind that ImageMagick is not sharpened for processing png images, so let’s tell it that it needs to work only with jpg images: -type f ( -name “*.jpg” -o -name “*.jpeg” ).

  1. We move to the directory with images and get ready to start processing, the full command will look like this: find . -type f ( -name “*.jpg” -o -name “*.jpeg” ) -execdir mogrify -quality 50 {} +
  2. After the command completes, we check the images. The program had to process, including files in nested directories

Please note: when copying the above commands, make sure that the quotes are simple, not curly.

We have considered a special case of working with quality reduction, but there are a lot of tools: the -resize command is used for resizing, -crop is for cropping, -format is for changing the format, etc. A complete list of all ImageMagick features can be found in the official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Begin work on your project today
Contact us on Telegram. Ask a question and get a quick response.
or
Message
in Telegram
By clicking the button, you give your consent to the processing of personal data and agree to the privacy policy.