I recently worked on the re-design of 2 Degrees, which required a lot of image processing on thumbnails. The thumbnails where to be in a variety of different sizes, all with rounded corners and keylines on a selection of virtually identical off-white backgrounds and gradients. And they all had to work on IE6 *spit* without the transparency hack.

Sorethumb examples

A variety of thumbnails generated by Sore Thumb

A lesser engineer may have told the front-end developer where to stick his rounded corners, but I didn't want see a grown man cry, so I built Sore Thumb, an on-the-fly thumbnail and image processing system for Django.

Sore Thumb uses a declarative method of defining thumbnails, similar to Django's model and form definitions. Here's an example of how to declare a thumbnail processor that produces a 120x100 pixel thumbnail with 10 pixel rounded corners and a dark grey keyline:

from sorethumb.djangothumbnail import DjangoThumbnail
from sorethumb.filters.defaultfilters import ThumbnailFilter
from sorethumb.filters.drawfilters import RoundedCornerFilter

class RoundedCornersEdged(DjangoThumbnail):
    format = 'png'
    filters = [ThumbnailFilter(120, 100),
               RoundedCornerFilter(10, border='#333')]

Once this class has been imported, the thumbnail processor will be available in templates via the sorethumb filter which takes a Django FileField and returns the url to the thumbnail.

For example:

{% load sorethumb %}

<img src="{{ profile.photo|sorethumb:"rounded_corners_edged" }} />

You will also need to add sorethumb to your INSTALLED_APPS for this to work.

That's pretty much all there is to working with Sore Thumb, see these examples for inspiration and the documentation for the details.

You can install sorethumb with easy_install, PIP, or directly from source:

easy_install -U sorethumb

Please let me know what you think of Sore Thumb. I'd rather not spend too much time maintaining it, but since most of the code has been in production for a number of months already, there shouldn't be many changes required. If you have any feature requests or bug-reports, now would be a good time to raise them while I have the time to do the work!

This blog post was posted to It's All Geek to Me on Monday June 14th, 2010 at 5:14PM
 

20 Responses to "Announcing Sore Thumb, a thumbnail and image processing module for Django"

  • June 14th, 2010, 5:42 p.m.

    Looks like a typo there! sorl-thumbnal

  • June 14th, 2010, 6:06 p.m.

    I use django-photologue to achieve a very similar result..

    Your app looks great and well made thought !

  • Rob O'Dwyer
    June 14th, 2010, 9:12 p.m.

    Fantastic!

    I'm so going to use this the next time i need thumbnails on a site…

    Looks like a much nicer way of doing it than the alternatives.

  • June 15th, 2010, 12:47 a.m.

    That's really awesome!

    I was looking for a thumbnail utility and I am so glad you provide one.

  • climbus
    June 15th, 2010, 9:14 a.m.

    Don't use os.path etc. Use storage functions. Your lib isn't usable for other file storages (MogileFS etc)

  • Temnovit
    June 15th, 2010, 1:45 p.m.

    So, this means a thumbnail is generated each time it is requested in the template? Won't this mean a crash of server with a visited site?

  • June 15th, 2010, 1:59 p.m.

    Temnovit, it only generates the thumbnail the first time it is requested. Subsequently it can use the version cached on disk.

  • June 24th, 2010, 10:24 a.m.

    It looks like aggdraw doesn't compile on 64-bit Linux, and that a dependency for sore…

  • June 24th, 2010, 10:30 a.m.

    Piotr,

    There is a version of aggdraw that works on 64-bit linux here:

    http://bitbucket.org/2degrees/aggdraw-64bits/overview [bitbucket.org]

  • Piotr
    June 24th, 2010, 12:59 p.m.

    Thanks

    Now in the djangoexample app I get

    Caught IOError while rendering: No such file or directory: ‘/home/piotr/svn/sorethumb/djangoexample/media/alpha.png’

    media does exist with sorethumb subfolders but there isn't any alpha.png file there.

  • Anselmo
    July 2nd, 2010, 9:03 p.m.

    Wow, this seems to be well thought-out, unlike other similar django apps. Thank you for opensourcing it!

  • Andre LeBlanc
    July 11th, 2010, 5:57 p.m.

    is this using some kind of name mangling to map sorethumb:“rounded_corners_edged” to the RoundedCornersEdged class? that seems a little unnecessary to me and maybe slightly confusing. otherwise this looks like a great package and will likely be my new replacement for sorl-thumbnail. I too would like to see it using django's filestorage system instead of writing straight to the filesystem though. I've got a site already that uses sorl to store thumbnails on a CDN which this wouldn't work for.

  • July 11th, 2010, 6:02 p.m.

    Andre, the name is derived from the class name (camel case converted to lower case with underscores). But, you can set the ‘name’ attribute to set it explicitly.

    Django storage will come eventually. But I will accept patches if anyone does it before I get to it!

  • Cris
    February 5th, 2011, 9:12 p.m.

    Is there any possibility to download Sore Thumb if easy install not working right?

  • February 5th, 2011, 9:29 p.m.

    Cris,

    you can check out a copy with mercurial

    hg clone [url]https://sorethumb.googlecode.com/hg/[/url] sorethumb

    If you don't have Mercurial, that may be why easy_install isn't working for you!

    Will

  • Cris
    February 5th, 2011, 9:47 p.m.

    Thank You. It works when i install Mercurial :)

  • February 22nd, 2011, 4:53 p.m.

    Hi Will - this is fantastic.

    Just had to make one minor change to get it to work with storage:

    In sorethumb/templatetags/sorethumb.py:

    if file_field and file_field.storage:
    image_path = os.path.join(settings.MEDIA_ROOT, file_field.storage.location, str(file_field))
    else:
    image_path = os.path.join(settings.MEDIA_ROOT, str(file_field))

    :-)

  • Daniel Sokolowski
    July 27th, 2011, 5:02 p.m.

    Hi There,

    It appears you have taken down sorethumb download files - will you be putting it back and if so when it might be?

    Thanks

  • July 28th, 2011, 9:46 a.m.

    Daniel,

    Nothing has been taken down, all the files should be available on http://packages.python.org/sorethumb/ [packages.python.org]

  • July 28th, 2011, 9:49 a.m.

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...

My Tweets

Will McGugan

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

Search for Posts
Possibly related posts
Tags
Popular Tags
 
Archives
2013
 
Recent Comments
Men, you are awesome. You are so great. It works so fucking good and it was so easy to install. ...
Men, you are awesome. You are so great. It works so fucking good and it was so easy to install. ...
Sir, can you give me full code.?
Trying to implement this code. What are the requirements? Does it need a certain version of PHP, etc? Do I ...
Hi Will Can't seem to run any pyopengl applications on my Win7 64bit setup. Using python 3.3 but keep getting ...
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan