DISQUS

Code Spatter: Quick Thumbnails in Django

  • Arien · 1 year ago
    I am doing a small project in Django (I'm a Python newbie, learning as I go). I initially wanted to deal with thumbnails, but finally skipped it, since was not really relevant to the project. But I'll definitely try this when I start working on improvements.

    Thanks for sharing! :)
  • Greg Allard · 1 year ago
    I hope it helps you out when you need it. I just started learning django and python when trying to figure this out and that is probably why it took me a while.
  • Batiste · 1 year ago
    Take a look at this snippets:

    http://www.djangosnippets.org/snippets/192/

    Thumbnails are generated in a lazy way. I like the idea.
  • Greg Allard · 1 year ago
    I'll probably stick with my method since it gets the processing done initially and avoids the case where many images all need to be resized on a single page load.

    Although, having a filter to do that will be convenient if you change your mind on the thumbnail size.
  • simeon · 1 year ago
    I'm all for writing a little code to understand a library like PIL. It's worth noting sorl-thumbnail (http://code.google.com/p/sorl-thumbnail/), however, as a django app that solves just this problem... It can do a lot of things but at the simplest level you can just use a template tag to specify the thumbnail size and thumbnails are generated and cached for future use. A very nice app...
  • Greg Allard · 1 year ago
    Thanks for the link. I will probably check that out since I know I will want more features at some point.
  • dobrych · 1 year ago
    Thank you for the StringIO tip, it saved my time :-)
  • Greg Allard · 1 year ago
    I'm glad this helped. That was the hardest part to figure out. I didn't want to create a temporary file on the file system and StringIO wasn't the first thing I tried.
  • Greg Allard · 7 months ago
    I updated this post. I was thinking the uploaded file was deleted after using it, but I just needed to reset the file. Django's InMemoryUploadedFile uses StringIO. Doing file.seek(0)
    will reset the StringIO file to be ready to create another thumbnail.
  • wkoorts · 5 months ago
    Great post, thanks. I especially liked learning about StringIO as I didn't know about it before now.