Python Resize All Pictures Of A Folder

Do you like this?

This script helps you reduce sizes of all images in the current folder in which the script is run.


Before running the script, you have to install Python Image Editing library:

pip install PILLOW
You can change the following settings to your liking: resize_method, max_width, max_height, extensions.
import os
from PIL import Image
resize_method = Image.ANTIALIAS
    #Image.NEAREST)  # use nearest neighbour
    #Image.BILINEAR) # linear interpolation in a 2x2 environment
    #Image.BICUBIC) # cubic spline interpolation in a 4x4 environment
    #Image.ANTIALIAS) # best down-sizing filter

max_height= 1200
max_width= 1200
extensions= ['JPG']

path= os.path.abspath(".")

def adjusted_size(width,height):
    if width>max_width or height>max_height:
        if width>height:
            return max_width, int (max_width * height/ width)
            return int (max_height*width/height), max_height
        return width,height

if __name__ == "__main__":
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path,f)):
            f_text, f_ext= os.path.splitext(f)
            f_ext= f_ext[1:].upper()
            if f_ext in extensions:
                print f
                image =,f))
                width, height= image.size
                image = image.resize(adjusted_size(width, height))

comments powered by Disqus