【问题标题】:How to keep same colors when converting CMYK image to RGB using PIL?使用 PIL 将 CMYK 图像转换为 RGB 时如何保持相同的颜色?
【发布时间】:2021-03-10 01:15:30
【问题描述】:

将 CMYK 图像转换为 RGB 时,我的颜色会反转。我想保持相同的(视觉上)颜色并删除 exif 数据。

我需要这样做才能在任何图像或 pdf 查看器上显示转换后的图像。

结果(代码如下)

原始 CMYK

图片太重无法上传 -> https://www.dropbox.com/s/q9z2jejhasjqdoy/original.jpeg?dl=0

转换后的 RGB

取消注释invert_pixels(image) 后转换为 RGB

使用TinyJPG 转换(此转换在 PDF 和其他图像查看器中正常工作。我想从根本上重新创建此图像配置文件)

堆栈

  • Python 3.5.9
  • PIL 5.1.0

测试代码:

image_filename = "original.jpeg"
image_file = open(image_filename, "rb")
image = Image.open(image_file)

orientate_image_using_exif(image)

image_data = list(image.getdata())

image_without_exif = image.convert("RGB")
image_without_exif.putdata(image_data)

# invert_pixels(image)

image_without_exif.save(
    u"RGB_{}".format(image_filename),
    progressive=True,
    icc_profile=image.info.get('icc_profile'),
    subsampling=0,
)

评论

  • 我转换为 RGB 只是因为 TinyJPG 图像的元数据显示它是 RGB
  • 考虑到像素反转让我更接近了多少,我的猜测是我需要为每个像素转换找到正确的公式来保持正确的颜色
  • orientate_image_using_exif 对最终颜色没有影响

问题

  • 谁能帮我让它正常工作?期望的结果就像 TinyJPG 图像。它确实进行无损压缩。我不想/不在乎。我只对删除 exif 数据(有效)和保持正确的颜色(无效)感兴趣

【问题讨论】:

  • 为什么要将图像转换为 RGB? JPEG 可以直接保存 CMYK/YCCK 图像。
  • 我正在尝试完全实现上一张图片中显示的内容。与上一张图像相同,将原始图像更改为 RGB 似乎可以使其在 PDF 查看器中工作。使用 PDF 查看器打开原始 CMYK 图像时会反转颜色
  • 在将图像转换为 RGB 时,也许您还应该删除 icc_profile。
  • 顺便说一句,您分享的图片不包含 Exif 数据(APP1 标记段)。
  • 是的,似乎没有。这意味着问题在于颜色空间如何在不同的阅读器中表示。理想的解决方案是在视觉上保持与压缩的微小 jpg 图像相同的颜色

标签: python image image-processing python-imaging-library jpeg


【解决方案1】:

我不确定我是否理解您在这里所做的事情。我使用以下代码获得了正确着色的剥离图像:

from PIL import Image

im = Image.open('original.jpg')
RGB = im.convert('RGB')
RGB.save('result.jpg')

我可以在exiftool 看到原版中的“ICC Profile”,如下所示:

exiftool original.jpg

ExifTool Version Number         : 12.00
File Name                       : original.jpg
Directory                       : .
File Size                       : 5.1 MB
File Modification Date/Time     : 2020:11:27 12:08:27+00:00
File Access Date/Time           : 2020:11:27 12:09:49+00:00
File Inode Change Date/Time     : 2020:11:27 12:08:43+00:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
DCT Encode Version              : 100
APP14 Flags 0                   : (none)
APP14 Flags 1                   : (none)
Color Transform                 : Unknown (RGB or CMYK)
Profile CMM Type                : Adobe Systems Inc.
Profile Version                 : 2.1.0
Profile Class                   : Output Device Profile
Color Space Data                : CMYK
Profile Connection Space        : Lab
Profile Date Time               : 2000:07:26 05:41:53
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Adobe Systems Inc.
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Adobe Systems Inc.
Profile ID                      : 0
Profile Description             : U.S. Web Coated (SWOP) v2
Profile Copyright               : Copyright 2000 Adobe Systems, Inc.
Media White Point               : 0.7084 0.73595 0.57104
A To B0                         : (Binary data 41478 bytes, use -b option to extract)
A To B2                         : (Binary data 41478 bytes, use -b option to extract)
A To B1                         : (Binary data 41478 bytes, use -b option to extract)
B To A0                         : (Binary data 145588 bytes, use -b option to extract)
B To A1                         : (Binary data 145588 bytes, use -b option to extract)
B To A2                         : (Binary data 145588 bytes, use -b option to extract)
Gamut                           : (Binary data 37009 bytes, use -b option to extract)
Image Width                     : 4167
Image Height                    : 4167
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 4
Image Size                      : 4167x4167
Megapixels                      : 17.4

我可以看到它在输出文件中是这样的:

exiftool result.jpg

ExifTool Version Number         : 12.00
File Name                       : result.jpg
Directory                       : .
File Size                       : 718 kB
File Modification Date/Time     : 2020:11:27 12:15:48+00:00
File Access Date/Time           : 2020:11:27 12:16:14+00:00
File Inode Change Date/Time     : 2020:11:27 12:15:54+00:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : None
X Resolution                    : 1
Y Resolution                    : 1
Image Width                     : 4167
Image Height                    : 4167
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 4167x4167
Megapixels                      : 17.4

关键字:图像处理、Python、PIL、Pillow、EXIF、strip EXIF、profile、ICC profile、strip profile。

【讨论】:

  • 非常感谢您抽出宝贵时间提供帮助。好的,似乎主要问题比我想象的更容易解决。只需将打开的图像转换为 RGB,PDF 查看器也可以正确显示。
  • 但是,如果您比较代码的结果:dropbox.com/s/6fh3yunscl25mz4/result_stack.jpeg?dl=0 和 TinyJPG 图像,颜色仍然不同。你知道怎么解决吗?
  • 问题是配置文件会影响颜色,一旦您将其删除,它们就会消失,您需要应用配置文件来修改颜色,然后在没有配置文件的情况下保存修改后的颜色。不知道该建议什么。
猜你喜欢
  • 2011-11-04
  • 2011-01-26
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
  • 2013-08-17
相关资源
最近更新 更多