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 = [ 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.
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
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
Twitter: @pythonprogrammi - python_pygame