Skip to content

Instantly share code, notes, and snippets.

@ihercowitz
Created October 23, 2010 20:19
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ihercowitz/642650 to your computer and use it in GitHub Desktop.
Save ihercowitz/642650 to your computer and use it in GitHub Desktop.
Python Script to resize all the images, on a given directory, to a 1024x768 JPEG format.
#!/usr/bin/env python
import Image
import os, sys
def resizeImage(infile, dir, output_dir="", size=(1024,768)):
outfile = os.path.splitext(infile)[0]+"_resized"
extension = os.path.splitext(infile)[1]
if extension.lower()!= ".jpg":
return
if infile != outfile:
try :
im = Image.open(dir+os.sep+infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(output_dir+os.sep+outfile+extension,"JPEG")
except IOError:
print "cannot reduce image for ", infile
if __name__=="__main__":
dir = os.getcwd()
if len(sys.argv[1:]) > 0:
args = sys.argv[1:]
if args[0] == "-d":
if args[1]!="./":
dir = args[1]
output_dir = dir+os.sep+"resized"
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for file in os.listdir(dir):
resizeImage(file,dir,output_dir=output_dir)
@NightmareXv
Copy link

images are resized keeping the aspect ratio same as the original e.g. 3136x2091 image is resized to 1024x682 & not 1024x768. I need all images of same size so pls do correct it

U r a legende xd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment