skip to Main Content

While saving jpg thumbnails from tiff (CMYK) file I’m encountering the following problem:

  1. Thumbnail created with color mode conversion from CMYK to RGB gets blue hue:

enter image description here

  1. Thumbnail created from the same tiff in photoshop (no ICC profile, not converted to sRGB, RGB only) get the colors the right way:

enter image description here

  1. Thumbnails created without color mode conversion (jpeg with CMYK) gets similar results to the photoshopped ones but are not usable for the web (still blue).

Code snippet:

img = Image.open(img_tiff)
img.thumbnail(size)
img.convert("RGB").save(img_jpg, quality=70, optimize=True)

Also, when trying to include tiff’s ICC profile it gets corrupted in some way, and photoshop is complaining about the profile being corrupted. I was including the profile in the save function using:

icc_profile=img.info.get('icc_profile')  

What am I doing wrong / missing here?

EDIT:

Searching for the solution I found that the problem was linked to the icc profiles. Tiff file has FOGRA profile, jpeg should have some sRGB. Couldn’t get to work profile transformation from within Pillow (ImageCms.profileToProfile() was throwing PyCMSError cannot build transform ,'ImageCmsProfile' object is not subscriptable, 'PIL._imagingcms.CmsProfile' object is not subscriptable ).

Found a workaround to the problem using ImageMagick@7 convert:

com_proc = subprocess.call(['convert',
                            img_tiff,
                            '-flatten',
                            '-profile', 'path/to/sRGB profile',
                            '-colorspace', 'RGB',
                            img_jpeg])

The result of conversion is very good, file size is quite small and most important of all, colours are matching the original tiff file.

enter image description here

Still would like to know how to do it properly from within Pillow (ICC profile reading and applying).

Python 3.6.3, Pillow 4.3.0, OSX 10.13.1

2

Answers


  1. Chosen as BEST ANSWER

    I've finally found the way to convert from CMYK to RGB from within Pillow (PIL) without recurring to external call to ImageMagick.

    #  first - extract the embedded profile:
    tiff_embedded_icc_profile = ImageCms.ImageCmsProfile(io.BytesIO(tiff_img.info.get('icc_profile')))
    
    #  second - get the path to desired profile (in my case sRGB)
    srgb_profile_path = '/Library/Application Support/Adobe/Color/Profiles/Recommended/sRGB Color Space Profile.icm'
    
    #  third - perform the conversion (must have LittleCMS installed)
    converted_image = ImageCms.profileToProfile(
                                         tiff_img,
                                         inputProfile=tiff_embedded_icc_profile,
                                         outputProfile=srgb_profile_path,
                                         renderingIntent=0,
                                         outputMode='RGB'
                                        )
    #  finally - save converted image:
    converted_image.save(new_name + '.jpg', quality=95, optimize=True)
    

    All colors are preserved.


  2. The reason why CMYK to RGB got the blue tint is because the conversion used only a very rough formula, probably something like:

      red = 1.0 – min (1.0, cyan + black)
    green = 1.0 – min (1.0, magenta + black)
     blue = 1.0 – min (1.0, yellow + black)
    

    To get the colours you expected you would need to use a proper Colour Management System (CMS), such as LittleCMS. Apparently Pillow is able to work with LCMS but I’m not sure if it’s included be default so you would probably need to build Pillow yourself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search