Skip to content

Instantly share code, notes, and snippets.

@B0und
Created June 17, 2021 12:21
Show Gist options
  • Save B0und/419d3066d9cedba88a2a8f9e2a9401d6 to your computer and use it in GitHub Desktop.
Save B0und/419d3066d9cedba88a2a8f9e2a9401d6 to your computer and use it in GitHub Desktop.
Convert png files to one pdf, with transparency replaced with white colour
"""
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