How to handle multi-page images in PythonMagick?

后端 未结 3 802
臣服心动
臣服心动 2021-02-09 14:16

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         


        
3条回答
  •  粉色の甜心
    2021-02-09 14:38

    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 in range(npage):
        im = PythonMagick.Image()
        im.density('300')
        im.read(pdffilename + '[' + str(p) +']')
        im.write('file_out-' + str(p)+ '.png')
    

提交回复
热议问题