Last active
September 7, 2019 14:51
-
-
Save zhangzhhz/1fc2b33d3e3212ebef5420a4f5317e44 to your computer and use it in GitHub Desktop.
A way to reszie images and save them to one pdf file using Python package Pillow (PIL fork)
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 | |
# assumed ".jpg" files | |
def resize_images(files, width, height): | |
for f in files: | |
img = Image.open(f).resize((width, height)) # Note: this won't keep aspect ratio | |
img.save(f.replace('.jpg', '_resized.jpg')) | |
def save_to_pdf(files, pdf_file_name): | |
for i, f in enumerate(files): | |
img = Image.open(f) | |
to_append = True | |
if i == 0: | |
to_append = False | |
img.save(pdf_file_name, format='pdf', append=to_append) | |
files = glob('*.jpg') | |
resize_images(files, 1000, 800) | |
save_to_pdf(files, 'mypdf.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment