Hue Tint Color error converting tiff to jpg in python3 with pillow

冷暖自知 提交于 2021-02-11 12:36:38

问题


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:

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

  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.

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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/47178272/hue-tint-color-error-converting-tiff-to-jpg-in-python3-with-pillow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!