How to save all png to pdf with just Python and PIL

This script lets you include all the png files in a folder into one pdf file, whatever is the dimension of the png files. It needs only the PIL module.

It takes one image and attaches all the other to the first and then creates a pdf out of this list of images.

To install PIL

pip install pillow

from PIL import Image
from glob import glob
import os

# ============= create pdf =========

files = glob("*.png")
# rgb.save(PDF_FILE, 'PDF', resoultion=100.0)
for f in files:
    print(f)
    print(f[:-4])
    newname = f[:-4] + ".png"
    print(newname)
    os.rename(f, newname)

files = glob("*.png")
print(files)


iml = []
print(f"{files=}")
for img in files:
    imgs = Image.open(img)
    iml.append(imgs)
pdf = "ALL.pdf"
print(iml)
image = iml[0]
iml.pop(0)
image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml)

os.system("ALL.pdf")

https://github.com/formazione/utilities


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.