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Ā http://tympanus.net/codrops/2011/09/20/responsive-image-gallery/

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
"""
resizeImages.py

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

Created by Maksim Surguy on 2012-04-09.
https://maxoffsky.com

"""

import os
from PIL import Image

def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(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 = Image.open(source_folder + fname)
if image.size[0] <= 640 and image.size[1] <= 400:
image.save(current_folder + output_big_folder + "/" + fname, "JPEG", quality = 92)
else:
image_big = image.copy()
image_big.thumbnail((max_width, max_height), Image.ANTIALIAS)
image_big.save(current_folder + 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 = Image.new ("RGB", thumb_size)
thumbnail.paste(image, thumb_box)
thumbnail.save(current_folder + output_thumb_folder + "/" + fname, "JPEG", quality = 90)

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

if __name__ == ‘__main__’:
main()

[/sourcecode]

You can download the script here:
Download

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

6 comments

  • 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:
    http://stackoverflow.com/questions/11274040/os-getcwd-vs-os-path-abspathos-path-dirname-file

    So instead of getcwd we need to use
    os.path.abspath(os.path.dirname(__file__))

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

    os.path.join(os.path.abspath(os.path.dirname(__file__)),”output.html”)

    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  

    Hi,

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

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

    Thanks.

Leave a comment