[英]Hue Tint Color error converting tiff to jpg in python3 with pillow
从tiff(CMYK)文件保存jpg缩略图时,我遇到以下问题:
程式码片段:
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配置文件的读取和应用)。
Python 3.6.3,Pillow 4.3.0,OSX 10.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。
我终于找到了从枕头(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)
保留所有颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.