3

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

5

The naming is a slightly different format, but:

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

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.

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

-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.