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:
# 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.
from PIL import Image
d = os.path.dirname(f)
if not os.path.exists(d):
# 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=" " /></a></li>n’%(output_thumb_folder,fname,output_big_folder, fname)
image = Image.open(source_folder + fname)
if image.size <= 640 and image.size <= 400:
image.save(current_folder + output_big_folder + "/" + fname, "JPEG", quality = 92)
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)
thumb_box = ( thumb_size/2 – image.size/2 , thumb_size/2 – image.size/2)
thumbnail = Image.new ("RGB", thumb_size)
thumbnail.save(current_folder + output_thumb_folder + "/" + fname, "JPEG", quality = 90)
if __name__ == ‘__main__’:
You can download the script here:
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!
It should be in the folder where you execute the script… You can also replace the line :
And that should give you the output in the window instead of the file.
That was a quick response, you are great, thanks! Will be trying the print script too, thank you SOO much!
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?
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.
How can I change the size of the output pics?
It seems, they have always the same size, whatever dimensions I set…