Different quality setting for different areas of an image

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
runeks
Posts: 5
Joined: 2013-10-09T18:51:49-07:00
Authentication code: 6789

Different quality setting for different areas of an image

Post by runeks »

I'd like to compress an image heavily, but use a higher quality setting for specific areas of an image, is this possible with ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Different quality setting for different areas of an imag

Post by fmw42 »

You can compress the image multiple times with different settings. Then use masks to composite all the images together.

Or compress the image the least amount and then use -region or -mask to compress further in the desired areas.

see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/script/comma ... php#region
http://www.imagemagick.org/script/comma ... s.php#mask
http://www.imagemagick.org/script/comma ... #clip-mask

If you can clarify further the regions you want to use, then perhaps a better answer with example could be provided.
runeks
Posts: 5
Joined: 2013-10-09T18:51:49-07:00
Authentication code: 6789

Re: Different quality setting for different areas of an imag

Post by runeks »

fmw42 wrote:You can compress the image multiple times with different settings. Then use masks to composite all the images together.

Or compress the image the least amount and then use -region or -mask to compress further in the desired areas.

see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/script/comma ... php#region
http://www.imagemagick.org/script/comma ... s.php#mask
http://www.imagemagick.org/script/comma ... #clip-mask

If you can clarify further the regions you want to use, then perhaps a better answer with example could be provided.
Thanks! The region I want to be compressed less is the lower right corner: http://needsmorejpeg.com/i/mry

I was just checking out this site needsmorejpeg.com and the problem is that the watermark is also compressed heavily (since that's the purpose of the site; "more jpeg"), but it kind of makes the watermark useless, since you can't read it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Different quality setting for different areas of an imag

Post by fmw42 »

Can you provide a link to your orginal input image and the exact region you want to compress less.
runeks
Posts: 5
Joined: 2013-10-09T18:51:49-07:00
Authentication code: 6789

Re: Different quality setting for different areas of an imag

Post by runeks »

fmw42 wrote:Can you provide a link to your orginal input image and the exact region you want to compress less.
Sure. This is the image I want to compress a lot, except for the region described below: http://i.imgur.com/Zb6r15W.png

And the area I want to compress less is defined by the following rectangle in the above picture (x1,y1,x2,y2): 386,359,493,372

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Different quality setting for different areas of an imag

Post by fmw42 »

try this (unix syntax)


convert yourimage \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" -write mpr:image +delete \) \
-mask mpr:image -quality 10 result.jpg

The second line creates a white image with your area marked in black as a mask and saves it to mpr: (in memory format) and deletes the mask from the image chain leaving the copy in memory.

The third line applies the in-memory mask using -mask.

You can change the -quality setting to whatever you want. I use 10 to emphasize the compression for easy visualizing.

In window, remove the the \ from the \( and \) and replace the end of line \ with end of line ^


see http://www.imagemagick.org/Usage/masking/#write_mask


NOTE: there appears to be some "bleeding" of the higher compression into your region. I believe this is because JPG compression works in blocks of (16?). So you would likely get better results by enlarging your region to multiples of the block size and aligning it on block boundaries. However, if you just make the region large enough to cover full blocks plus an extra block on all sizes as a buffer around the text, it should work without all that effort.


ALTERNATE:

convert yourimage \( -clone 0 -quality 10 \) \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" \) \
-compose over -composite result.jpg


This clones the image and processes it with -quality 10. Then it creates the same mask (but does not write to mpr:). Then it uses the mask to composite the two images together.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Different quality setting for different areas of an imag

Post by snibgo »

I thought that "-quality" applied only to the saving on an image to a file, and could only apply to the entire image. I didn't think the OP's requirement could be met with IM.

Taking fmw42's alternative command on runeks's image, inserting a "write", Windows syntax:

Code: Select all

%IM%convert ^
  Zb6r15W.png ^
  ( -clone 0 -quality 10 -write r.jpg ) ^
  ( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" ) ^
  -compose over -composite ^
  result.jpg
If we could have different compressions across the image, r.jpg and result.jpg would be different. But they are identical, and this remains true for a variety of compression numbers.
snibgo's IM pages: im.snibgo.com
runeks
Posts: 5
Joined: 2013-10-09T18:51:49-07:00
Authentication code: 6789

Re: Different quality setting for different areas of an imag

Post by runeks »

The results I get agree with snibgo.

Using this bash script should take the aforementioned example image and compress the right half of the image less than the left half. This doesn't happen, the whole image is compressed with quality=1, seemingly:

Code: Select all

IMG="/home/rune/Desktop/cat-needsmorejpeg.png"
RECT="250,0,500,375"

convert "$IMG" \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle $RECT" -write mpr:image +delete \) \
-mask mpr:image -quality 1 result1.jpg

convert "$IMG" \( -clone 0 -quality 1 \) \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle $RECT" \) \
-compose over -composite result2.jpg
On further though; is this even possible?

As far as I can see, I need to specify two different quantization matrices in the JPEG file, and use a low quality one for the blocks I want compressed the most, and a high quality one for the blocks I want compressed not as much. Is this even allowed for the JPEG format?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Different quality setting for different areas of an imag

Post by fmw42 »

snibgo is correct. So it looks like the only way to do that is with 3 steps, creating a tmp disk low quality jpg, compositing and deleting the tmp jpg afterwards


convert Zb6r15W.png -quality 10 1tmp.jpg

convert Zb6r15W.png 1tmp.jpg \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" \) \
-compose over -composite result3.jpg

rm 1tmp.jpg


Or in two commands as

convert -respect-parenthesis Zb6r15W.png \( -clone 0 -quality 10 +write 1tmp.jpg \) +delete 1tmp.jpg \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" \) \
-compose over -composite result4.jpg

rm 1tmp.jpg


The -respect-parenthesis is to avoid the -quality 10 being used on the original.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Different quality setting for different areas of an imag

Post by snibgo »

Or in a single command, eg:

Code: Select all

convert ^
  Zb6r15W.png ^
  ( -clone 0 -quality 2 -write r.jpg +delete ) ^
  ephemeral:r.jpg ^
  ( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" -write rm.png ) ^
  -compose over -composite ^
  -quality 80 ^
  result2.jpg
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Different quality setting for different areas of an imag

Post by fmw42 »

I was just trying that, even though as I recall Anthony does not like that use of ephemeral, it does work.

You do not need -write rm.png unless you want to save the mask image for review.

My unix command equivalent to before would be


convert -respect-parenthesis yourimage \( -clone 0 -quality 10 +write 1tmp.jpg \) +delete ephemeral:1tmp.jpg \
\( -clone 0 -fill white -colorize 100 -fill black -draw "rectangle 386,359 493,372" \) \
-compose over -composite result4.jpg

You can remove the -respect-parenthesis if you reset a new -quality XX before the output (as in snibgo's command)
runeks
Posts: 5
Joined: 2013-10-09T18:51:49-07:00
Authentication code: 6789

Re: Different quality setting for different areas of an imag

Post by runeks »

Hooray! That totally works. Thanks guys!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Different quality setting for different areas of an imag

Post by snibgo »

[I included "-write rm.png" so help me see what the desired effect should be. I didn't mean to include that in the suggested solution. Sorry about that.]
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Different quality setting for different areas of an imag

Post by GreenKoopa »

The jpegtran utility supports "crop 'n' drop". I'm curious if you could achieve smaller files or faster processing.
jpegclub.org/jpegtran/ wrote: Here is a sample output image and an equivalent shell script producing the same image using one temporary file.
Post Reply