Quick image resizing with python

I typically build automatic resizing into any django models that use ImageField by overriding the save function like so:

def save(self):
super(GalleryImage, self).save() # Call the "real" save() method
if self.image:
# now change the size of the image
path = settings.MEDIA_ROOT + self.image
img1 = PIL.open(path)
img2 = PIL.open(path)
img2.save(self.get_thumbnail_path())

size = 600,600
img1.thumbnail(size, PIL.ANTIALIAS)
img1.save(path)

# create the thumbnail
size = 150, 150
img2.thumbnail(size, PIL.ANTIALIAS)
img2.save(self.get_thumbnail_path())

def get_thumbnail_path(self):
path = settings.MEDIA_ROOT + self.image
return self.convert_path_to_thumbnail(path)

def get_thumbnail_url(self):
path = self.get_image_url()
return self.convert_path_to_thumbnail(path)

def convert_path_to_thumbnail(self, path):

basedir = os.path.dirname(path) + '/'
base, ext = os.path.splitext(os.path.basename(path))

# make thumbnail filename
th_name = base + "_tn"
th_name += ext
return urlparse.urljoin(basedir, th_name)

This ensures that all my images conform to the sizes I need. However, even if the server is automatically resizing the images for me, I don’t want to spend hours uploading huge images only to have them resized. It’s just a waste of bandwidth and time. So, I’ve written a quick python script that resizes all the images in a given directory and places the copies in a new directory called resized:


#! /usr/bin/env python

"""
What:
Resize all the jpg images in a directory
All images will be placed inside a directory called "resized"
This directory must exist
Usage: ./resize.py
"""

import PIL.Image as PIL
import re, os, sys, urlparse

SIZE = 600,600
JPG = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)

def get_new_path(path):
basedir = os.path.dirname(path) + '/resized/'
base, ext = os.path.splitext(os.path.basename(path))
file = base + ext

return urlparse.urljoin(basedir, file)

try:
image_dir = sys.argv[1]
except:
print "You must specify a directory"
exit(0)

files = os.listdir(image_dir)
for file in files:
if JPG.match(file):
f = image_dir.rstrip("/") + "/" + file
print f
img = PIL.open(f)
img.thumbnail(SIZE, PIL.ANTIALIAS)
img.save(get_new_path(f))

Advertisements

2 responses to “Quick image resizing with python

  1. And waht will be when:
    1. I upload image called ‘image_tn.jpg’
    2. I upload image called ‘image.jpg’

  2. Vitality,
    The code simply resizes the image you’ve posted and appends a “_tn” to the basename. So, your original image will remain “image_tn.jpg” while the thumbnail will be “image_tn_tn.jpg”.
    So, get_image_url() will return /path/to/image_tn.jpg and get_thumbnail_url() will return /path/to/image_tn_tn.jpg

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s