pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

how to detect colorspace of an image

Open 2h4dl opened this issue 5 years ago • 9 comments

I'm using pyvips.Image.icc_transform to convert an image's color space to sRGB. This API with a tiny, little flaw is also covert a sRGB image to sRGB. I need to detect source image's color space, only a non-sRGB image will be converted.

2h4dl avatar Nov 29 '19 03:11 2h4dl

Hello, I'm not sure I understand.

Could you post a sample image and explain exactly what you need to know?

jcupitt avatar Nov 29 '19 07:11 jcupitt

adobeRGB Image

I need to detect colorspace of this image. If its colorspace is not sRGB, I will convert it to sRGB with pyvips.Image.icc_transform, else do nothing.

2h4dl avatar Nov 29 '19 07:11 2h4dl

Ah OK. Just test to see if there's an ICC profile attached.

x = pyvips.Image.new_from_file(...)
if x.get_typeof("icc-profile-data") != 0:
    x = x.icc_transform("srgb")

Now the image is in srgb colourspace.

You can also do:

x = pyvips.Image.new_from_file(...)
x = x.icc_transform("srgb", embedded=True, input_profile="srgb")

This means: transform to srgb, use the embedded profile if possible as the input space, otherwise default to srgb for the input.

jcupitt avatar Nov 29 '19 08:11 jcupitt

if x.get_typeof("icc-profile-data") != 0 do not say if present icc is already sRGB, how can we check what is indeed the profile embedded?

devlfm avatar Nov 29 '19 17:11 devlfm

You can't, unfortunately.

There's no such thing as an sRGB profile, there are only profiles which (more or less accurately) implement the sRGB standard. You'll find there are dozens of sRGB profiles and they are all slightly different :(

The only thing you can know for sure is that an untagged image is probably sRGB. Unless it's CMYK.

jcupitt avatar Nov 29 '19 17:11 jcupitt

ok! fair enough :)

devlfm avatar Nov 29 '19 18:11 devlfm

I come across almost the same issue,

image.get('icc-profile-data') 

and get

b'\x00\x00\x02$appl\x04\x00\x00\x00mntrRGB XYZ \x07\xe1\x00\x07\x00\x07\x00\r\x00\x16\x00 acspAPPL\x00\x00\x00\x00APPL\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-appl\xca\x1a\x95\x82%\x7f\x10M8\x99\x13\xd5\xd1\xea\x15\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ndesc\x00\x00\x00\xfc\x00\x00\x00ecprt\x00\x00\x01d\x00\x00\x00#wtpt\x00\x00\x01\x88\x00\x00\x00\x14rXYZ\x00\x00\x01\x9c\x00\x00\x00\x14gXYZ\x00\x00\x01\xb0\x00\x00\x00\x14bXYZ\x00\x00\x01\xc4\x00\x00\x00\x14rTRC\x00\x00\x01\xd8\x00\x00\x00 chad\x00\x00\x01\xf8\x00\x00\x00,bTRC\x00\x00\x01\xd8\x00\x00\x00 gTRC\x00\x00\x01\xd8\x00\x00\x00 desc\x00\x00\x00\x00\x00\x00\x00\x0bDisplay P3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00text\x00\x00\x00\x00Copyright Apple Inc., 2017\x00\x00XYZ \x00\x00\x00\x00\x00\x00\xf3Q\x00\x01\x00\x00\x00\x01\x16\xccXYZ \x00\x00\x00\x00\x00\x00\x83\xdf\x00\x00=\xbf\xff\xff\xff\xbbXYZ \x00\x00\x00\x00\x00\x00J\xbf\x00\x00\xb17\x00\x00\n\xb9XYZ \x00\x00\x00\x00\x00\x00(8\x00\x00\x11\x0b\x00\x00\xc8\xb9para\x00\x00\x00\x00\x00\x03\x00\x00\x00\x02ff\x00\x00\xf2\xa7\x00\x00\rY\x00\x00\x13\xd0\x00\x00\n[sf32\x00\x00\x00\x00\x00\x01\x0cB\x00\x00\x05\xde\xff\xff\xf3&\x00\x00\x07\x93\x00\x00\xfd\x90\xff\xff\xfb\xa2\xff\xff\xfd\xa3\x00\x00\x03\xdc\x00\x00\xc0n'
2054197595280

b'\x00\x00\x02$appl\x04\x00\x00\x00mntrRGB XYZ \x07\xe1\x00\x07\x00\x07\x00\r\x00\x16\x00 acspAPPL\x00\x00\x00\x00APPL\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-appl\xca\x1a\x95\x82%\x7f\x10M8\x99\x13\xd5\xd1\xea\x15\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ndesc\x00\x00\x00\xfc\x00\x00\x00ecprt\x00\x00\x01d\x00\x00\x00#wtpt\x00\x00\x01\x88\x00\x00\x00\x14rXYZ\x00\x00\x01\x9c\x00\x00\x00\x14gXYZ\x00\x00\x01\xb0\x00\x00\x00\x14bXYZ\x00\x00\x01\xc4\x00\x00\x00\x14rTRC\x00\x00\x01\xd8\x00\x00\x00 chad\x00\x00\x01\xf8\x00\x00\x00,bTRC\x00\x00\x01\xd8\x00\x00\x00 gTRC\x00\x00\x01\xd8\x00\x00\x00 desc\x00\x00\x00\x00\x00\x00\x00\x0b🔴Display P3🔴\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00text\x00\x00\x00\x00Copyright Apple Inc., 2017\x00\x00XYZ \x00\x00\x00\x00\x00\x00\xf3Q\x00\x01\x00\x00\x00\x01\x16\xccXYZ \x00\x00\x00\x00\x00\x00\x83\xdf\x00\x00=\xbf\xff\xff\xff\xbbXYZ \x00\x00\x00\x00\x00\x00J\xbf\x00\x00\xb17\x00\x00\n\xb9XYZ \x00\x00\x00\x00\x00\x00(8\x00\x00\x11\x0b\x00\x00\xc8\xb9para\x00\x00\x00\x00\x00\x03\x00\x00\x00\x02ff\x00\x00\xf2\xa7\x00\x00\rY\x00\x00\x13\xd0\x00\x00\n[sf32\x00\x00\x00\x00\x00\x01\x0cB\x00\x00\x05\xde\xff\xff\xf3&\x00\x00\x07\x93\x00\x00\xfd\x90\xff\xff\xfb\xa2\xff\xff\xfd\xa3\x00\x00\x03\xdc\x00\x00\xc0n' 2054197595280

I see the embeded icc profile name "Display P3" in the output, can I get that value with pyvips for later use.

To do the same thing with exiftool

exiftool -icc_profile:ProfileDescription some_file.jpg

and get

Profile Description             : Display P3

jindili avatar Jul 06 '21 01:07 jindili

You need a module that can parse ICC profiles and extract the profile name. Just pass it the result of image,get().

jcupitt avatar Jul 06 '21 11:07 jcupitt

Thanks @jcupitt

Just find a module that can extract the profile name

ImageCms Module — Pillow (PIL Fork) 8.3.1 documentation https://pillow.readthedocs.io/en/stable/reference/ImageCms.html#PIL.ImageCms.getProfileDescription

jindili avatar Jul 06 '21 15:07 jindili