Skip to content

Instantly share code, notes, and snippets.

@MXP2095onetechguy
Created July 14, 2023 08:43
Show Gist options
  • Select an option

  • Save MXP2095onetechguy/dcebf379ff0ee7e418f353658f5d02a0 to your computer and use it in GitHub Desktop.

Select an option

Save MXP2095onetechguy/dcebf379ff0ee7e418f353658f5d02a0 to your computer and use it in GitHub Desktop.
Convert and compile multiple images to one multipaged TIFF image file.
#/usr/bin/env python3
# import modules
from PIL import Image
import argparse
import glob
# initialize magic images
tiffimg = Image.new("RGB", (80, 80), "white")
imgs = []
# initialize argument parsing
aparse = argparse.ArgumentParser(prog="tiffc", description="Compile a multipaged TIFF file from other image files.")
aparse.add_argument("-o", "--output", dest="output", help="Where to emit the multipaged TIFF file to?", default="outputmp.tiff", metavar="WHERE")
aparse.add_argument("images", help="Which image files are to be used?", nargs="+", metavar="IMAGES")
# parse and grab arguments
args, _ = aparse.parse_known_args()
out = args.output
simgs = args.images
# Grab all image path, glob them and open them to be added to 'imgs'
for pattern in simgs:
# Use glob to expand file patterns into actual file paths
for imp in glob.glob(pattern):
imgs.append(Image.open(imp))
# Convert and save each image as a multipaged TIFF
tiffimg.save(out, format='TIFF', save_all=True, append_images=imgs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment