8

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

0

4 Answers 4

10

The naming is a slightly different format, but:

for img in *.jpg; do
  convert -resize 20% "$img" "opt-$img"
done
1
  • ah~ this trick avoids modifying the end of the file name.
    – David S.
    Commented Jul 31, 2014 at 22:14
8

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.

3
  • find | xargs, a neat combo! That would be find _dir_to_look_in -name *.jpg, however.
    – xtofl
    Commented Aug 25, 2021 at 10:10
  • 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
    Commented Aug 27, 2021 at 7:30
  • 1
    @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!
    – seumasmac
    Commented Aug 27, 2021 at 7:44
0

Try with for i in $( ls *.jpg); do convert -resize 20% $i optimized_$i; done (You can set the path to the folder other than just *.jpg)

As a GUI you could use Converseen

1
-1

I think a simple bash script can help you.

for i in `ls /path/to/img/dir`; do convert -resize 20% "/path/to/img/dir/${i:0:-4}" "/path/to/new/img/dir/${i:0:-4}_optimized.jpg"; done

Note: ${i:0:-4} is to remove the .jpg extension. If your file have different extension length, you may want to change -4 to something else :)

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .