How to Convert Image to PDF in Python

Updated September 12, 2023

In the world of digital document management, the ability to convert images into PDF files is a crucial task with a myriad of applications. Whether it's archiving important photographs, creating professional presentations, or consolidating scanned documents, the process of transforming images into PDFs is an essential skill for individuals and businesses alike. Fortunately, Python, with its versatile libraries and powerful tools, offers a straightforward and efficient way to accomplish this task.

In this article, we will explore the world of image-to-PDF conversion in Python, providing you with valuable insights and practical code examples to help you master this invaluable skill. So, let's embark on this journey. The Python file and library we will use in this article is IronPDF for Python.

How to Convert Images to PDF in Python

  1. Install the Python library for converting images to PDF files.
  2. Utilize the ImageToPdfConverter method to convert an image to a PDF file.
  3. Save the newly generated PDF documents using the SaveAs method.
  4. Convert multiple images to a single PDF file object.
  5. Run the project to see the output file.

1. What Is IronPDF?

IronPDF for Python is a versatile library that enables developers to easily generate, manipulate, and work with file types of PDF documents in Python applications. It provides a comprehensive set of tools for creating PDFs from scratch, converting HTML and images into PDFs, and performing various operations on existing PDFs, such as merging, splitting, and adding interactive elements to all the files like forms and annotations. With its user-friendly API, IronPDF simplifies PDF generation and manipulation tasks, making it a valuable tool for developers looking to integrate PDF functionality into their Python projects, whether for generating reports, invoices, or interactive documents.

2. Creating a New PyCharm Project and Install IronPDF

To create a new PyCharm project and install IronPDF, you'll need to follow these steps:

  1. Install PyCharm: If you haven't already, download and install PyCharm from the JetBrains website ( Make sure you have Python installed on your system as well.
  2. Create a New PyCharm Project: Open PyCharm and follow these steps to create a new project:
    • Click on "File" in the top-left corner.
    • Select "New Project..."
    • Choose the location where you want to create your project and give it a name.
    • Select the Python interpreter you want to use for your project (you may need to create a virtual environment if you haven't already).
  3. Install IronPDF: To install the IronPDF library, you can use the Python package manager pip. Open the terminal within PyCharm and run the following command:

     pip install ironpdf

    This command will download and install the IronPDF library and its dependencies.

3. Convert Images to PDF Files

Once the new PyCharm project folder is created and IronPDF is installed, we can convert an image to a PDF using IronPDF for Python. In the code snippet below, you will see how you can convert an image to a PDF file extension.

from ironpdf import *
pdf = ImageToPdfConverter.ImageToPdf("win to pay.png")

This code snippet uses the IronPDF library to convert an image file named "win to pay.png" into a PDF document. It first creates an ImageToPdfConverter object, passing the path to the image file as a parameter. Then, it saves the converted PDF with the filename "image-to-pdf.pdf" using the SaveAs method. Essentially, it takes an image and transforms it into a PDF file, which can be useful for various document management and sharing purposes.

3.1. Output PDF File

How to Convert Image to PDF in Python: Figure 1 - An invoice output as a PDF.

4. Convert Multiple Images to a PDF File

Using IronPDF for Python, you can convert and merge all the images in a folder into a single PDF file with just a few lines of code.

The below Python Script will show how to composite multiple images into a single PDF file.

from ironpdf import *
image_files = [os.path.join("assets", f) for f in os.listdir("assets") if f.lower().endswith(('.jpg', '.jpeg'))]
directory_list = List [str]()
for i in range(len(image_files)):
    directory_list.Add(image_files [i])

This code utilizes the IronPDF library to convert a list of image files with the extensions '.jpg' or '.jpeg' from a specified directory ("assets") into a single composite PDF document. Here's a step-by-step explanation:

  1. It starts by importing the necessary modules from the IronPDF library.
  2. It uses a list comprehension to generate a list of image file paths from the "assets" directory that meet the criteria of having a '.jpg' or '.jpeg' file extension. This list is stored in the image_files variable.
  3. A list named directory_list is created to store the paths of the selected image files.
  4. It then iterates through the image_files list and adds each image file path to the directory_list using the Add method.
  5. Finally, it calls ImageToPdfConverter.ImageToPdf(directory_list) to convert the list of image files into a composite PDF document and save it as "composite.pdf" using the SaveAs method.

In summary, this code scans a directory for image files with specific extensions, compiles them into a single PDF document, and saves the resulting PDF as "composite.pdf."

4.1. Output Image

How to Convert Image to PDF in Python: Figure 2 - A PDF of photos, each taking up one page.

5. Conclusion

In today's digital age, the ability to convert images into PDF files is a valuable skill that finds application in various aspects of our lives. Whether it's organizing cherished photos, other data, creating professional presentations, or managing scanned documents, the process of transforming images into PDFs is a fundamental need for individuals and businesses. Thankfully, Python, with its versatile libraries and robust tools, offers a simple and efficient solution to this task.

In this article, we've explored the world of image-to-PDF conversion in Python, providing you with practical insights and easy-to-follow examples to help you master this essential skill. Through the example of the use of IronPDF for Python, we've demonstrated how you can effortlessly convert individual images into PDF documents and even merge multiple images into a single, cohesive PDF file.

With IronPDF's user-friendly API, Python developers can streamline PDF generation, conversion, and manipulation tasks, making it a valuable resource for projects involving reports, invoices, interactive documents, and more. By following the steps outlined in this article, you can harness the power of Python and elevate your document management capabilities, all while simplifying your workflow and improving efficiency.

If you are wondering how to convert PDF files to images, check out this tutorial. Also, the source code for the example Image to PDF can be found as an example on the IronPDF website.

How to Extract Invoice Data From PDF in Python
How to Generate PDF Forms in Python

Ready to get started? Version: 2024.5 just released

Free pip Install View Licenses >