使用PIL将JPG从AdobeRGB转换为sRGB?

2024-10-03 00:17:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何检测JPG是否为AdobeRGB,以及是否在python中将其转换为srgbjpg。在

如果在PIL中这是可能的,那就太好了。谢谢您。在


Tags: pil中将jpgsrgbjpgadobergb
3条回答

感谢spec链接martineau,我将一些工作的PIL代码与检测Image中是否存在adobergb-ICC配置文件的函数放在一起,并将颜色空间转换为sRGB。在

adobe_to_xyz = (
    0.57667, 0.18556, 0.18823, 0,
    0.29734, 0.62736, 0.07529, 0,
    0.02703, 0.07069, 0.99134, 0,
) # http://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf                                

xyz_to_srgb = (
    3.2406, -1.5372, -0.4986, 0,
    -0.9689, 1.8758, 0.0415, 0,
    0.0557, -0.2040, 1.0570, 0,
) # http://en.wikipedia.org/wiki/SRGB                                                     

def adobe_to_srgb(image):
    return image.convert('RGB', adobe_to_xyz).convert('RGB', xyz_to_srgb)

def is_adobe_rgb(image):
    return 'Adobe RGB' in image.info.get('icc_profile', '')

# alternative solution if happy to retain profile dependency:                             
# http://stackoverflow.com/a/14537273/284164                                              
# icc_profile = image.info.get("icc_profile")                                             
# image.save(destination, "JPEG", icc_profile=icc_profile)

(我使用这些创建了一个Djangoeasy-thumbnails处理器函数):

^{pr2}$

我也遇到了同样的问题,我测试了所有的答案,最后得到了错误的颜色。@我尝试过的所有矩阵在红色和黑色中都给出了错误的结果,所以我的解决方案是:

我发现的唯一方法是从图像读取配置文件并使用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.

我认为这种方法可以用于任何颜色的轮廓,但不能测试。在

要自己编程,可以将AdobeRGB颜色空间中的像素转换为CIE XYZ,然后将其转换为sRGB。PILimage对象有一个名为convert()的方法,能够对图像中的所有像素应用一般的矩阵变换(请参阅PIL image模块的联机文档中关于^{}的部分注意,示例显示了从RGB到XYZ所需的矩阵值)。在

AdobeRGB1998.pdfspec中的4.3.4节显示了一个将XYZ转换为RGB的矩阵。在

我不知道如何检测JPG图像的颜色空间。我不记得它的有效性,但我不记得它的有效性。维基百科关于JPEG file format的文章说配置文件是嵌入的。在

相关问题 更多 >