1

I am using PythonMagick to convert multiple-page .pdf files into .png files but failed as long as the .pdf file's background are transparent.

My ImageMagick version is 7.0.7-5 and Ghostscript version is 9.22

Here is what I did:

# just read the first page as an example:
import PythonMagick
im =  PythonMagick.Image()
im.density('400')          # change dpi
im.read('myfile.pdf[0]')   # error occurs
# im.write('target_dir')

Then it will return an error

RuntimeError: python.exe: iCCP: Not recognizing known sRGB profile that has been edited `C:\Users\...\AppData\Local\Temp\5\magick-19492pMuJsmcrWoca1' @ warning/png.c/MagickPNGWarningHandler/1832

I can use the following code to change the background and avoid the error but I can't change image dpi with this method:

# just read the first page as an example:
import PythonMagick

im =  PythonMagick.Image('myfile.pdf[0]')
im.density('400') # doesn't work

bgColour = '#ffffff'
size = "%sx%s" % (im.columns(), im.rows())
flattened = PythonMagick.Image(size, bgColour)
flattened.type = im.type
flattened.composite(im, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp)
flattened.density('400') # also not working
flattened.write('target_dir')

It seems that as long as I use .read() it will return the error but changing dpi requires .read() function. Is there any solution to both read the pdf and change the dpi?

TooYoung
  • 387
  • 8
  • 18
  • 1
    Do you have to do it via PythonMagick? It's easy enough to do on the command line calling Ghostscript directly, eg `gs -dSAFER -dNOPAUSE -dBATCH -r400 -sDEVICE=pngalpha -sOutputFile=output%03d.png myfile.pdf` where `-r400 ` sets the resolution and `output%03d.png` is a pattern to set filenames with 3 digit sequence numbers. I haven't tried it with PDFs, but it works fine with PostScript and EPS files. There's also an option to set a background colour: `-dBackgroundColor=16#RRGGBB`. See https://www.ghostscript.com/doc/9.21/Devices.htm#PNG – PM 2Ring Oct 18 '17 at 18:17
  • The `PythonMagick` library doesn't really offer much for exception handling. In this case, it looks like a common `iCCP` warning bubbles up into a full `RuntimeError` in python. – emcconville Oct 18 '17 at 20:31
  • Thank you guys, directly calling Ghostscript works. But I am still wondering if there is any solution with PythonMagick – TooYoung Oct 18 '17 at 20:54

0 Answers0