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.
感谢spec链接martineau,我将一些工作的
PIL
代码与检测Image
中是否存在adobergb-ICC配置文件的函数放在一起,并将颜色空间转换为sRGB。在(我使用这些创建了一个Djangoeasy-thumbnails处理器函数):
^{pr2}$我也遇到了同样的问题,我测试了所有的答案,最后得到了错误的颜色。@我尝试过的所有矩阵在红色和黑色中都给出了错误的结果,所以我的解决方案是:
我发现的唯一方法是从图像读取配置文件并使用ImageCms进行转换。在
我认为这种方法可以用于任何颜色的轮廓,但不能测试。在
要自己编程,可以将AdobeRGB颜色空间中的像素转换为CIE XYZ,然后将其转换为sRGB。PIL} 的部分注意,示例显示了从RGB到XYZ所需的矩阵值)。在
image
对象有一个名为convert()
的方法,能够对图像中的所有像素应用一般的矩阵变换(请参阅PIL image模块的联机文档中关于^{AdobeRGB1998.pdfspec中的4.3.4节显示了一个将XYZ转换为RGB的矩阵。在
我不知道如何检测JPG图像的颜色空间。我不记得它的有效性,但我不记得它的有效性。维基百科关于JPEG file format的文章说配置文件是嵌入的。在
相关问题 更多 >
编程相关推荐