How to Convert Image to PDF in Python

In this tutorial, we will learn how to convert image to PDF file in Python.

As we all Know PDF (Portable Document Format) is much more reliable than the Image (JPG, PNG), As they occupy less space, they are not easily editable this ensures there security too. However, there are certain disadvantages of PDF like there integrity with the webpages is a little bit tedious work to do.

So, let us learn how to convert the Image into PDF in Python. Now before converting our Image to PDF, there is one important thing that we need to understand, Images are generally found in two formats jpg/jpeg & png. The JPG images are in RGB mode where R – Red, G- Green, B- Blue and PNG images are mostly found in RGBA mode where RGB has the usual meaning and A – Alpha Channel (it is a factor responsible for transparency or opacity in images).

But PDF has nothing to do with Alpha Channel and it does not support it too. So to convert Images with Alpha Channel into PDF, we need to first convert them into RGB mode and then into PDF.

Also learn: How to Add Watermark to a PDF File Using Python

Based on the above discussion, I had found two ways to convert Image to PDF.

  • Using the img2pdf  Library.
  • Using the Python Imaging Library (PIL).

Image to PDF using img2pdf Python Library

Advantages:

  • Lossless – The PDf generated are lossless i.e there is no deduction in pixels.
  • Smaller – The PDF generated is nearly similar in size of the input image only the overhead of PDF containers are added.
  • Faster – This converts Images to PDF faster as compared to other solutions in the same format.

Disadvantages:

  •  The only disadvantage is that it does not take Image consisting of Alpha Channel as input as there is no direct method in this library to convert RGBA mode image to RGB mode one.

Installation:

$ pip3 install img2pdf

Source Code:

# Importing Required Module
import img2pdf

# Creating Image File Object
ImgFile = open("test1.jpg","rb")

# Creating PDF File Object
PdfFile = open("test1.pdf","wb")

# Converting Image File to PDF
PdfFile.write(img2pdf.convert(ImgFile))

#Closing Image File Object
ImgFile.close()

#Closing PDF File Object
PdfFile.close()

Image to PDF Using the Python Imaging Library (PIL)

Python Imaging Library (PIL) (it’s newer version is Pillow), it is a free library available which provides various operations on Images such as:

  • Opening and Closing of Images.
  • Manipulation at the Pixel Level i.e each and every pixel can be handled separately.
  • Filtering of Images, such as Blurring, Contouring, Smoothing, etc.
  • Enhancing of Images, such as Sharpening, Brightness Adjustment, Contrast or Color Settings.
  • Adding Text to the Images for editing purposes.

And It Supports all the file formats such as JPG/JPEG, PNG, TIFF, GIF, etc. Here using a mode method we detect the mode of Image (RGBA/RGB) and convert it suitably for the required file format.

Installation:

$ pip install Pillow

Source Code:

# Importing Required Module
from PIL import Image

# Creating Image File Object
ImgFile = Image.open("test2.png")

# Cheaking if Image File is in 'RGBA' Mode
if ImgFile.mode == "RGBA":
# If in 'RGBA' Mode Convert to 'RGB' Mode
  ImgFile = ImgFile.convert("RGB")

# Converting and Saving file in PDF format
ImgFile.save("test2.pdf","PDF")

# Closing the Image File Object
ImgFile.close()

There are various other techniques also through which you can convert Image to PDF, but I had found these two as the simple ones you may go with the other one. So, I hope this article might be useful to you, thank you “Keep Learning Keep Coding”.

Leave a Reply

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