JPEG how to make -quality <%> so that it's bound to its max

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
feelart
Posts: 16
Joined: 2013-07-09T08:08:24-07:00
Authentication code: 6789

JPEG how to make -quality <%> so that it's bound to its max

Post by feelart »

Hi,

how to make -quality <%> so that it's bound to the current photo max.
This info is in

Code: Select all

identify -verbose 
I mean, take a low quality jpeg and convert -quality 96 for instance, than the output jpeg is way bigger.
It's obviously not what you want, you want the max quality regarding the transformation you're applying does not to exceed 96, but if it's says 80, so must it be 80 and not 96.
A bit like -resize 1980x1980 which won't scale up an image which width or height < 1980.

Thanks
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: JPEG how to make -quality <%> so that it's bound to its

Post by GreenKoopa »

I understand what you are saying and that makes sense. I don't know if IM can do this (without a simple outside script). Are you otherwise modifying the images, or are you simply reading them in and writing them back out but possibly smaller?
feelart
Posts: 16
Joined: 2013-07-09T08:08:24-07:00
Authentication code: 6789

Re: JPEG how to make -quality <%> so that it's bound to its

Post by feelart »

>I understand what you are saying and that makes sense
Thanks :)

>Are you otherwise modifying the images, or are you simply reading them in and writing them back out but possibly smaller?

It should be a general behaviour, but you're right it most happens when the source is a non "raw" photo jpeg (I mean not direct photo shot, but as already been compressed).

It often happens in batch processing a directory and typically

1/ white-balance correct
2/ increase luminisance by a small factor 15%
3/ -depth 16
4/ strip exif and -resize 1024x1024.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: JPEG how to make -quality <%> so that it's bound to its

Post by GreenKoopa »

If it helps, I believe that an unspecified quality defaults to the original quality. What you ask may be possible somehow with the new support for escapes in settings.

After all those changes, especially the resize, increasing the quality above the initial one will likely add visual quality.

What does the -depth 16 do?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: JPEG how to make -quality <%> so that it's bound to its

Post by fmw42 »

If a jpg image has a quality setting, it can be extracted by using

convert image -format "%Q" info:

see http://www.imagemagick.org/script/escape.php

A value of 0 means the default, which is either the input quality or 92 if no quality is set.

see http://www.imagemagick.org/script/comma ... hp#quality

The result of the command can be put into a variable and tested against your limit and the result can be used for each image.

Example (unix syntax) -- assumes quality is an integer and not float value.

# create jpg with 80 quality
convert rose: -quality 80 rose_q80.jpg

# get quality and put into variable
qual=`convert rose_q80.jpg -format "%Q" info:`
echo $qual
80

# test if 0 or if greater than 96 and process image
if [ "$qual" -eq "0" ]; then
convert rose_q80.jpg -quality 92 rose_q96.jpg
elif [ $qual -gt 96 ]; then
convert rose_q80.jpg -quality 96 rose_q96.jpg
fi

The above will only set the quality to 96 if it is bigger than 96. Otherwise, it leaves the image unchanged or applies the default 92 to any jpg with no quality setting. You can set the default value to anything you want, when a zero value is returned.
Post Reply