Skip to content

Pdf to png

In this blog we'll learn how you can convert a PDF into any image format (PNG, JPG and JPEG) using Python.

We're going to be creating a basic GUI with the Tkinter Python module. This GUI will take the user inputted PDF file from the local directory and convert into an image format.

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 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

https://youtu.be/3XkwR_D9WoY


Last update: November 18, 2023
Created: October 12, 2023