How to Convert Image to PDF in Python?
Last Updated :
27 Jan, 2023
img2pdf is an open source Python package to convert images to pdf format. It includes another module Pillow which can also be used to enhance image (Brightness, contrast and other things) Use this command to install the packages
pip install img2pdf
Below is the implementation: Image can be converted into pdf bytes using img2pdf.convert() functions provided by img2pdf module, then the pdf file opened in wb mode and is written with the bytes.
Python
import img2pdf
from PIL import Image
import os
img_path = "C: / Users / Admin / Desktop / GfG_images / do_nawab.png"
pdf_path = "C: / Users / Admin / Desktop / GfG_images / file .pdf"
image = Image. open (img_path)
pdf_bytes = img2pdf.convert(image.filename)
file = open (pdf_path, "wb")
file .write(pdf_bytes)
image.close()
file .close()
print ("Successfully made pdf file ")
|
Output:
Successfully made pdf file
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...