Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PIL将JPG从AdobeRGB转换为sRGB?_Python_Image Processing_Python Imaging Library_Jpeg_Color Profile - Fatal编程技术网

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

Python 使用PIL将JPG从AdobeRGB转换为sRGB?,python,image-processing,python-imaging-library,jpeg,color-profile,Python,Image Processing,Python Imaging Library,Jpeg,Color Profile,如何检测JPG是否为AdobeRGB,以及是否在python中将其转换为srgbjpg 如果这在PIL中是可能的,那就太好了。谢谢。要自行编程,您可以将AdobeRGB颜色空间中的像素转换为CIE XYZ,然后将其转换为sRGB。PILimage对象有一个名为convert()的方法,该方法能够对图像中的所有像素应用通用矩阵变换(请参见PIL图像模块在线文档中的一节——请注意显示从RGB到XYZ所需矩阵值的示例) AdobeRGB1998.pdf中的第4.3.4节显示了将XYZ转换为RGB的矩阵

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


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

要自行编程,您可以将AdobeRGB颜色空间中的像素转换为CIE XYZ,然后将其转换为sRGB。PIL
image
对象有一个名为
convert()
的方法,该方法能够对图像中的所有像素应用通用矩阵变换(请参见PIL图像模块在线文档中的一节——请注意显示从RGB到XYZ所需矩阵值的示例)

AdobeRGB1998.pdf中的第4.3.4节显示了将XYZ转换为RGB的矩阵


我不知道如何检测JPG图像的颜色空间。我记得读到过一些关于ICC xml概要文件被附加到文件末尾的内容(当有多个文档时会出现问题),但我不能保证它的有效性。维基百科上的文章说配置文件是嵌入的。

感谢规范链接martineau,我将一些工作的
PIL
代码与检测
图像中是否存在Adobe RGB 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)
(我使用这些函数创建处理器函数):


我也有同样的问题,我测试了所有的答案,结果在最终的图像中得到了错误的颜色@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.

我认为这种方法可以用于任何颜色配置文件,但没有经过测试。

@miki725:如果您采用这种方法,请注意,两种矩阵变换可以结合使用,这将允许在一个步骤中完成转换(一个
im.convert()
call)。有关将矩阵相乘的更多信息,请参阅此维基百科。请参阅问题——很抱歉,上次链接被弄乱了。您是否仍有兴趣获得此问题的更好答案?当然。你想出了一个更好的方法?不完全是,但我可以通过其他方法来改进我的答案,使之更具体——如果这是你一直在寻找的,因为到目前为止你还没有接受答案。是的,你可以组合矩阵,在一个
转换
操作中完成。
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.