Created
June 17, 2021 12:21
-
-
Save B0und/419d3066d9cedba88a2a8f9e2a9401d6 to your computer and use it in GitHub Desktop.
Convert png files to one pdf, with transparency replaced with white colour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Convert png files to one pdf, with transparency replaced with white colour | |
""" | |
import glob | |
from PIL import Image | |
IMG_FOLDER = "rez" | |
files = [] | |
for img in glob.glob(f"{IMG_FOLDER}/*.png"): | |
rgba = Image.open(img) | |
rgb = Image.new("RGB", rgba.size, (255, 255, 255)) # white background | |
rgb.paste(rgba, mask=rgba.split()[3]) # paste using alpha channel as mask | |
files.append(rgb) | |
files[0].save( | |
"out.pdf", "PDF", resolution=100.0, save_all=True, append_images=files[1:] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment