Skip to content

Instantly share code, notes, and snippets.

@crearo
Created May 4, 2018 07:41
Show Gist options
  • Save crearo/64dc48678b7ee6b99a33fb970c5130b2 to your computer and use it in GitHub Desktop.
Save crearo/64dc48678b7ee6b99a33fb970c5130b2 to your computer and use it in GitHub Desktop.
resize-images-in-directory.py
#!/usr/bin/env python
'''
uses PIL to resize all images in a directory
'''
import Image
import os, sys
def resizeImage(infile, output_dir="", size=(640,480)):
outfile = os.path.splitext(infile)[0]
extension = os.path.splitext(infile)[1]
# if (cmp(extension, ".png")):
# return
if infile != outfile:
try :
im = Image.open(infile)
w, h = im.size
# im = im.crop((w/2.0 - 5, 10, w/2.0 + 5, h-10))
im = im.resize(size)
path =output_dir+outfile+extension
print 'saving to',path
im.save(path,"PNG")
except IOError:
print "cannot reduce image for ", infile
if __name__=="__main__":
output_dir = "resized/"
dir = os.getcwd()
if not os.path.exists(os.path.join(dir,output_dir)):
os.mkdir(output_dir)
for file in os.listdir(dir):
resizeImage(file,output_dir, size=(256,256))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment