Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to detect colorspace of an image #140

Open
2h4dl opened this issue Nov 29, 2019 · 9 comments
Open

how to detect colorspace of an image #140

2h4dl opened this issue Nov 29, 2019 · 9 comments
Labels

Comments

@2h4dl
Copy link

2h4dl commented Nov 29, 2019

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.

@jcupitt
Copy link
Member

jcupitt commented Nov 29, 2019

Hello, I'm not sure I understand.

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

@2h4dl
Copy link
Author

2h4dl commented Nov 29, 2019

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.

@jcupitt
Copy link
Member

jcupitt commented Nov 29, 2019

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.

@devlfm
Copy link

devlfm commented Nov 29, 2019

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?

@jcupitt
Copy link
Member

jcupitt commented Nov 29, 2019

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.

@devlfm
Copy link

devlfm commented Nov 29, 2019

ok! fair enough :)

@jindili
Copy link

jindili commented Jul 6, 2021

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

@jcupitt
Copy link
Member

jcupitt commented Jul 6, 2021

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

@jindili
Copy link

jindili commented Jul 6, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants