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
# Python3 program to convert image to pdf# using img2pdf library# importing necessary librariesimport img2pdffrom PIL import Imageimport os# storing image pathimg_path = "C:/Users/Admin/Desktop/GfG_images/do_nawab.png"# storing pdf pathpdf_path = "C:/Users/Admin/Desktop/GfG_images/file.pdf"# opening imageimage = Image.open(img_path)# converting into chunks using img2pdfpdf_bytes = img2pdf.convert(image.filename)# opening or creating pdf filefile = open(pdf_path, "wb")# writing pdf files with chunksfile.write(pdf_bytes)# closing image fileimage.close()# closing pdf filefile.close()# outputprint("Successfully made pdf file") |
Output:
Successfully made pdf file
