Created
October 23, 2010 20:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Nice script and so useful!
I've made some improvements (dirs and size can be passed with command line arguments):
#!/usr/bin/env python
import os
import sys
import argparse
from PIL import Image
"""
Reduce images size
Example: python image_resize.py -d /home/user/images -o /home/user/output_dir -s 1024 768
"""
def resizeImage(infile, output_dir, size):
outfile = os.path.splitext(os.path.basename(infile))[0]
extension = os.path.splitext(infile)[1]
if (cmp(extension, ".jpg")):
return
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(os.path.join(output_dir, outfile+extension),"JPEG")
except IOError:
print "cannot reduce image for ", infile
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", help="Directory to look up for images")
parser.add_argument("-o", help="Output directory")
parser.add_argument("-s", nargs=2, type=int, help="Output size")
args = parser.parse_args()
input_dir = os.path.normpath(args.d) if args.d else os.getcwd()
output_dir = os.path.normpath(args.o) if args.o else os.path.join(os.getcwd(), 'resized')
output_size = tuple(args.s) if args.s else (1024,768)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for file in os.listdir(input_dir):
resizeImage(os.path.join(input_dir, file), output_dir, output_size)
Gist here: https://gist.github.com/Saigesp/111cc103a837344f4fcda2a2d2090c66
cmp function is not defined anymore in python 3.x
Here is the modified script for python 3
#!/usr/bin/env python
import os
import sys
import argparse
from PIL import Image
"""
Reduce images size
Example: python image_resize.py -d /home/user/images -o /home/user/output_dir -s 1024 768
"""
def cmp(a, b):
return (a > b) - (a < b)
def resizeImage(infile, output_dir, size):
outfile = os.path.splitext(os.path.basename(infile))[0]
extension = os.path.splitext(infile)[1]
if (cmp(extension, ".jpg")):
return
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(os.path.join(output_dir, outfile+extension),"JPEG")
except IOError:
print("cannot reduce image for ", infile)
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", help="Directory to look up for images")
parser.add_argument("-o", help="Output directory")
parser.add_argument("-s", nargs=2, type=int, help="Output size")
args = parser.parse_args()
input_dir = os.path.normpath(args.d) if args.d else os.getcwd()
output_dir = os.path.normpath(args.o) if args.o else os.path.join(os.getcwd(), 'resized')
output_size = tuple(args.s) if args.s else (1024,768)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for file in os.listdir(input_dir):
resizeImage(os.path.join(input_dir, file), output_dir, output_size)
Here is the modified script for python 3
#!/usr/bin/env python import os import sys import argparse from PIL import Image """ Reduce images size Example: python image_resize.py -d /home/user/images -o /home/user/output_dir -s 1024 768 """ def cmp(a, b): return (a > b) - (a < b) def resizeImage(infile, output_dir, size): outfile = os.path.splitext(os.path.basename(infile))[0] extension = os.path.splitext(infile)[1] if (cmp(extension, ".jpg")): return if infile != outfile: try: im = Image.open(infile) im.thumbnail(size, Image.ANTIALIAS) im.save(os.path.join(output_dir, outfile+extension),"JPEG") except IOError: print("cannot reduce image for ", infile) if __name__=="__main__": parser = argparse.ArgumentParser() parser.add_argument("-d", help="Directory to look up for images") parser.add_argument("-o", help="Output directory") parser.add_argument("-s", nargs=2, type=int, help="Output size") args = parser.parse_args() input_dir = os.path.normpath(args.d) if args.d else os.getcwd() output_dir = os.path.normpath(args.o) if args.o else os.path.join(os.getcwd(), 'resized') output_size = tuple(args.s) if args.s else (1024,768) if not os.path.exists(output_dir): os.mkdir(output_dir) for file in os.listdir(input_dir): resizeImage(os.path.join(input_dir, file), output_dir, output_size)
no effect on output dir no save file?
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
Thanks brother