Python PDF to Image Converter Project

We offer you a brighter future with FREE online courses - Start Now!!

Many times we find the need to convert a PDF to Image. To fulfill this need , we are going to create a Pdf to Image Converter using Python. We are going to create a GUI window using Python that will provide us the facility of PDF to Image Converter. Now let’s start!!!

PDF to Image Converter

PDF to Image Converter is an application that helps us convert a PDF into Image. If there are multiple pages in the PDF then it will be saved as multiple images automatically.

Python PDF to Image Converter Project Details

In this project, we are going to use the Tkinter Module to create the GUI window for this project. To convert a PDF to an Image, we are going to use the pdf2image module.

In this project-

  1. Users will have to choose a pdf file
  2. After choosing the file, the name will pop up on the screen
  3. Click on the convert button and the pdf will be converted into an image and saved in the system

Prerequisites for PDF to Image Converter project

This is a beginner-level project so no prior knowledge is needed.

We will need to install the following modules to create the project-

  • Tkinter Module – pip install tk
  • Pdf2image Module – pip install pdf2image

Download the Source Code

Before proceeding with the project, click here to download the source code of the project: PDF to Image Converter Project Code

Steps to Create the PDF to Image Converter

The following are the steps to create the project;

  1. Import the Required Modules
  2. Creating the GUI Window
  3. Browsing a File
  4. Convert from Pdf to Image

1. Import the Required Modules:

#importing the required modules
from tkinter import *
from pdf2image import convert_from_path
from tkinter import filedialog
from tkinter import messagebox
  • Tkinter Module – Tkinter Module will help us create the GUI of our project.
  • Filedialog – This helps in creating a browsing window.
  • Messagebox – This helps in creating a pop up message window.
  • Pdf2image Module – This will help us convert a pdf to image.

2. Creating the GUI Window:

root = Tk()#creating GUI window
root.geometry('400x350')#geometry of window
root.title("ProjectGurukul")#title of window
  • Tk() – This method helps us create a window named root.
  • Geometry() – After creating the window, we are going to set its geometry i.e. its size using the geometry() method.
  • title() – title() assigns an appropriate title to the GUI window
#creating Labels
Label(root, text="Convert PDF to Image ",font="Arial 15",bg='green').pack()
Label(root, text="Choose a File ").pack()
#Creating a button
Button(root,command=browsefunc,text='browser').pack()
 
pathlabel = Label(root)
pathlabel.pack()
#Creating a button
Button(root, text="Convert", command=pdf2img,font='Arial, 16',bg='yellow').pack()
  • Using the Label() method, we are going to create a Label widget. In a label, we can display any piece of text. Inside a label() method, we can specify the color, font, size, background color etc of the text that needs to be displayed. We have displayed this widget using the pack() method.
  • To create a button, we are using the Button() method. We can specify the color, size, foreground color, font etc of the button. Command specifies which function should be evoked when the button is clicked. To display this Button on the window, we use the pack() method.
  • pack() – This helps in displaying the widgets on the window without specifying the x and y coordinates. Whichever widget is written first in the code gets displayed first.

3. Browsing the File:

def browsefunc():
    global filename1#global variable
    filename1 = filedialog.askopenfilename()#browsing a file
    pathlabel.config(text=filename1)#configuring the pathlabel Label
  • We have created a global variable filename1.
  • askopenfilename() – This will open up a browsing window from which the user can browse a file and open it.
  • config() – We created a Label named pathlabel. After the file is selected we configure the pathlabel widget and display the filename which is selected.

4. Conversion from PDF to Image:

def pdf2img():
    try:
        images = convert_from_path(filename1,dpi=200,poppler_path=r'C:\Users\Dell\Desktop\certificates')#converting from path
        for img in images:
            img.save('output.jpg', 'JPEG')#save file as jpeg
 
    except :
        Result = "NO pdf found"
        messagebox.showinfo("Result", Result)#display messagebox
 
    else:
        Result = "success"
        messagebox.showinfo("Result", Result)#display messagebox
  • Convert_from_path() – This takes in the file path selected by the user and converts it to an image.
  • Using a loop, we convert each page of the pdf to an image and save it using the save() method.
  • If no file is selected, then we show an error message using the messagebox library, else we show a success message if the image gets converted and saved successfully.

Python PDF to Image Converter Output

This is how the project GUI Window will look like

python pdf to image converter output

Summary

We have successfully created the python PDF to Image Converter Project. We have used the Tkinter Module and Pdf2image Module to create the project.

Leave a Reply

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