Created
September 20, 2021 10:21
-
-
Save Alfex4936/5b4525d6f0a035ada662c7d1cd5b0467 to your computer and use it in GitHub Desktop.
Python: Merge all images into one pdf
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
> python pdf.py -f 1.png 2.png 3.png | |
> python pdf.py -f 1.png 2.png -n outname |
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 argparse import ArgumentParser | |
from PIL import Image | |
if __name__ == "__main__": | |
parser = ArgumentParser() | |
parser.add_argument("--files", "-f", type=str, required=True, nargs="+") | |
parser.add_argument("--name", "-n", type=str) | |
args = parser.parse_args() | |
img_list = [] | |
base_img = Image.open(args.files[0]) | |
for img_path in args.files[1:]: | |
img_list.append(Image.open(img_path)) | |
if args.name and not args.name.endswith(".pdf"): | |
args.name += ".pdf" | |
pdf_name = args.name if args.name else "assignment.pdf" | |
base_img.save( | |
pdf_name, "PDF", resolution=100.0, save_all=True, append_images=img_list | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment