In this
‘Convert’ command is a very powerful image manipulation utility which comes preinstalled in almost all
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
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
Batch resize and compress images with convert command
Below command will resize and compress all images ending with
–
-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
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/compressed images, You can see the optimized images in the photos-Optimized directory.
10 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
Ansar · October 3, 2018 at 10:39 AM
Thanks for this great post.
Correction: You forget to update to look into NEW directory in your post :”You can see the optimized images in current directory”
Error: I have one small problem that the files with spaces is not selecting well with the command. Any workaround?
Suppose I have a file named Electrical Inspectorate1.jpg, when covert command the error will be like this :
convert: unable to open image `Electrical’: No such file or directory @ error/blob.c/OpenBlob/2761.
convert: no decode delegate for this image format `’ @ error/constitute.c/ReadImage/504.
convert: unable to open image `Inspectorate1.jpg’: No such file or directory @ error/blob.c/OpenBlob/2761.
shyam jos · October 3, 2018 at 12:34 PM
Hi Ansar,
Thanks for the suggestions, I have fixed the script to handle filenames with spaces (added double quotes for variables), Also updated the post with output directory name.
sandeep g · September 28, 2019 at 10:41 AM
Hi, How can i reduce Batch images file size at a time. total images folder size is 3.4GB and 6.5GB
linus · January 19, 2020 at 11:53 AM
Hello how do you include subfolders etc?
Robert L. Nelson · April 19, 2020 at 5:20 AM
I have a group of panorama photos with different ratios of width to height. I would like to use your with the following change. I put a wild card in place of the width (mkdir photos-Optimized;for photos in *.JPG;do convert -verbose “$photos” -quality 85% -resize 1600x* ./photos-Optimized/”$photos-Optimized.jpg”; done. I basically need them all to be the same width and maintain their ratio. Other than that I love your script. I have used it to batch resize about 20000 pics and it worked super.