向图像添加 alpha 通道和颜色配置文件

Add an alpha channel and color profile to image

我正在尝试获取具有 alpha 通道和 sRGB IEC61966-2.1 颜色配置文件的图像。

我开始使用的图像没有 Alpha 通道,但它有我想要的颜色配置文件。参见 starting image

如果我运行以下,

from PIL import Image
img = Image.open('84.png')
print(img.mode)
img.convert('RGBA').save('84a.png')

我得到了一个 alpha 通道,但颜色配置文件似乎不见了。参见 ending image. Note that img.mode is 'P'. I saw this ,但如果可能的话,我想在没有 cv2 的情况下进行。此外,我认为解决方案是从一张已经有 alpha 通道的图像开始的。也许它适用,但我遗漏了一些东西。

谢谢

这个 帮助解决了这个问题。我所做的只是在 /System/Library/ColorSync/Profiles/ 找到我想要的 *.icc 文件。我将它复制到我的 运行 目录并命名为 sRGB.icc。那我运行

from PIL import Image, ImageCms
img = Image.open('84.png').convert('RGBA')
img = ImageCms.profileToProfile(img, 'sRGB.icc', 'sRGB.icc', renderingIntent=0, outputMode='RGBA')
img.save('84a.png')

这给了我想要的 alpha 通道和颜色配置文件。但是,它有点 'hacky',因为 profileToProfile() 应该从一个配置文件转换为另一个配置文件。 Python returns None 当我 运行 Image.open('84.png').info.get('icc_profile') 所以似乎没有识别初始颜色配置文件。