Created
May 22, 2022 23:36
-
-
Save Davidcparrar/944be9fc8c7cc235a86855000a50bf95 to your computer and use it in GitHub Desktop.
File to create a pdf from a list of 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 | |
| import argparse | |
| import sys | |
| def process_image(file: str) -> Image: | |
| """Load and convert image to RGB""" | |
| image = Image.open(file) | |
| im = image.convert("RGB") | |
| return im | |
| def get_name(file: str) -> str: | |
| """Creates .pdf name from file""" | |
| split_name = file.split(".") | |
| split_name[-1] = ".pdf" | |
| new_name = "".join(split_name) | |
| return new_name | |
| def main(): | |
| """Main Function""" | |
| parser = argparse.ArgumentParser(description="Script to convert to pdf") | |
| parser.add_argument( | |
| "file", | |
| help="File name", | |
| nargs="+", | |
| ) | |
| args = parser.parse_args() | |
| if args.file: | |
| list_files = [] | |
| args.file.reverse() | |
| for file in args.file: | |
| print(f"Processing: {file}") | |
| if not "." in file: | |
| raise FileExistsError(f"{file} Incorrect file") | |
| list_files.append(process_image(file)) | |
| name = get_name(file) | |
| list_files[0].save(name, save_all=True, append_images=list_files[1:]) | |
| else: | |
| parser.print_help() | |
| sys.exit() | |
| if __name__ == "__main__": | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage from terminal: