How to join png into one file or one pdf and how to join more pdf files

I had the will to join more images taken from a Powerpoint file in just one vertical image file. Searching on Stack overflow I found something and I modified so that it works with any file in the directory where you have the png files (instead of 3 of a given name).

import numpy as np
from PIL import Image
import glob

list_im = glob.glob("*.png")
imgs    = [ for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )

# save that beautiful picture
imgs_comb = Image.fromarray( imgs_comb) 'Trifecta.png' )    

# for a vertical stacking it is simple: use vstack
imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
imgs_comb = Image.fromarray( imgs_comb) 'Trifecta_vertical.png' )


How to join more png in one pdf file

If you want instead join more png into one pdf, use this script.

from PIL import Image
from glob import glob
import os

iml = []

files = glob("*.png")
#, 'PDF', resoultion=100.0)
for f in files:
    newname = f[:-4] + ".png"
    os.rename(f, newname)
files = glob("*.png")
# rgba =
# To avoid ValueError: cannot save mode RGBA 
rgba ="*.png")[0])
rgb ='RGB', rgba.size, (255, 255, 255))  # white background
rgb.paste(rgba, mask=rgba.split()[3])               # paste using alpha channel as mask
for img in files:
    rgba2 =
    rgb2 ='RGB', rgba2.size, (255, 255, 255))  # white background
    rgb2.paste(rgba2, mask=rgba2.split()[3])               # paste using alpha channel as mask
pdf = "ALL.pdf", "PDF" ,resolution=100.0, save_all=True, append_images=iml)

How to make one pdf from more pdf

You need the PyPDF2 module

import os
from PyPDF2 import PdfFileMerger

x = sorted([a for a in os.listdir() if a.endswith(".pdf")])
merger = PdfFileMerger()

for pdf in x:
    merger.append(open(pdf, 'rb'))

with open("result.pdf", "wb") as fout:

Find the code here

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

Twitter: @pythonprogrammi - python_pygame


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.