You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PIL将CMYK图像转RGB时画面严重变浅且画质损坏的问题

解决Pillow转换CMYK图像为RGB时画质损坏的问题

问题根源

你的输入图像是CMYK模式(img.mode返回'CMYK'),且包含Adobe色彩配置信息(adobe_transform:2)。Pillow默认的convert("RGB")方法未正确适配Adobe的CMYK→RGB色彩转换规则,导致色彩失真、画质损坏。

修复方案

方案1:使用色彩管理模块(推荐)

利用Pillow的ImageCms模块,基于图像自带的ICC配置文件进行准确的色彩空间转换,这是最可靠的解决方式:

from PIL import Image
from PIL.ImageCms import profileToProfile

# 打开原CMYK图像
img = Image.open("input_image1.jpg")
# 获取图像内置的CMYK ICC配置
cmyk_profile = img.info.get('icc_profile')
# 目标RGB配置采用标准sRGB
rgb_profile = ImageCms.createProfile("sRGB")

# 执行色彩空间转换
img_rgb = profileToProfile(img, cmyk_profile, rgb_profile)
# 保存时保留原图像的300DPI参数
img_rgb.save("output_image1.jpg", format="jpeg", dpi=(300, 300))

方案2:手动指定转换矩阵(备选)

如果无法使用ImageCms(比如缺少依赖),可以针对Adobe CMYK配置手动设置转换矩阵:

from PIL import Image

img = Image.open("input_image1.jpg")
# Adobe CMYK转RGB的标准转换矩阵(适配Adobe RGB 1998)
adobe_cmyk_to_rgb_matrix = (
    2.0413690, -0.5649464, -0.3446944, 0.0,
    -0.9692660, 1.8760108, 0.0415560, 0.0,
    0.0134474, -0.1183897, 1.0154096, 0.0
)
img_rgb = img.convert("RGB", matrix=adobe_cmyk_to_rgb_matrix)
img_rgb.save("output_image1.jpg", format="jpeg", dpi=(300, 300))

注意事项

  • 保存输出图像时必须指定dpi=(300,300),否则会丢失原图像的高分辨率信息
  • 优先选择方案1,色彩管理转换能最大程度还原原图像的色彩和画质
  • 你的Pillow版本(10.2.0)已支持上述功能,无需降级或升级

内容的提问来源于stack exchange,提问作者Xabier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:33:26