3

I have a huge amount of images and a lot of them have empty icc_profile when I read it using PIL. The way I am checking icc profile is:

from PIL import Image
img = Image.open('image.jpg')
icc = img.info.get('icc_profile', '')

Is there a way to identify colour space of an image (preferably using PIL) even if icc_profile is empty?

Sabih
  • 325
  • 1
  • 3
  • 12

1 Answers1

4

Apart from looking for color space information in ICC profile, you can also look into EXIF metadata tags. Particularly EXIF tag ColorSpace (0xA001) indicates sRGB when it is equal to 1. Other values are not standard, according to this document, but might indicate other color spaces. Another helpful EXIF tag might be InteropIndex (0x0001).

You could check these tags like this:

from PIL import Image
img = Image.open('image.jpg')

def exif_color_space(img):
    exif = img._getexif() or {}
    if exif.get(0xA001) == 1 or exif.get(0x0001) == 'R98':
        print ('This image uses sRGB color space')
    elif exif.get(0xA001) == 2 or exif.get(0x0001) == 'R03':
        print ('This image uses Adobe RGB color space')
    elif exif.get(0xA001) is None and exif.get(0x0001) is None:
        print ('Empty EXIF tags ColorSpace and InteropIndex')
    else:
        print ('This image uses UNKNOWN color space (%s, %s)' % 
               (exif.get(0xA001), exif.get(0x0001)))

Also if your files come from DCIM folder (like in digital cameras or smartphones), Adobe RGB color space can be indicated by name starting from an underscore (like _DSC) or having an extension other than JPG (like JPE).

If the color space of your image is still unknown, the safest is just to assume sRGB. If later a user finds out that the image looks too dull or dim, they can just view the image in other color spaces and it will probably make the image to appear more saturated.

Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49