How to read image colorspace?
Posted: 2009-10-12T03:24:36-07:00
I have a dilemma reading colorspace from images. I you use the standard way identify -format "%[colorspace]%" you will sometimes get wrong result.
For example i you have a RGB image with a RGB profile sRGB you might sometime get colorspace: GRAY instead of RGB if the internal image consists of only grayscale pixels. I think this have to do ImageMagick being "smart" and comparing each pixel channels and returning gray if each channel is the same. But this breaks the standard, a grayscale image can newer have RGB profile. I you do some convert command on this image and then save it you will end up with incorrect profile data. You can solve this by using -type truecolor and force to save to RGB.
So my question is how do I read this image as a RGB colorspace?
I have come up with a solution that works 95% of the times
identify -format "%r"
This will result in one of following values 'DirectClassRGB', 'PseudoClassRGB', 'PseudoClassGray', 'DirectClassGray'
if image class is DirectClassRGB or PseudoClassRGB the colorspace is always RGB.
if image class is PseudoClassGray the colorspace is always GRAY
if image class is DirectClassGray the colorspace is most_of_the_times RGB but now always
Do anyone have a solution that works 100% of the times?
For example i you have a RGB image with a RGB profile sRGB you might sometime get colorspace: GRAY instead of RGB if the internal image consists of only grayscale pixels. I think this have to do ImageMagick being "smart" and comparing each pixel channels and returning gray if each channel is the same. But this breaks the standard, a grayscale image can newer have RGB profile. I you do some convert command on this image and then save it you will end up with incorrect profile data. You can solve this by using -type truecolor and force to save to RGB.
So my question is how do I read this image as a RGB colorspace?
I have come up with a solution that works 95% of the times
identify -format "%r"
This will result in one of following values 'DirectClassRGB', 'PseudoClassRGB', 'PseudoClassGray', 'DirectClassGray'
if image class is DirectClassRGB or PseudoClassRGB the colorspace is always RGB.
if image class is PseudoClassGray the colorspace is always GRAY
if image class is DirectClassGray the colorspace is most_of_the_times RGB but now always
Do anyone have a solution that works 100% of the times?