How?: colorspace/gamma aware resize for Grayscale Images

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
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

How?: colorspace/gamma aware resize for Grayscale Images

Post by henrywho »

I am playing with the large ring image http://www.imagemagick.org/Usage/resize ... g_orig.png again.... this time with version 6.7.6-3-Q16-windows http://www.imagemagick.org/download/bin ... indows.zip

[1] convert.exe rings_lg_orig.png -distort resize 150x150! 01.png
[2] convert.exe rings_lg_orig.png -depth 16 -distort resize 150x150! -depth 8 02.png
[3] convert.exe rings_lg_orig.png -depth 16 -colorspace RGB -distort resize 150x150! -colorspace sRGB -depth 8 03.png
[4] convert.exe rings_lg_orig.png rings_lg_orig.png rings_lg_orig.png -combine -depth 16 -colorspace RGB -distort resize 150x150! -colorspace sRGB -depth 8 04.png
[5] convert.exe rings_lg_orig.png -depth 16 -gamma 0.454545 -distort resize 150x150! -gamma 2.2 -depth 8 05.png

=> [1] and [2] are classical non-gamma-aware resize.
=> [3] is simply wrong. It seems "-colorspace RGB" is not for grayscale.
=> [4] tries to combine 3 images into one RGB image, but the output is too bright.
=> [5] gives the best output.

Nevertheless, if I convert rings_lg_orig.png to RGB 24bit file in XnView, saving it as, say, "rings_lg_orig_as_24bit.png".

[6] convert.exe rings_lg_orig_as_24bit.png -depth 16 -colorspace RGB -distort resize 150x150! -colorspace sRGB -depth 8 06.png

=> [6] gives the intended output too!

Can I achieve the same thing with the grayscale "rings_lg_orig.png" using "-colorspace"?

----------------------------

BTW, 06.png generated in [6] is saved as a grayscale 8bit PNG but not a RGB 24bit PNG. Actually, a trivial "convert.exe rings_lg_orig_as_24bit.png directsave.png" also gives a grayscale 8bit PNG. Is this behavior by-design?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by fmw42 »

try adding -type truecolor. I think that gives what you want.

convert rings_lg_orig.png -type truecolor -depth 16 -colorspace RGB -distort resize 150x150! -colorspace sRGB -depth 8 06b.png
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by henrywho »

fmw42 wrote:convert rings_lg_orig.png -type truecolor -depth 16 -colorspace RGB -distort resize 150x150! -colorspace sRGB -depth 8 06b.png
No, the result is too bright. It gives the same result as combining three PNG.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by anthony »

The -depth option does nothing for the above examples. it is only used when reading and saving images, not for in-memory quaility.

See IM Examples, Basics, Depth and Quality
http://www.imagemagick.org/Usage/basics/#image_quality

Also remember your version of IM must be at least v6.7.5-1 and more preferably the very latest, to use -colorspace for corrections. The original poster however does appear to have the latest.

IMv6 does not make a distinction between Grayscale and 3-channel RGB (of whatever type). As such case '3' while 'seems' wrong is actually correct!

IMv7 on the otherhand will actually allow the use of one channel grayscale images in memory, though I have not looked to see if it makes a distinction between images in linear-GRAY colorspace and some type of 'sGRAY' colorspace. You do on the other hand have to use a -colorspace (or other) operations to convert the image from one channel to three channels.


As for case '4' (combining 3 grayscale to create a RGB -- not that it is needed) It may be that -combine is not setting the colorspace correctly for the colorspace conversions to use. You may have found a bug in the new colorspace handling. I myself have reported a number of bugs in colorspace handling (cycling images to some other colorspace and back is not comming out as No-Ops). In that situation I get the same 'too bright' result.

Finally case '5' is pretty will independant on the whole colorspace change. It just maps the image values regardless of what colorspace those values are supposedally in.

However in future versions of IM (I mean IMv7) the -draw and other commands will also map 'named' colors (which are defined in sRGB colorspace) to match the colorspace of the image they are being applied to. As such it makes sense to ensure an images colorspace (or at least its recorded gamma setting) is set correctly.

See Resizing with Colorspace and Gamma Corrections
http://www.imagemagick.org/Usage/resize ... colorspace
As well as the introduction to all this... Human Color Perception
http://www.imagemagick.org/Usage/color_ ... perception
Problems with Draw and sRGB colorspace
http://www.imagemagick.org/Usage/draw/#colorspace
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by henrywho »

[3] gives me a color image. That's why I said it is totally incorrect.

Together with a trivial "convert.exe rings_lg_orig_as_24bit.png directsave.png" giving a 8bit grayscale PNG (rings_lg_orig_as_24bit.png is the 8bit PNG converted to a gray-only 24bit RGB PNG), it appears a bug to me.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by anthony »

henrywho wrote:[3] gives me a color image. That's why I said it is totally incorrect.

Together with a trivial "convert.exe rings_lg_orig_as_24bit.png directsave.png" giving a 8bit grayscale PNG (rings_lg_orig_as_24bit.png is the 8bit PNG converted to a gray-only 24bit RGB PNG), it appears a bug to me.
Yes I see it now. The image has some green center rings and is no longer grayscale. That is a very very bad bug!
I don't even understand how this could happen as the sRGB convertsion mapping is suposed to be applied to all channels equally.

This is a minimal set of operations to see the problem....

Code: Select all

   wget http://www.imagemagick.org/Usage/img_photos/rings_lg_orig.png
   convert im/img_photos/rings_lg_orig.png -colorspace RGB test_RGB.png
Yes it is darker as it is now linear-RGB, but the problme is it is no longer grayscale.

On the other hand, assuming the image is linear-RGB and converting to sRGB does not have this problem.

Code: Select all

    convert im/img_photos/rings_lg_orig.png -set colorspace RGB -colorspace sRGB test_sRGB.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by henrywho »

anthony wrote:On the other hand, assuming the image is linear-RGB and converting to sRGB does not have this problem.

Code: Select all

    convert im/img_photos/rings_lg_orig.png -set colorspace RGB -colorspace sRGB test_sRGB.png
Giving a gray image, but the result is too bright.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by magick »

We can reproduce the problem you posted. Look for a patch in the ImageMagick 6.7.6-4 release within a few days.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How?: colorspace/gamma aware resize for Grayscale Images

Post by anthony »

henrywho wrote:
anthony wrote:On the other hand, assuming the image is linear-RGB and converting to sRGB does not have this problem.

Code: Select all

    convert im/img_photos/rings_lg_orig.png -set colorspace RGB -colorspace sRGB test_sRGB.png
Giving a gray image, but the result is too bright.
That image is in the WRONG colorspace, actually it is not even correct colorspace for its data! What I was looking at in that example, was that the image was producing grayscale. that ment the colors being seen was not comming from a RGB->sRGB conversion, but only in the sRGB->RGB conversion.

I applogies for not making that clear.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply