TooYoung
TooYoung

Reputation: 387

Pythonmagick fails to read transparent pdf file

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?

Upvotes: 1

Views: 245

Answers (0)

Related Questions