Pdf to Image Conversion using Python

Share this post

In this blog we will learn how you can convert any pdf file into images such as png, jpg and jpeg  formate using python code.

We make a simle GUI using Tkinter .This GUI take (by passing a pdf file ) a pdf file from your local directy and will convert into image formate. 

Step 1

1st of all Download Poppler from here here,Then extract it.In the code section just add poppler_path=r’C:Program Filespoppler-0.68.0bin’(for eg.) like below

Step 2

you need to install pdf2image library by using the commond below

# install this library
pip install pdf2image

step 3

Write a python script and also put download file name poppler into same directly

#http://blog.alivate.com.au/poppler-windows/

from tkinter import filedialog as fd
filename = fd.askopenfilename()

from pdf2image import convert_from_path
from tkinter import *
from tkinter import messagebox
 
print(filename)
 
def pdf2img():
    try:
        images = convert_from_path(filename,dpi=200,poppler_path=r'poppler-0.68.0\bin')
        for i, image in enumerate(images):
            fname = 'image'+str(i)+'.png'
            image.save(fname, "PNG")

    except  :
        Result = "NO pdf found"
        messagebox.showinfo("Result", Result)
 
    else:
        Result = "success"
        messagebox.showinfo("Result", Result)
 
master = Tk()
Label(master, text="File Location").grid(row=0, sticky=W)
 
b = Button(master, text="Convert", command=pdf2img)
b.grid(row=0, column=2,columnspan=2, rowspan=2,padx=5, pady=5)
  
mainloop()

Run the script and it will show you results like this


Share this post

Leave a Comment

Your email address will not be published. Required fields are marked *