Prevent imagemagick from mogrifing an image twice

398 views Asked by At

So i have a folder that has a lot of images. I use imagemagick to optimize all the images inside the folder. I aborted the command to "optimize" the images. Now. Some images have been optimized but not all. So I must run the command again. Is there a way that imagemagick skip the images that have already been optimized?

This is what i usually do to optimze all the images:

for i in *.jpg; do mogrify -quality 85 "$i"; done
1

There are 1 answers

0
Mark Setchell On

If you have tens of thousands of images to process, and a decent fairly modern machine (i.e. multi-core CPU and decent RAM and disks), you could consider GNU Parallel.

Basically, you need to pipe the filenames into GNU Parallel if there are more than your system's ARGMAX limit. It is best to use null-terminated names so that filenames including spaces continue to work, so you'd want:

find . -name \*.jpg -print0 | parallel -0 --dry-run mogrify -quality 85 {}

If that looks correct, you can remove the --dry-run and run it again for real. As it is, that will keep 4 copies of mogrify running in parallel if your CPU has 4 cores - i.e. it will keep them all busy and should process your images miles faster, However, it is not most efficient because it starts a whole new mogrify process for every image, it is better to add -X to GNU Parallel and then it will start mogrify with as many filenames as your OS can handle and the cost of starting the one process will be amortised over a large number of images. That would be:

find . -name \*.jpg -print0 | parallel -0 -X mogrify -quality 85 {}

Make your experiences with any new software on a copy of your data - GNU Parallel can do lots of stuff very fast - good stuff and bad!