Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

How to get ColorSpace attribute from Exif

Open julin69 opened this issue 3 years ago • 7 comments

Hello, I want to get colorSpace from image exif,my code is below:

from PIL import Image
current_image = Image.open("1.jpg")
exifs = current_image.info.get("exif") # bytes data

but the exifs is the bytes data, i don't know how to decode it, can you help me? Thanks very much!

julin69 avatar Aug 05 '22 07:08 julin69

Instead of using the "exif" part of the info dictionary, you can use getexif(). Within that data, you will want to get the EXIF IFD, pointed to by the EXIF IFD tag, and then get the ColorSpace tag.

from PIL import Image
current_image = Image.open("rgb.jpg")
exif = current_image.getexif().get_ifd(34665)
print(exif[40961])

https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif/colorspace.html

Normally sRGB (=1) is used to define the color space based on the PC monitor conditions and environment. If a color space other than sRGB is used, Uncalibrated (=65535) is set. Image data recorded as Uncalibrated can be treated as sRGB when it is converted to Flashpix.

radarhere avatar Aug 05 '22 08:08 radarhere

What is 34665?

julin69 avatar Aug 05 '22 08:08 julin69

The EXIF IFD tag. See the "Code" value at https://www.awaresystems.be/imaging/tiff/tifftags/exififd.html

Within the getexif(), there are a few sections, IFDs. There is EXIF, GPS and Interoperability. We have limited support for reading Makernote data.

radarhere avatar Aug 05 '22 09:08 radarhere

If a image without icc profile, how to embed icc file to the image?

srgb_profile = ImageCms.createProfile("sRGB")
new_image = ImageCms.profileToProfile(current_image, None, srgb_profile, 0, "RGB") # this line error

I want to use the new_image to crop and resize action, finally to save as jpg file with srgb icc file.

Now i have a question, first to transform profile then do crop and resize action,then save as jpg file with icc profile or first do crop and resize action, no matter the profile, finally save as icc profile image?

Thank you for your reply.

julin69 avatar Aug 05 '22 09:08 julin69

Your question is no longer about EXIF data. It would be good to either create a new issue, or continue this in #6467.

It shouldn't matter at what point in the process you crop, as that doesn't affect pixel values.

radarhere avatar Aug 05 '22 10:08 radarhere

Regarding an image without "icc_profile" in the info dictionary, https://stackoverflow.com/questions/50641637/identify-colour-space-of-any-image-if-icc-profile-is-empty-pil appears helpful.

You've also asked whether it is better to apply an ICC profile or to resize the image first. I would think it is better to resize first, as the resize operation may change the colors in a way that reduces the effectiveness of the ICC profile.

radarhere avatar Aug 06 '22 08:08 radarhere

@julin69 has that answered your questions?

radarhere avatar Aug 07 '22 23:08 radarhere

Closing this issue as no feedback has been received.

github-actions[bot] avatar Aug 16 '22 00:08 github-actions[bot]