How to batch compress images with Imagemagick or similar command or program in Linux

8,009

Solution 1

The naming is a slightly different format, but:

for img in *.jpg; do
  convert -resize 20% "$img" "opt-$img"
done

Solution 2

Using a 'for' loop will definitely work - and is a good general technique - but you almost certainly have more than 1 processor on your machine, so why do just one conversion at a time?

You can get things moving a lot quicker if you do:

find . -name '*.jpg' | xargs -n1 -P8 -I{} convert -resize 20% "{}" "opt-{}"

The arguments to xargs are:

n1  - Only give 'convert' one file at a time
P8  - Use 8 processes
I{} - Replace {} with the filename given by 'find'

And then the convert command is given afterwards.

Share:
8,009
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I recently learned about the convert command from imagemagick which I have used to compress many pictures that I have for personal use, and for a blog that I have. In my experience, Imagemagick is the best image compressor program, and it gives one the most control over how to compress images. I have many more pictures that I want to compress, but don't want to have to use the convert command so many times, one by one for every file. The main problem is that it is very time consuming for me to do it. I would like to be able to one big batch way to compress my images. I am OK with doing this via command line, but a GUI might make this a little bit more intuitive.

    Here is a sample of a command that I use to make the original image 20% of the original size. convert -resize 20% 20140322_102113.jpg 20140322_102113opt.jpg

    If I say have 100 images, and they are all in the same folder, I want to be able to do something like the following

    For all images convert -resize 20% imagename.jpg imagename_optimized.jpg

    I don't know if there is a command that can already do this, but if not, I thought about creating a bash command, but I am not so familiar with bash. Help creating this simple bash script, or advice on how to solve my dilemma is appreciated. I use Linux, and would only like a solution specifically for Linux. Thanks

  • David S.
    David S. almost 10 years
    ah~ this trick avoids modifying the end of the file name.
  • tink
    tink almost 10 years
  • tink
    tink almost 10 years
  • David S.
    David S. almost 10 years
    for day to day one line scripts, normal people would check what the loop outputs before modify the data. i don't see the point to check that post.
  • xtofl
    xtofl almost 3 years
    find | xargs, a neat combo! That would be find _dir_to_look_in -name *.jpg, however.
  • seumasmac
    seumasmac almost 3 years
    Hi @xtofl you can indeed provide find with a dir to look in, or even a list of dirs! But you can also give it a list of filenames to search. Or any mixture of the two! So, the above works if you're in the directory with the jpg files, because the shell expands *.jpg to a list of the filenames, and then find just lists them in its usual order. Try it and see!
  • seumasmac
    seumasmac almost 3 years
    @xtofl actually, on further reflection, while the above works, it would break with too many filenames, so I'll edit my reply to use the more standard approach. Thanks for the tip!