Skip to content

Instantly share code, notes, and snippets.

@Ausjorg
Created December 14, 2018 22:28
Show Gist options
  • Save Ausjorg/a5cc327096827573f2a9858cab0b42b4 to your computer and use it in GitHub Desktop.
Save Ausjorg/a5cc327096827573f2a9858cab0b42b4 to your computer and use it in GitHub Desktop.
Batch resize images to a smaller size by the width you set. Keeps aspect ratio. Python.
# Python 3.6.7
# OS: Ubuntu 18.04
# Date: 12-14-2018
# Author: Austin Jorgensen
# This batch resizes images to the width you set and keeps aspect ratio.
import os
import PIL # pip install Pillow
from PIL import Image
base_width = 100 # how many pixels wide to resize to?
target_format = ".png" # what image format are you targeting?
source_path = "." # where are you grabbing the original size image?
output_path = "./imgs/" # where do you want to put the resized images?
for files in os.listdir(source_path):
if files.endswith(target_format):
img = Image.open(files)
wpercent = (base_width / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((base_width, hsize), PIL.Image.ANTIALIAS)
img.save(output_path + files)
print("Finished: " + files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment