Downloads
Learn
Tools
Contact
About
How to convert PDF to Image in Python using Wand(Answering Users' Questions)

Convert PDF to image in Python using Wand

Converting a pdf to image is a very useful tool for me personally. I use such tools to save online registration forms in image format and in many cases to share some useful texts in pdfs as image with friends. Wand in python is a very easy-to-use tool to get this job done.

I had made a youtube video on how to use wand to convert pdf to image.

This post is to answer some user questions posted in the above video.

First of all, for windows users, besides having installed python, pip and wand on your system, make sure you have installed Imagemagic and ghostscript on your system. You can download Imagemagic from here and ghostscript from here.

For quick reference, the full source code used in the video is below:

from wand.image import Image as wi
pdf = wi(filename="cs1.pdf", resolution=300)
pdfimage = pdf.convert("jpeg")
i=1
for img in pdfimage.sequence:
    page = wi(image=img)
    page.save(filename=str(i)+".jpg")
    i +=1

The above code converts all the pages of the pdf to images in sequence. So obviously, it works for both single-page pdf and multipage pdf.

Now coming to the question: How can we convert a specific page of the pdf only to image?

If you understand the above code and you have even a beginner's experience of programming, then the above task should be a simple programming excercise for you. But if you are too lazy to work out yourself then the following is one simple solution:

def singlePage(page_num):
    i=1
    for img in pdfimage.sequence:
        if (i==page_num):
            page = wi(image=img)
            page.save(filename=str(i)+".jpg")
        i +=1

You can use the above function to convert a specific page in the pdf to image. Again, if you ask me how to use the above function, then just run the following code to print the third page of the pdf:

from wand.image import Image as wi
pdf = wi(filename="cs1.pdf", resolution=300)
pdfimage = pdf.convert("jpeg")
def singlePage(page_num):
    i=1
    for img in pdfimage.sequence:
        if (i==page_num):
            page = wi(image=img)
            page.save(filename=str(i)+".jpg")
        i +=1
singlePage(3)

Just change the page_num argument in the singlePage(page_num) to whichever page you want to convert.

That's it for now. Any more questions and suggestions are welcome.






Author:


Ratul Doley
Ratul Doley
Expertised in ReactJs, NodeJS, Modern Js+Css, Php, Java. Professional Android and iOS app developer and designer. Updated Aoril 02, 2020