问题
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