Skip to content

Instantly share code, notes, and snippets.

@donvito
Created August 13, 2019 04:28
Show Gist options
  • Save donvito/49e70f43ad120316b196bb5fc3ee2db5 to your computer and use it in GitHub Desktop.
Save donvito/49e70f43ad120316b196bb5fc3ee2db5 to your computer and use it in GitHub Desktop.
Python script to resize photos
import os, sys
from PIL import Image
size = 512, 512
resizedFolder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resized")
if not os.path.exists(resizedFolder):
os.makedirs(resizedFolder)
for file in os.listdir("./"):
if file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png") :
if file.endswith(".jpg"):
outfile = os.path.splitext(file)[0] + ".jpg"
if file.endswith(".jpeg"):
outfile = os.path.splitext(file)[0] + ".jpeg"
if file.endswith(".png"):
outfile = os.path.splitext(file)[0] + ".png"
newFolder = os.path.join(resizedFolder, outfile)
try:
img = Image.open(file)
img.thumbnail(size)
img.save(newFolder, "JPEG")
except IOError, ioe:
print "cannot resize", infile
print str(ioe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment