Quick Thumbnails in Django

I normally like to write code myself instead of installing some large script just to do one task for me. There were a few scripts out there that could create thumbnails, but I wanted something simple because I knew that I wouldn’t use most of those features. Additionally, I wanted an excuse to use the Python Image Library with Django 1.0 and to learn how to take an uploaded picture and create a few thumbnails of them.

After searching for a while I was able to piece some things together to get something working. In my model I added these two functions.

def thumbnailed(self, file, width, height):
    try:
        size = width, height
        tmp_file = StringIO()
        im = Image.open(StringIO(file.read()))
        im.thumbnail(size, Image.ANTIALIAS)
        im.save(tmp_file, im.format)
    except IOError:
        return None
 
    return ContentFile(tmp_file.getvalue())

Using StringIO, I was able to create a temporary file in memory to hold the thumbnail data and return it where it would later be passed to Django. I was trying to create 3 thumbnails which presented another problem: Django was obliterating the uploaded file and the data in my temp files when I was saving. So I figured out how to get around that, but there may be better ways.

def create_thumbnails(self, file):
    pic = self.thumbnailed(file, 160, 400)
    if pic is not None:
        self.picture.save(file.name, pic, save=False)
 
    # file is annihilated by django, use self . picture now
 
    thumb = self.thumbnailed(self.picture, 288, 96)
    if thumb is not None:
        self.thumbnail.save(file.name, thumb, save=False)
 
    tiny = self.thumbnailed(self.thumbnail, 144, 48)
    if tiny is not None:
        self.tiny_image.save(file.name, tiny, save=False)

The model has picture, thumbnail, and tiny_image as ImageFields and create_thumbnails, which was originally called from the view and passed the uploaded file from request.FILES.

There was a lot of trial and error trying to get this together, so I hope it helps someone skp past that.

Leave a Reply

Newer Entries

Recent Comments

  1. Justin Blake: Nice work on Convertly! I also like what you said about learning new technologies. That “we...