Convert from CMYK to RGB

梦想与她 提交于 2020-05-27 04:09:23

问题


I'm having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. I've tried reading through the Wand docs, but haven't found anything to simply replicate the original image. The official ImageMagick docs themselves are still rather opaque to me. For my situation, it is necessary to do this within a python script.

Below is the relevant code snippet.

with Image(filename = HOME + outFileName + ".pdf", resolution = 90) as original:
    original.format = "jpeg"
    original.crop(width=500, height=500, gravity="center")
    original.save(filename = HOME + outFileName + ".jpg")

How can I accurately convert from CMYK to RGB?

UPDATE: Here are links to a sample pdf and its converted output.

Original PDF

Converted to JPG


回答1:


This script will convert image to RGB and save it in-place if it detects that the image is in CMYK mode:

from PIL import Image
image = Image.open(path_to_image)
if image.mode == 'CMYK':
    image = image.convert('RGB')



回答2:


Finally I solved this problem. A CMYK mode JPG image that contained in PDF must be invert.

But in PIL, invert of CMYK mode image is not supported. Than I solve it using numpy.

Full source is in below link. https://github.com/Gaia3D/pdfImageExtractor/blob/master/extrectImage.py

See line 166~170.

imgData = np.frombuffer(img.tobytes(), dtype='B')
invData = np.full(imgData.shape, 255, dtype='B')
invData -= imgData
img = Image.frombytes(img.mode, img.size, invData.tobytes())
img.save(outFileName + ".jpg")


来源:https://stackoverflow.com/questions/33101659/convert-from-cmyk-to-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!