i want to convert my pdf files with ImageMagic python module PythonMagick but i havent found any solution on net.

import PythonMagick
im = PythonMagick.Image('files.pdf')
im.write("file_img%d.png")

i am using below code and it returns only first page of my pdf file.

any help would be greatly appreciated

Recommended Answers

All 5 Replies

since there really isnt a whole lot of documentation (at least that I could find) I will make an assumption --
PDF files are basically a collection of "pages" and it seems that you are simply running the create image function on the first "page"

I would encourage you to find a way to break apart the pages of the pdf, and run the image creation individually....

you can probably try...

from pyPdf import PdfFileReader
from PythonMagick import Image


myfile = PdfFileReader('files.pdf')
pages = myfile.getNumPages()

for page in pages:
    im = Image(myfile.getPage(i+1))
    im.write('file_image{}.png'.format(i+1))

Now.. Im a bit of a newb with Python... so.. would be interested to know if I figured that out right.. You may have to download the PyPdf package to make it work, though...

Ryan

Here is how I split a pdf file into pages from python by calling pdftk

pdf_name = "foo.pdf"
ndigits = 2
com_line = "pdftk A={src} burst output {dst}".format(
        src = pdf_name,
        dst = "%0{ndigits}d.pdf".format(ndigits=ndigits))
)
subprocess.call(com_line, shell=True)

cant figure out how to edit my post... so...
I made a boo boo..

change 'i+1' to 'page+1' in both instances, assuming my script works in the first place...

and the more I look at it, I think pages would be an int... so the for line should read:

for page in range(pages):
    ....

I'm not a developer, i always use this <snip> to convert pdf to image online.

commented: irrelevant and spammish -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.