How to batch resize and compress images in linux

Published by shyam jos on

In this tutorial I will show you how to batch resize and compress images in ubuntu or in any other linux distros(debian,arch and fedora).

‘Convert’ command is a very powerful image manipulation utility which comes preinstalled in almost all linux distributions (ubuntu,debian,arch and fedora) and it is a part of ImageMagick software suite.

Check if convert command is available on your linux distro
 convert -version 

output:

Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC

If above command is not found on your linux distro you can install it by typing below command.

Install convert tool on ubuntu / Debian
 sudo apt-get install imagemagick 
Install convert tool on fedora
 sudo dnf install imagemagick 

 

Install convert tool on arch
 sudo pacman -S imagemagick 

Note: It’s always good idea take backup of your images before  running convert command.

Batch resize and compress images with convert command

Below command will resize and compress all images ending with .jpg extension in a ‘for loop’ and save the compressed/resized image as ‘filename-Optimized.jpg’.

-quality :  this option is used to set the image compression level in percentage

-resize : this option is used to resize the image to a given resolution , You can use  -resize option with width ( -resize 1600x ) only or both width and height ( -resize 1600×900), In both cases convert command will automatically adjust the given resolution to get optimum aspect ratio  so you don’t have to worry about stretched images.

Note: some jpeg image extensions may end with .JPG (in capital letters) ,Since linux is case sensitive you may need to adjust the bash script accordingly.

open terminal and cd into the directory containing your photos and execute below command

 mkdir photos-Optimized;for photos in *.jpg;do convert -verbose $photos -quality 85% -resize 1600x900 ./photos-Optimized/$photos-Optimized.jpg ; done 

Voila ! Now you have successfully batch resized/compress images, You can see the optimized images in current directory with the file extension ‘filename-Optimized.jpg’.


shyam jos

My name is Shyam Jos, I am a passionate Linux system administrator from Kochi, India. I am a strong advocate of using Linux and Open-Source software.

5 Comments

Marcin Migacz · September 27, 2017 at 4:24 PM

Hi great command line !
I wisch only to improve it a little bit, and for the sake of usability put all optimized jpg files in to new folder optimized. How it will look like a new command that way ?

Great Thanks !

    shyam jos · September 27, 2017 at 9:10 PM

    Hi Marcin,
    Good question , you can do something like this mkdir photos-Optimized;for photos in *.jpg;do convert -verbose $photos -quality 85% -resize 1600x900 ./photos-Optimized/$photos-Optimized.jpg ; done , I have also updated the post. Thank You!

James T · June 19, 2018 at 12:20 PM

Doesn’t work.

    shyam jos · June 19, 2018 at 8:41 PM

    Can you share the error message you are getting?

      Warren · July 10, 2018 at 9:01 PM

      I faced the same issue. It’s the shift from IM6 to 7. Since you have installed 7, you need to replace the convert with magick. One more thing, the input is case sensitive. So if you have JPG files (instead of jpg) you will have to capitalize as needed. Hope this helps

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.