In this post we will see how to convert image to pdf using Python language. We will use here img2pdf module for converting image file to pdf file.
Though there are number of tools available for converting image to pdf file but still you may need to convert image using programming language for certain situations. Here we are going to use img2pdf library in Python 3 for converting image.
Why do you need to convert image to pdf?
- The size of the image file can vary widely depending on the resolution of the image but pdf takes up a lot less memory on computer hard disk than image.
- The scalability of images also creates issues in printing. The print quality of image depends on the resolution, and the printout may have a different form factor than the on screen image.
- JPG images do not scale down to small size well. This creates problem if the image contains icons, symbols or text.
- The Portable Document Format (pdf) was created to present a document (formatted text and image combined together) independent of application software, hardware, and operating systems. The content in pdf format looks same everywhere with same styles and same quality. Pdf has become the industry standard as the best format for the display and printing of data. It has a wide range of capabilities, such as non-lossy compression, interactive elements, multiple layers usage, encryption, digital signatures and much more.
Feature of img2pdf Module
It provides lossless conversion of raster images to PDF. You should use img2pdf if your
priorities are (in this order):
- Always lossless: the image embedded in the PDF will always have the
exact same color information for every pixel as the input
- Small: if possible, the difference in filesize between the input image
and the output PDF will only be the overhead of the PDF container itself
- Fast: if possible, the input image is just pasted into the PDF document
as-is without any CPU hungry re-encoding of the pixel data
Python 3.7.4, img2pdf 0.3.3
Install the required module
img2pdf using the following command from the command line tool. Make sure you open the command line tool in administrator mode.
Once succesfully installed you will get the success message as shown in the below image:
Converting Image to PDF
We will see here different ways of converting image to pdf file. We will convert both jpg or jpeg and png format images and write to pdf files. We will also convert multiple images to pdf files.
First you need to import
here we will see how to convert jpeg and png images into pdf files from their file names.
To convert png to pdf use below code:
with open("gentleman.pdf","wb") as f: f.write(img2pdf.convert('gentleman.png'))
The input image name is gentleman.jpg and the output file name is gentleman.pdf.
To convert jpeg or jpg to pdf use below code:
with open("sample.pdf","wb") as f: f.write(img2pdf.convert('sample.jpg'))
Input file name is sample.jpg and output file is sample.pdf.
Next we will see how to convert multiple images and write to a single pdf file. You can use same types or different types of images to convert. For example, here I have used both jpg and png images to write to pdf file.
with open("output1.pdf","wb") as f: f.write(img2pdf.convert("gentleman.png", "sample.jpg"))
In the above code two input images will be written to a single pdf file – output1.pdf. Another alternative approach to convert multiple images into pdf file is as follows:
with open("output2.pdf","wb") as f: f.write(img2pdf.convert(["gentleman.png", "sample.jpg"]))
If you need to specify the paper size, for example, you want that your image should be written to A4 paper size then you can do in the following way:
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297)) layout_fun = img2pdf.get_layout_fun(a4inpt) with open("paper_size.pdf","wb") as f: f.write(img2pdf.convert('sample.jpg', layout_fun=layout_fun))
There is another library called ImageMagick which is used to convert image to pdf file but using ImageMagick library it takes twice memory of using img2pdf library.
There is also another library pdflaTeX but it unnecessarily increases the size of the output pdf file.
You can download input, output and source code files from the following link.
Thanks for reading.