Convert JPG from AdobeRGB to sRGB using PIL?

How can I determine if a JPG is AdobeRGB, and if it converts it in python to sRGB JPG.

If possible in PIL, that would be great. Thanks.

+8
python image-processing jpeg python-imaging-library color-profile
source share
3 answers

Thanks for the martineau specification, I put some working PIL code along with functions that detect the presence of an Adobe RGB ICC profile in Image and convert the color space to sRGB.

 adobe_to_xyz = ( 0.57667, 0.18556, 0.18823, 0, 0.29734, 0.62736, 0.07529, 0, 0.02703, 0.07069, 0.99134, 0, ) # http://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf xyz_to_srgb = ( 3.2406, -1.5372, -0.4986, 0, -0.9689, 1.8758, 0.0415, 0, 0.0557, -0.2040, 1.0570, 0, ) # http://en.wikipedia.org/wiki/SRGB def adobe_to_srgb(image): return image.convert('RGB', adobe_to_xyz).convert('RGB', xyz_to_srgb) def is_adobe_rgb(image): return 'Adobe RGB' in image.info.get('icc_profile', '') # alternative solution if happy to retain profile dependency: # http://stackoverflow.com/a/14537273/284164 # icc_profile = image.info.get("icc_profile") # image.save(destination, "JPEG", icc_profile=icc_profile) 

(I used them to create Django easy-thumbnails ):

 def preserve_adobe_rgb(image, **kwargs): if is_adobe_rgb(image): return adobe_to_srgb(image) return image 
+7
source share

I had the same problem, I tested all the answers and got the wrong color in the final image. @DrMeers All the matrices I tried gave the wrong result in red and black, so here is my solution:

The only thing I learned was to read the profile from the image and convert using ImageCms.

 from PIL import Image from PIL import ImageCms import tempfile def is_adobe_rgb(img): return 'Adobe RGB' in img.info.get('icc_profile', '') def adobe_to_srgb(img): icc = tempfile.mkstemp(suffix='.icc')[1] with open(icc, 'w') as f: f.write(img.info.get('icc_profile')) srgb = ImageCms.createProfile('sRGB') img = ImageCms.profileToProfile(img, icc, srgb) return img img = Image.open('testimage.jpg') if is_adobe_rgb(img): img = adobe_to_srgb(img) # then do all u want with image. crop, rotate, save etc. 

I think this method can be used for any color profile, but not tested.

+5
source share

To program this yourself, you can convert pixels in the AdobeRGB color space to CIE XYZ, and then convert it to sRGB. PIL image objects have a method called convert() with the ability to apply general matrix transformation to all image pixels (see the Convert section in the online documentation of the PIL Image module - pay attention to an example showing the matrix values โ€‹โ€‹needed to switch from RGB to XYZ) .

Section 4.3.4 in AdobeRGB1998.pdf spec shows the matrix for converting XYZ to RGB.

I am not sure how to define the color space of JPG images. I remember reading something about the fact that ICC xml profiles are attached to the end of the file (and the problem occurs when there were several), but I cannot vouch for its authenticity. A Wikipedia article on the JPEG format says nested profiles.

+4
source share

All Articles