Create thumbnails in Python

I needed a script that would take a folder of pictures, resize the pictures, save them in a new folder and also make square thumbnails with the picture centered in the thumbnail. This Python script that I wrote does this very task quickly and produces lean images ( for example resulting thumbnails are 8 Kb comparing to Photoshop that for some reason was giving me images that are 5-8 times as heavy, 45 Kb for a 65×65 image)

The script also produces HTML code that you can just cut and paste into a responsive gallery likeĀ

You need to have PIL and Python installed to run this script.
Here’s the script:

[sourcecode language=”python”]

#!/usr/bin/env python
# encoding: utf-8

This script takes a folder named ‘photos’, creates two folders to store resized photos and thumbnails

Created by Maksim Surguy on 2012-04-09.


import os
from PIL import Image

def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):

def main():

# set max image dimensions
max_width = 640
max_height = 400

# set thumbnail size
thumb_size = (65, 65)

# optional – set the folder names if different names needed
photo_source_folder = "photos"
output_big_folder = "pics"
output_thumb_folder = "thumbs"

# do not edit below this line
htmlsource = ""
thumb_box = (0,0)

source_folder = os.getcwd() + "/" + photo_source_folder + "/"
current_folder = os.getcwd()+ "/"
ensure_dir(current_folder+ output_big_folder + "/")
ensure_dir(current_folder+ output_thumb_folder + "/")

dirList = [fname for fname in os.listdir(source_folder)
if fname.lower().endswith(".png") or fname.lower().endswith(".jpg")]
for fname in dirList:
print "Resizing %s" % fname
htmlsource += ‘<li><a href="#"><img src="%s/%s" data-large="%s/%s" alt="image" data-description="&nbsp;" /></a></li>n’%(output_thumb_folder,fname,output_big_folder, fname)

image = + fname)
if image.size[0] <= 640 and image.size[1] <= 400: + output_big_folder + "/" + fname, "JPEG", quality = 92)
image_big = image.copy()
image_big.thumbnail((max_width, max_height), Image.ANTIALIAS) + output_big_folder + "/" + fname, "JPEG", quality = 92)

image.thumbnail(thumb_size, Image.ANTIALIAS)
thumb_box = ( thumb_size[0]/2 – image.size[0]/2 , thumb_size[1]/2 – image.size[1]/2)
thumbnail = ("RGB", thumb_size)
thumbnail.paste(image, thumb_box) + output_thumb_folder + "/" + fname, "JPEG", quality = 90)

open("output.html", "w").write(htmlsource)

if __name__ == ‘__main__’:


You can download the script here:

You may also like


  • Michelle November 12, 2012  

    Hi…. Code works perfectly! I’m curious about the html part…. I don’t see that code generated anywhere within the Python26 folder. Can you direct me to it? Thanks!

  • Maks Surguy November 12, 2012  

    It should be in the folder where you execute the script… You can also replace the line :
    open(“output.html”, “w”).write(htmlsource)

    with :
    print htmlsource

    And that should give you the output in the window instead of the file.

  • Michelle November 12, 2012  

    That was a quick response, you are great, thanks! Will be trying the print script too, thank you SOO much!

  • Michelle November 12, 2012  

    One other thing… The output is going into the Python26 folder which is under C:\

    How do I do this so that I can use an original source folder on my desktop, and add the output folders and html to the source folder?

  • Maks Surguy November 13, 2012  

    Hmmm I haven’t checked this script on Windows, it must be the getcwd command that gives the path to the directory where script gets executed (in that case its the Python folder), not where the script is actually located.

    Some explanation is here:

    So instead of getcwd we need to use

    and also where the script writes to HTML we would need to do something like this as the location of the file :


    I will revise the whole script soon because there is an update to the original Responsive Gallery HTML+JS from CoDrops, it would make sense to make a new script that works on both Windows and MAC and then I’ll come back to this script and change the requirements.

  • MamaSchnack January 26, 2015  


    one question:
    How can I change the size of the output pics?

    It seems, they have always the same size, whatever dimensions I set…


Leave a comment