Skip to content

Instantly share code, notes, and snippets.

@Davidcparrar
Created May 22, 2022 23:36
Show Gist options
  • Select an option

  • Save Davidcparrar/944be9fc8c7cc235a86855000a50bf95 to your computer and use it in GitHub Desktop.

Select an option

Save Davidcparrar/944be9fc8c7cc235a86855000a50bf95 to your computer and use it in GitHub Desktop.
File to create a pdf from a list of images
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()
@Davidcparrar
Copy link
Author

Usage from terminal:

python convert2pdf.py file1.jpg file2.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment