Created
May 27, 2022 15:08
-
-
Save boki1/3f10abb5e8386160124501024ba555f0 to your computer and use it in GitHub Desktop.
Create PDFs from multiple images
This file contains hidden or 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
from PIL import Image | |
from glob import glob | |
from argparse import ArgumentParser | |
from colorama import Fore, Style | |
extension='jpg' | |
path='./' | |
out=f'{path}/slides.pdf' | |
parser = ArgumentParser(description='Convert multiple images to a pdf') | |
parser.add_argument('--extension', help='extension of images', default='jpg', nargs='?') | |
parser.add_argument('--path', help='directory input path', default='./', nargs='?') | |
parser.add_argument('--out', help='directory output path', default='./style.pdf', nargs='?') | |
parser.add_argument('--images', type=list[str], nargs='+') | |
args = parser.parse_args() | |
def get_iter(): | |
if args.path and args.extension: | |
return glob(f'{args.path}/*.{args.extension}') | |
if args.images and len(args.images) > 0: | |
return args.images | |
print(f'Invalid arguments.') | |
exit(1) | |
obj_images = [] | |
for f in get_iter(): | |
print(f'{Fore.GREEN}Processing{Style.RESET_ALL} {f}') | |
obj_images.append(Image.open(f).convert('RGB')) | |
else: | |
print(f'{Fore.RED}Done{Style.RESET_ALL}') | |
print(f'{Fore.YELLOW}Outputing{Style.RESET_ALL} {args.out}') | |
obj_images[0].save(f'{args.out}', save_all=True, append_images=obj_images[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment