python - 使用 PIL 将 JPG 从 Adob​​eRGB 转换为 sRGB?

标签 python image-processing python-imaging-library jpeg color-profile

我如何检测 JPG 是否为 Adob​​eRGB,以及是否在 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 从 Adob​​eRGB 转换为 sRGB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11041044/

相关文章:

python - 如何修复 pandas FutureWarning : inferring datetime64[ns] from data containing strings is deprecated

Windows中的python选项卡完成

python - 从图像中提取多个背景中的文本

c# - 如何检测图像中的单词

python - 遍历图像列表并将它们分配为 python 中的变量

python - Django REST Framework 授权 token

python - 将文件从 URL 传输到 Cloud Storage

image - 如何从裁剪的图像中找到原件

python - 如何将图像大小调整为 shape=(None, 321, 321, 3) 且名称 name=None 且 dtype=tf.float32

python - 将RGB图像转换为黑白PIL手部识别