Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
Last active September 7, 2019 14:51
Show Gist options
  • Save zhangzhhz/1fc2b33d3e3212ebef5420a4f5317e44 to your computer and use it in GitHub Desktop.
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)
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