How to handle multi-page images in PythonMagick?

I want to convert some files with multiple .tif or .pdf pages to separate .png images. From the command line (using ImageMagick) I just do:

convert multi_page.pdf file_out.png 

And I get all the pages as separate images (file_out-0.png, file_out-1.png, ...)

I would like to handle this file conversion in Python, unfortunately PIL cannot read .pdf files, so I want to use PythonMagick. I tried:

 import PythonMagick im = PythonMagick.Image('multi_page.pdf') im.write("file_out%d.png") 

or simply

 im.write("file_out.png") 

But I only get 1 page converted to png. Of course, I could load each page individually and convert them one at a time. But should there be a way to do them all at once?

+6
source share
3 answers

ImageMagick is not memory efficient, so if you try to read a large pdf file, for example, 100 pages, the memory demand will be huge and may lead to a malfunction or serious slowdown of your system. So after all reading, all pages at once with PythonMagick is a bad idea, it is unsafe. So, for pdf files, I ended up working page by page, but for this I need to first get the number of pages 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') 
+6
source

A more complete example based on the answer of Ivo Flips and http://ps.co.nz/wordpress/pdf-to-png-using-pythonmagick/

This uses a higher resolution and uses PyPDF2 instead of the old 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 in range(npage): im = PythonMagick.Image() im.density('300') im.read(pdffilename + '[' + str(p) +']') im.write('file_out-' + str(p)+ '.png') 
+1
source

I had the same problem and I worked with ImageMagick and did

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

Source: https://habr.com/ru/post/915136/


All Articles