本文介绍了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。

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

如果它转换为sRGB然后如果我传入Adobe RGB图像转换功能,它会成功转换为sRGB吗?

If it converts to sRGB then if I pass in Adobe RGB image to 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位值。因此,它从根本上改变了图像表示和存储方式的模式

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 中找到关键'icc_profile',就像这样 image.info.get('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):
    icc = img.info.get('icc_profile', '')
    if icc:
        fd = io.BytesIO(icc)
        sPrf = ImageCms.ImageCmsProfile(fd)
        dPrf = ImageCms.createProfile('sRGB')
        img = ImageCms.profileToProfile(img, sPrf, dPrf)
    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', ''):
    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 other quality than 100.

示例输入(Adobe RGB JPEG):

Example input (Adobe RGB JPEG):

示例输出(sRGB JPEG):

Example output (sRGB JPEG):

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

09-18 00:30