Skip to content Skip to sidebar Skip to footer

How To Handle Multi-page Images In Pythonmagick?

I want to convert some multi-pages .tif or .pdf files to individual .png images. From command line (using ImageMagick) I just do: convert multi_page.pdf file_out.png And I get all

Solution 1:

ImageMagick is not memory efficient, so if you try to read a large pdf, like 100 pages or so, the memory requirement will be huge and it might crash or seriously slow down your system. So after all reading all pages at once with PythonMagick is a bad idea, its not safe. So for pdfs, I ended up doing it page by page, but for that I need to get the number of pages first using pyPdf, its reasonably fast:

pdf_im = pyPdf.PdfFileReader(file('multi_page.pdf', "rb"))
npage = pdf_im.getNumPages()
for p in npage:
    im = PythonMagick.Image('multi_page.pdf['+ str(p) +']')
    im.write('file_out-' + str(p)+ '.png')

Solution 2:

A more complete example based on the answer by Ivo Flipse and http://p-s.co.nz/wordpress/pdf-to-png-using-pythonmagick/

This uses a higher resolution and uses PyPDF2 instead of older pyPDF.

import sys
import PyPDF2
import PythonMagick

pdffilename = sys.argv[1] 
pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb"))
npage = pdf_im.getNumPages()
print('Converting %d pages.' % npage)
for p inrange(npage):
    im = PythonMagick.Image()
    im.density('300')
    im.read(pdffilename + '[' + str(p) +']')
    im.write('file_out-' + str(p)+ '.png')

Solution 3:

I had the same problem and as a work around i used ImageMagick and did

import subprocess
params = ['convert', 'src.pdf', 'out.png']
subprocess.check_call(params)

Post a Comment for "How To Handle Multi-page Images In Pythonmagick?"