PIL image.convert("RGB")是否将图像转换为sRGB或AdobeRGB? [英] Does PIL image.convert("RGB") convert images to sRGB or AdobeRGB?

查看:515
本文介绍了PIL image.convert("RGB")是否将图像转换为sRGB或AdobeRGB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python中的PIL将图像从RGBa,CMYK等各种模式批量转换为sRGB.

I want to batch convert images from various modes such as RGBa, CMYK etc to sRGB using PIL in Python.

但是,我不确定是否可以在任何地方找到它

However, I am not sure and can't find it anywhere whether

image.convert("RGB") 

将图像转换为sRGB或Adobe RGB.我的直觉说,它可以转换为sRGB.

converts images to sRGB or Adobe RGB. My intuition says that it converts to sRGB.

如果将其转换为sRGB,那么如果我将Adobe RGB图像传递给convert函数,它将成功转换为sRGB吗?

If it converts to sRGB then if I pass in Adobe RGB image to the convert function, would it successfully convert to sRGB?

推荐答案

默认情况下,PIL不了解颜色空间(例如sRGB和Adobe RGB).

By default PIL is agnostic about color spaces (like sRGB and Adobe RGB).

您可能知道,RGB只是红色,绿色和蓝色的三个8位值. CMYK是四个8位值.使用Image.convert('RGB')时,它只是将每个像素转换为三位8位值.因此,它从根本上改变了图像表示和存储方式的 mode .

As you might know, RGB is simply three 8-bit values for red, green and blue. CMYK is four 8-bit values. When you use Image.convert('RGB') it just converts each pixel to the triple 8-bit value. So it fundamentally changes the mode of how image is represented and stored.

sRGB和Adobe RGB只是 RGB颜色空间.基本上,这些是有关如何在监视器,打印机或任何其他物理输出设备上呈现这三个值的规则.例如,可以通过诸如Photoshop之类的软件以ICC配置文件的形式将此类规则添加到JPEG文件中.但是当您仅使用Image.save时,PIL通常不会附加任何ICC配置文件.

sRGB and Adobe RGB, on the other hand, are just RGB color spaces. Basically, these are rules for how to render those three values on your monitor, printer or any other physical output device. Such rules can be added to JPEG files in form of ICC profiles by such software as Photoshop, for example. But PIL normally will not attach any ICC profile when you just use Image.save.

为了检查您的图像是否嵌入了ICC配置文件,您可以像这样image.info.get('icc_profile', None)在字典image.info中寻找键'icc_profile'.

In order to check if your image embeds ICC profile, you can look for key 'icc_profile' in dictionary image.info like this image.info.get('icc_profile', None).

要使用PIL保存保留ICC配置文件的JPEG图像,您可以使用以下方式:

In order to save JPEG images preserving ICC profile with PIL you could use something like this:

img.save('image.jpg',
         format = 'JPEG',
         quality = 100,
         icc_profile = img.info.get('icc_profile',''))

要将任何RGB JPEG图像从其色彩空间转换为sRGB,可以使用此功能:

In order to convert any RGB JPEG image from its color space to sRGB, you could use this function:

import io
from PIL import Image
from PIL import ImageCms

def convert_to_srgb(img):
    '''Convert PIL image to sRGB color space (if possible)'''
    icc = img.info.get('icc_profile', '')
    if icc:
        io_handle = io.BytesIO(icc)     # virtual file
        src_profile = ImageCms.ImageCmsProfile(io_handle)
        dst_profile = ImageCms.createProfile('sRGB')
        img = ImageCms.profileToProfile(img, src_profile, dst_profile)
    return img

因此,用于转换为单一RGB颜色空间的代码可能如下所示:

So your code for conversion to single RGB color space might look like:

img = Image.open('image_AdobeRGB.jpg')
img_conv = convert_to_srgb(img)
if img.info.get('icc_profile', '') != img_conv.info.get('icc_profile', ''):
    # ICC profile was changed -> save converted file
    img_conv.save('image_sRGB.jpg',
                  format = 'JPEG',
                  quality = 100,
                  icc_profile = img_conv.info.get('icc_profile',''))

注意:您可能希望使用略低于100的质量.

NOTE: you might want to use slightly lower quality than 100.

示例输入(Adobe RGB JPEG):

Example input (Adobe RGB JPEG):

示例输出(sRGB JPEG):

Example output (sRGB JPEG):

这篇关于PIL image.convert("RGB")是否将图像转换为sRGB或AdobeRGB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆