Amazing-Python-Scripts
25 строк · 646.0 Байт
1import os2import img2pdf3
4# path tot the directory which contains the files
5path = ""6
7images = [imgs for imgs in os.listdir(path) if imgs.endswith(8('.jpg', '.jpeg', '.png', '.gif'))]9
10images.sort()11
12# List to store image bytes of images present in the directory
13images_bytes = list()14
15# converting all the images to image-bytes and appending them to a list for further processing
16for i in images:17with open(os.path.join(path, i), "rb") as im:18images_bytes.append(im.read())19
20# To convert image bytes to pdf-bytes
21
22pdf_bytes = img2pdf.convert(images_bytes)23
24with open('Output.pdf', "wb") as pdfFile:25pdfFile.write(pdf_bytes)26