Page 1 of 1

change jpg quality only if lower than existing

Posted: 2011-02-23T13:03:23-07:00
by HugoRune
Hi,

Is there a way to tell mogrify to change the "-quality" compression level only if the image does not already have a lower quality setting?

i.e.
> mogrify -quality 50% *.jpg
should leave files that are already compressed at quality 20% untouched.

Or in other words, if the file size would increase, do not change the file.

Re: change jpg quality only if lower than existing

Posted: 2011-02-23T13:11:05-07:00
by magick
See http://www.imagemagick.org/script/formats.php. Look for the JPEG format:
  • Finally, to restrict the maximum file size, use jpeg:extent, for example -define jpeg:extent=400kb.

Re: change jpg quality only if lower than existing

Posted: 2011-02-23T13:27:23-07:00
by HugoRune
Thanks!
Can I somehow apply that to all files in a directory with mogrify *.jpg?
something like mogrify -quality 50% -define jpeg:extent $EXISTING_SIZE *.jpg

Ideally I would want to leave files with lower compression level totally untouched, not recompressing them to stay at the same size. I suppose the only way to do that is to first get the current quality of each file with identify and then parse that somehow?

Re: change jpg quality only if lower than existing

Posted: 2011-02-23T19:27:03-07:00
by anthony
The problem is quality is dependant on indivitual JPG library implementations.
It is also not actually stored with the JPG image, though a 'educated guess' can be made on it.