[英]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.