Skip to content

Instantly share code, notes, and snippets.

@stevenliebregt
Created March 2, 2018 11:56
Show Gist options
  • Save stevenliebregt/504f7f0be2b9880d836e20eee33996d1 to your computer and use it in GitHub Desktop.
Save stevenliebregt/504f7f0be2b9880d836e20eee33996d1 to your computer and use it in GitHub Desktop.
Resize images, edit to change folders and or sizes
#!/usr/bin/python3
import os
from PIL import Image
CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))
INPUT_PATH = CURRENT_PATH + '/png'
OUTPUT_PATH = CURRENT_PATH + '/png_50'
TARGET_SIZE_X = 50
TARGET_SIZE_Y = 50
TARGET_SIZE = TARGET_SIZE_X, TARGET_SIZE_Y
def resize(file):
print('...resizing : ' + file + '...')
infile = INPUT_PATH + '/' + file
outfile = OUTPUT_PATH + '/' + file
im = Image.open(infile)
im.thumbnail(TARGET_SIZE)
im.save(outfile)
if __name__ == '__main__':
if not os.path.exists(INPUT_PATH):
print('Error, input path `png` does not exist')
exit(1)
if not os.path.exists(OUTPUT_PATH):
os.mkdir(OUTPUT_PATH)
print('Start resizing...')
for file in os.listdir(INPUT_PATH):
resize(file)
print('...done resizing!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment