Convert JPG from AdobeRGB to sRGB using PIL?

后端 未结 3 1575
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 19:15

How can I detect if the JPG is AdobeRGB and if it is convert it in python to sRGB JPG.

If that is possible in PIL, that would be great. Thank you.

3条回答
  •  半阙折子戏
    2021-02-13 20:03

    I had the same problem, I tested all answers and get wrong color in final image. @DrMeers All matrixes I've tried gave wrong result in red and blacks, so here is my solution:

    The only way I found out is to read profile from 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 to any color profile, but not tested.

提交回复
热议问题