Django Quick Tips #2: Image Thumbnails

May 4, 2007

One of the most common tasks in web development is resizing uploaded images into thumbnails of the original. Today I’ll show you how to accomplish this in Django inside your model.

Here is an example Photo model. Read the inline comments to see what is going on. Before you can run this, PIL must be installed on your system.

And there you have it. Your thumbnail is automatically saved when a Photo object is saved. You can access the original image by using Photo.get_photo_url(), and the thumbnail by Photo.get_thumbnail_url().

Note: This serves as a simple example of thumbnailing an image. Any additional logic can be added quite easily.


Comments

nice work :)

Posted by apollo13

There's a ticket out there to add a thumbnail contrib to Django.
http://code.djangoproject.com/ticket/...

This page discusses some of the options.
http://code.djangoproject.com/wiki/Th...

Posted by Chris Moffitt

Nice work indeed, very helpfull as I am rewriting my php photo website to use python and Django as learning exercise. I would recommend including the PIL_usm library as well to allow for some unsharp mask to the thumbnail. I'm using ImageMagick, but PIL seems to be a lot faster.

Posted by VidJa

I've been using the thumbnail function on djangosnippets ( http://www.djangosnippets.org/snippet... ), which is useful for cases where more than one size of thumb is required.

Posted by Joe Murphy

here's an excellent utility which makes thumbnails using template filter : http://code.google.com/p/django-utils...

Posted by emes

Nice work. I've also used the template filter approach which is nice.

Posted by Michael Trier

I think I screwed something... The thumbnail file gets name image_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________.jpg, which is waaaaay to long and I receive IOError... What's the fault?

Posted by zgoda

Zgoda:

It looks like you're running into a loop somehow. When Django tries to save an image that already exists, it adds an underscore ("_") to the filename. So apparently it's doing that over and over.

Posted by SuperJared

Zgoda, SuperJared:

I had this same problem. The line:

self.save_thumbnail_file(self.get_photo_filename(), '')

is the culprit, which I figured out thanks to a kind soul on #django. When saving the file the entire instance is also saved, which creates an infinite recursive loop. You have to change the line to:

self.save_thumbnail_file(self.get_photo_filename(), '', save=False)

For it to work appropriately (at least in my case).

Cheers,
-Matt

Posted by matt_pdx

Nice tip Jared. I had to make the same change as Matt, but it works a dream.

I like the way you used a second ImageField, so the file is deleted nicely when the object is deleted.

Ross

Posted by Ross Poulton

Nice snippet!

I can't figure out how to handle updates thou. If I update an object via the admin interface, the thumbnail stays the same... any ideas?

Posted by Magnus

Magnus, It'd be as simple as removing the "if not self.thumbnail:" portion. This would recreate the thumbnail every time and object is saved, but for most applications this wouldn't be a significant issue.

Posted by SuperJared

Hey - thanks! It worked first time, no problems at all. Nice post.

Posted by Chris

When updating the object, how would you create the thumbnail again ONLY if a new file has been uploaded and not always?

Posted by keko

Hi, I make it work in Django 1.0. Refer to http://biohackers.net/wiki/Django1.0/...

Posted by yong27

Add your comment

No HTML; Only URLs and line breaks are converted.