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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import numpy as np from PIL import Image import glob list_im = glob.glob("*.png") imgs = [ Image.open(i) 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) imgs_comb.save( '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) imgs_comb.save( 'Trifecta_vertical.png' ) |
Utilities

How to join more png in one pdf file
If you want instead join more png into one pdf, use this script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from PIL import Image from glob import glob import os iml = [] 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) # rgba = Image.open(PNG_FILE) # To avoid ValueError: cannot save mode RGBA rgba = Image.open(glob("*.png")[0]) rgb = Image.new('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 = Image.open(img) rgb2 = Image.new('RGB', rgba2.size, (255, 255, 255)) # white background rgb2.paste(rgba2, mask=rgba2.split()[3]) # paste using alpha channel as mask iml.append(rgb2) pdf = "ALL.pdf" rgb.save(pdf, "PDF" ,resolution=100.0, save_all=True, append_images=iml) |
How to make one pdf from more pdf
You need the PyPDF2 module
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import os from PyPDF2 import PdfFileMerger x = sorted([a for a in os.listdir() if a.endswith(".pdf")]) print(x) merger = PdfFileMerger() for pdf in x: merger.append(open(pdf, 'rb')) with open("result.pdf", "wb") as fout: merger.write(fout) os.startfile("result.PDF") |
Find the code here
https://github.com/formazione/pypdf
Subscribe to the newsletter for updatesPost written by

My youtube channel
Twitter: @pythonprogrammi - python_pygame
Higlighted videos
Speech recognition gamePygame cheatsheets Videos about this map editor
New free game: Crystal of time
How to make a map editor 1
How to make a map editor 2
How to make a map editor 3
Map editor 1.5
Map editor 1.6
How to make a videogame map editor with Python - MEP v. 2.1
Map editor for 2d platform game in Python with Pygame v.3.0
How to save a list with pickle with python
Pygame Map Editor 4.0 for Crystals of time
Github repository
Newest branch (to clone it with git)
git clone --branch cotb2 https://github.com/formazione/timecrystals.git