使用枕头在 python3 中将 tiff 转换为 jpg 时出现 Hue Tint Color 错误

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

从 tiff (CMYK) 文件保存 jpg 缩略图时遇到以下问题:

  1. 使用从 CMYK 到 RGB 的颜色模式转换创建的缩略图获得蓝色色调:

  1. 在 photoshop 中使用相同的 tiff 创建的缩略图(无 ICC 配置文件,未转换为 sRGB,仅 RGB)以正确的方式获得颜色:

  1. 未经颜色模式转换(jpeg 与 CMYK)创建的缩略图与经过 Photoshop 处理的缩略图得到相似的结果,但不能用于网络(仍然是蓝色)。

代码片段:

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

此外,当尝试包含 tiff 的 ICC 配置文件时,它会以某种方式损坏,并且 photoshop 抱怨配置文件被损坏。我使用以下方法在保存函数中包含配置文件:

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

我在这里做错了什么/遗漏了什么?

编辑:

搜索解决方案后,我发现问题与 icc 配置文件有关。 Tiff 文件有 FOGRA 配置文件,jpeg 应该有一些 sRGB。无法从 Pillow 中进行工作配置文件转换(ImageCms.profileToProfile() 正在抛出 PyCMSError cannot build transform'ImageCmsProfile' object is not subscriptable'PIL._imagingcms.CmsProfile' object is not subscriptable)。

使用 ImageMagick@7 convert 找到解决问题的方法:

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

转换的结果非常好,文件很小,最重要的是,颜色与原始 tiff 文件匹配。

仍然想知道如何从 Pillow 中正确地做到这一点(阅读和应用 ICC 配置文件)。

Python3.6.3,枕头 4.3.0,OSX10.13.1

CMYK 到 RGB 之所以有蓝色调是因为转换只使用了一个非常粗略的公式,大概是这样的:

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

要获得您期望的颜色,您需要使用适当的颜色管理系统 (CMS),例如 LittleCMS。显然 Pillow 能够与 LCMS 一起使用,但我不确定它是否包含在默认情况下,因此您可能需要自己构建 Pillow。

我终于找到了从 Pillow (PIL) 中将 CMYK 转换为 RGB 的方法,而无需重复调用 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)

保留所有颜色。