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

3

2

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

Justin

Posted 2014-07-31T21:53:42.470

Reputation: 31

Answers

6

The naming is a slightly different format, but:

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

technosaurus

Posted 2014-07-31T21:53:42.470

Reputation: 996

ah~ this trick avoids modifying the end of the file name. – David S. – 2014-07-31T22:14:43.500

4

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 *.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.

seumasmac

Posted 2014-07-31T21:53:42.470

Reputation: 336

-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 :)

David S.

Posted 2014-07-31T21:53:42.470

Reputation: 606

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. – David S. – 2014-08-03T04:54:14.423

-1

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

phaberest

Posted 2014-07-31T21:53:42.470

Reputation: 101