我如何检测 JPG 是否为 AdobeRGB,以及是否在 python 中将其转换为 sRGB JPG。
如果这在 PIL 中是可能的,那就太好了。谢谢。
最佳答案
我遇到了同样的问题,我测试了所有答案并在最终图像中得到了错误的颜色。 @DrMeers 我试过的所有矩阵都给出了错误的红色和黑色结果,所以这是我的解决方案:
我发现的唯一方法是从图像中读取配置文件并使用 ImageCms 进行转换。
from PIL import Image
from PIL import ImageCms
import tempfile
def is_adobe_rgb(img):
return 'Adobe RGB' in img.info.get('icc_profile', '')
def adobe_to_srgb(img):
icc = tempfile.mkstemp(suffix='.icc')[1]
with open(icc, 'w') as f:
f.write(img.info.get('icc_profile'))
srgb = ImageCms.createProfile('sRGB')
img = ImageCms.profileToProfile(img, icc, srgb)
return img
img = Image.open('testimage.jpg')
if is_adobe_rgb(img):
img = adobe_to_srgb(img)
# then do all u want with image. crop, rotate, save etc.
我认为这个方法可以用于任何颜色配置文件,但没有经过测试。
关于python - 使用 PIL 将 JPG 从 AdobeRGB 转换为 sRGB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11041044/