Convert Image to PDF Using Python

Convert Image to pdf


We will show you how to convert image to PDF using python in this project. The Tkinter, OS, PIL, and reportlab packages were used to develop this project. The Tkinter package makes it possible to create GUI programs quickly and simply. The OS library offers a means of interacting with the operating system, which is useful for manipulating file paths. To work with photos, utilize PIL (Python Imaging Library). The tool Reportlab is used to create PDFs. This software features a simple button and label design.


With the label “Select an Image File” and buttons to choose an image and convert it to PDF, this code generates a basic Tkinter-based graphical user interface. Following conversion, a label with the PDF file location appears. Reportlab is used to convert pictures to PDF, PIL is used to work with images, and OS library is used to communicate with the operating system.

Source Code

import os
from tkinter import Tk, Button, Label, filedialog
from PIL import Image
from reportlab.pdfgen import canvas

class ImageToPDFConverter:
    def __init__(self, master):
        self.master = master
        master.title("Image to PDF Converter")

        self.label = Label(master, text="Select an image file:")

        self.select_button = Button(master, text="Select Image", command=self.select_image)

        self.convert_button = Button(master, text="Convert to PDF", command=self.convert_to_pdf, state='disabled')

    def select_image(self):
        file_path = filedialog.askopenfilename(title="Select an image file", filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")])
        if file_path:
            self.image_path = file_path
            self.label.config(text=f"Selected image: {os.path.basename(self.image_path)}")

    def convert_to_pdf(self):
        if hasattr(self, 'image_path'):
            image =
            pdf_filename = os.path.splitext(os.path.basename(self.image_path))[0] + '.pdf'
            pdf_path = os.path.join(os.path.expanduser("~"), "Downloads", pdf_filename)

            with open(pdf_path, 'wb') as pdf_file:
                pdf = canvas.Canvas(pdf_file)
                pdf.setPageSize((image.width, image.height))

                pdf.drawInlineImage(self.image_path, 0, 0, width=image.width, height=image.height)


            self.label.config(text=f"Conversion successful! PDF saved at: {pdf_path}")

if __name__ == "__main__":
    root = Tk()
    app = ImageToPDFConverter(root)


convert image to pdf using python
convert image to pdf using python

Leave a Comment