Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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, output_dir="", size=(1024,768)):
outfile = os.path.splitext(infile)[0]+"_resized"
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(output_dir+outfile+extension,"JPEG")
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)
@ihercowitz

This comment has been minimized.

Copy link
Owner Author

@ihercowitz ihercowitz commented Oct 23, 2010

This Script requires PIL (easy_install pil)

@Shafeeanwar

This comment has been minimized.

Copy link

@Shafeeanwar Shafeeanwar commented May 14, 2018

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

@crazywolf132

This comment has been minimized.

Copy link

@crazywolf132 crazywolf132 commented May 19, 2018

@Shafeeanwar mate. Correct it yourself, The size is defined on line 6.

@issacjohn96

This comment has been minimized.

Copy link

@issacjohn96 issacjohn96 commented Dec 5, 2018

@Shafeeanwar Thug Life

@tfossoul

This comment has been minimized.

Copy link

@tfossoul tfossoul commented Jan 4, 2019

Damned I don't find the line 6 ! (just kiding)

@NidhiDua

This comment has been minimized.

Copy link

@NidhiDua NidhiDua commented Apr 22, 2019

@Shafeeanwar did u find the solution for the issue u mentioned>? if yes can u plz share

@Mahibro

This comment has been minimized.

Copy link

@Mahibro Mahibro commented May 10, 2019

@Shafeeanwar Try this ,it works
#!/usr/bin/python
from PIL import Image
import os, sys

path = "/root/Desktop/python/images/"
dirs = os.listdir( path )

def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((200,200), Image.ANTIALIAS)
imResize.save(f + ' resized.jpg', 'JPEG', quality=90)

resize()

@doomSDey

This comment has been minimized.

Copy link

@doomSDey doomSDey commented Jul 13, 2019

Thanks brother

@Saigesp

This comment has been minimized.

Copy link

@Saigesp Saigesp commented Sep 25, 2019

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

@sakshi8398

This comment has been minimized.

Copy link

@sakshi8398 sakshi8398 commented Nov 27, 2019

cmp function is not defined anymore in python 3.x

@shayan09

This comment has been minimized.

Copy link

@shayan09 shayan09 commented Dec 26, 2019

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)
@angyee

This comment has been minimized.

Copy link

@angyee angyee commented Feb 3, 2020

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?

@NightmareXv

This comment has been minimized.

Copy link

@NightmareXv NightmareXv commented Feb 21, 2020

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
You can’t perform that action at this time.