色相颜色错误将tiff转换为python3中带有枕头的jpg [英] Hue Tint Color error converting tiff to jpg in python3 with pillow

查看:86
本文介绍了色相颜色错误将tiff转换为python3中带有枕头的jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 通过从CMYK到RGB的颜色模式转换创建的缩略图变为蓝色:

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

  1. 在不进行颜色模式转换的情况下创建的缩略图(带有CMYK的jpeg)得到的结果与光敏照片相似,但不能用于网络(仍然是蓝色).

代码段:

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

此外,当尝试包含tiff的ICC配置文件时,它在某种程度上已损坏,并且photoshop抱怨配置文件已损坏.我使用以下命令将配置文件包含在保存功能中:

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?

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

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 ).

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

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])

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

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

还是想从Pillow(ICC配置文件的读取和应用)中正确地做到这一点.

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

Python 3.6.3, Pillow 4.3.0, OSX 10.13.1

推荐答案

我终于找到了从枕头(PIL)内将CMYK转换为RGB的方法,而无需重复调用ImageMagick.

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)

所有颜色都会保留.

这篇关于色相颜色错误将tiff转换为python3中带有枕头的jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆