16bit grayscale TIFF is too dark

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?".
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

16bit grayscale TIFF is too dark

Post by theiviaxx »

So i have some images that are saved as RGB16 but actually have no color and should be saved as 16bit grayscale images. However when using imagemagick, it results ina darker grayscale image. If i leave colorspace alone, then it looks right, but is still RGB

Ive tried the following

Code: Select all

convert image.tif -colorspace sRGB -modulate 100,0 _image.tif
convert _image.tif -channel R -separate _image.tif

Code: Select all

convert image.tif -colorspace sRGB -modulate 100,0 -colorspace Gray _image.tif

Code: Select all

convert image.tif -colorspace Gray _image.tif
All resulting in the same thing. If i look at the desaturated rgb image in photoshop, each channel is correct so I'm not sure how IM is extracting the channel and it being darker. Am i missing something in this process?

Thanks
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 16bit grayscale TIFF is too dark

Post by anthony »

What version of IM? How are you viewing the images? Can you give and example image?

Lots of changes were made in the last few months to do with colorspace handlin in IM, specifically correctly setting sRGB colorspace on most images. The exception to this is greyscale images.

Note that -seperate will generate 'gray' colorspace images, as that is what channel extracted images are.

A pure grayscale image in IM (colorspace "gray") is saved as linear grayscale, rather than sRGB (to avoid colorspace conversion losses). Many viewers automatically convert image colorspace to sRGB for display. Web browsers like Firefox does, which typically results in an image becoming brighter. Note that the IM "display" comamnd does not do this conversion (by default, as users can request the conversion), and shows the data AS IS.

Photoshop probably also shows and edits the data AS IS (so it looks darker). You do not want to convert values unless you really need to, especially in 8 bit resolution (photoshop) IM typically is either 16 bit integer values, or in some cases HDRI (floating point).

This is an area that is still being discussed and looked at within ImageMagick. Remember most grayscale images are mathematical linear value images (alpha masks, lighting effects, LUTs, Fourier Transform Spectrum, channel separations, etc), but grayscale photos should be saved using a non-linear sRGB colorspace, and not a linear colorspace.

Part of this is part of the preparations for IMv7 which will handle one channel images in memory with auto-promoting them to 3 color channels. It can also handle multi-spectral images containing dozens of channels, though there are few image file formats that can save such images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

try either

convert image.tif -colorspace sRGB -set colorspace RGB -colorspace Gray resultimage.tif

convert image.tif -colorspace sRGB -modulate 100,0,100 resultimage.tif

-colorspace gray produces a linear gray. add -set colorspace RGB before it to force it to create a non-linear gray as it used to on older than IM 6.7.5.5 (possibly before 6.7.6.7).

-modulate does not convert to linear gray, so -set colorspace RGB is not needed

see
viewtopic.php?f=4&t=21269
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 16bit grayscale TIFF is too dark

Post by anthony »

Actually modulate converts the image to one of the special Hue colorspaces before applying its actions, then converts back again. As such it essentially ignores the current colorspace (except to what to restore the image to after it has done its work).

See Modulate Examples
http://www.imagemagick.org/Usage/color_mods/#modulate

You may also like to look at the new HCL Hue type colorspace (based on YUV colorspace), which preserves the color intensity better when doing Hue rotations.
http://www.imagemagick.org/Usage/color_ ... dulate_HCL
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

im on the latest. My image to test is a simple gradient 512x512. In PS it's 3channel, but has been completely desaturated. Theoretically i just need to extract the r channel as they all should be the same, right? But in PS the resulting image from -separate looks darker than the R channel in PS. Is this just PS doing something to it?

convert image.tif -colorspace sRGB -set colorspace RGB -colorspace Gray resultimage.tif

this gets the closest. I still see some variation, but its not nearly as bad

convert image.tif -colorspace sRGB -modulate 100,0,100 resultimage.tif

matches perfect, but still a 3 channel image. If i add -colorspace Gray it gets a single channel but is way dark as before.


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

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

Then try

convert image.tif -colorspace sRGB -modulate 100,0,100 -set colorspace RGB -colorspace gray resultimage.tif

None of us can say anything for sure about the coding of PS?

I believe that PS uses the desaturate method (-modulate) to get grayscale, but it is not clear which colorspace they use (HSL, HSB, HCL).

I do know that PS may not use the same mixing of channels to get grayscale from their channel mixer. IM grayscale is linear and needs -colorspace RGB to force it to be non-linear the way it used to work and the way I presume PS works. The IM channel mixing to get gray is defined by standards and is specified at http://www.imagemagick.org/script/comma ... colorspace as

Gray = 0.298839*R+0.586811*G+0.114350*B

I think PS uses a default of .3R + .6G + .1B in their channel mixer.
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

turns out a little brighter. For this particular image if i use -modulate 83,0 it gets super close, but there is still a discrepancy.

I have no idea how PS achieves this. So i may just end up using PS to do the work.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

-modulate 83,0 will make it darker not lighter. Not sure which way you want to go now. Before you said it was too dark.

You can try changing the colorspace for -modulate by adding -define

convert image.tif -colorspace sRGB -define modulate:colorspace=HSL -modulate 100,0,100 -set colorspace RGB -colorspace gray resultimage.tif
convert image.tif -colorspace sRGB -define modulate:colorspace=HSB -modulate 100,0,100 -set colorspace RGB -colorspace gray resultimage.tif
convert image.tif -colorspace sRGB -define modulate:colorspace=HCL -modulate 100,0,100 -set colorspace RGB -colorspace gray resultimage.tif

The IM default is HSL, but I think someone mentioned that PS uses HCL. Let us know if that works. You may also try without the -colorspace gray.


convert image.tif -colorspace sRGB -define modulate:colorspace=HSL -modulate 100,0,100 resultimage.tif
convert image.tif -colorspace sRGB -define modulate:colorspace=HSB -modulate 100,0,100 resultimage.tif
convert image.tif -colorspace sRGB -define modulate:colorspace=HCL -modulate 100,0,100 resultimage.tif
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

convert.exe color1.tif -colorspace sRGB -define modulate:colorspace=HCL -modulate 100,0,100 -set colorspace RGB __color.tif
convert.exe color1.tif -colorspace sRGB -define modulate:colorspace=HCL -modulate 100,0,100 -set colorspace RGB -colorspace Gray __color.tif

These result in the same image that is slightly lighter than what it should be.

Changing the colorspace for modulate didn't change anything and sometimes PS would complain about an ICC profile
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

I took a simple color image and desaturated in PS and then on IM using

convert sailboat1.jpg -modulate 100,0,100 sailboat1_desat_hsl.jpg

which used HSL colorspace. Visually they look the same. So I did a comparison

compare -metric rmse sailboat1_desat_ps.jpg sailboat1_desat_hsl.jpg null:
912.831 (0.0139289)

which show a 1.39% rmse difference.
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

was the resulting image grayscale or rgb?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

theiviaxx wrote:was the resulting image grayscale or rgb?
It was gray, but I did not look at the colorspace. But if you add -type grayscale after -modulate, the result should be a gray image with colorspace gray
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

Ok so i did

convert.exe color1.tif -colorspace sRGB -modulate 100,0,100 -set colorspace RGB -colorspace Gray __color.tif

And in PS the result looks slightly lighter than the target. So i used compare as you did and i get:

0.645167 (9.84462e-006)

So does that mean PS is just displaying it wierd and the actual images are relatively the same?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 16bit grayscale TIFF is too dark

Post by fmw42 »

You have negligible difference in data values.

Yes, the two viewers are displaying differently. IM displays as the colorspace dictates, in this case linear. But most other viewers display as non-linear (sRGB). I would suggest you view both images in some browser. But don't expect all browsers to view alike, either. Just display in the same viewer all the time.

or try

convert.exe color1.tif -colorspace sRGB -modulate 100,0,100 -set colorspace RGB -colorspace -set colorspace sRGB Gray __color.tif

See if that makes a difference.


But I am puzzled why you need all the -colorspace sRGB at the beginning, if your tif is already sRGB?

It is kind of hard to know what to tell you unless you provide your input image, your output image and your PS image and what PS function(s) you used to create it. Then other can see and test with your image.
theiviaxx
Posts: 11
Joined: 2012-09-11T15:26:23-07:00
Authentication code: 67789

Re: 16bit grayscale TIFF is too dark

Post by theiviaxx »

https://docs.google.com/folder/d/0B2MGM ... p0Snc/edit

Here are two images:
* color1.tif is the source, it's desaturated but has 3 channels
* _color1.tif is the result from PS. Simply changing the mode to Grayscale. In photoshop they look the same

So I tried running compare against these two and there is a difference. ~2% in my tests. If i view them with picasa or windows, they do look different. the grayscale looks darker. In PS they look the same, so PS is doing some magic there.

I guess my final question is why is it so difficult to grab a single channel from a tiff without loss?

If you switch to grayscale, then each channel gats multiplied by the values listed above. But if all channels are the same, then you will get a different image, right?
Post Reply