pdfcmykpixmappymupdf

Color issue when saving PDF page Pixmap as PNG using PyMuPDF


I'm running the following bit of Python code from the PyMuPDF 1.16.17 documentation, which save PNG images for every page in a PDF file.

import sys, fitz  # import the binding
fname = "test.pdf" # get filename from command line
doc = fitz.open(fname)  # open document
for page in doc:  # iterate through the pages
    pix = page.getPixmap()
    pix.writePNG("F:/cynthia/page-%i.png" % page.number)  # store image as a PNG

The resulting PNG images' colors are off from the PDF originals (too saturated and high contrast). I know function Page.getPixmap() has a "colorspace" argument, and using Document.getPageImageList I found out that my PDF's colorspace is "DeviceCMYK". But when I try to get a Pixmap using CMYK as colorspace (replacing the pix = page.getPixmap() line with pix = page.getPixmap(colorspace="CMYK") or `pix = page.getPixmap(colorspace=csCMYK)), it doesn't change the resulting colors. Any help is appreciated.


Solution

  • Please upgrade your PyMuPDF version. Then ICC color support will be included which should improve your output.