change jpg quality only if lower than existing

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

change jpg quality only if lower than existing

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: change jpg quality only if lower than existing

Post 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.
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: change jpg quality only if lower than existing

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: change jpg quality only if lower than existing

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply