Saturday, May 17, 2008

StdImageField: Improved image field for Django

I'm pleased to announce a new project, django-stdimage, that provides a new image field with many improvements respect to Django's core ImageField.

Features of StdImageField:

  • Saved files have standardized names (using field name and object id)

  • Images can be removed

  • Automatically creates a thumbnail

  • Automatically resizes both image and thumbnail (with optional crop to fit exactly specified size)


Here you've an example of usage:


from django.db import models
from stdimage import StdImageField


class MyClass(models.Model):
  my_image = StdImageField(upload_to='path/to/img', blank=True, \
    size=(640, 480), thumbnail_size=(100, 100, True))


If a file called "uploaded_file.png" is uploaded for object id 34, then result will be:

  • /path/to/img/my_image_34.png (with bigger possible size to fit in a 640x480 area)

  • /path/to/img/my_image_34.thumbnail.png (with a exact size of 100x100, cropping if necessary)


Also it will appear a check-box for deleting when using admin.

15 comments:

  1. this is awesome. thank you very much :D

    ReplyDelete
  2. Thanks Marc!

    It looks great, i'll do some proofs, it could be interesting for our project.

    ReplyDelete
  3. Bosco,

    consider this issue (http://code.google.com/p/django-stdimage/issues/detail?id=7) if you're going to use it for a large application.

    ReplyDelete
  4. Hey,


    Good work on the improvements.
    Personally I use a different approach with dynamic resizing.
    Look for instance at sites such as gravatar. Do not create thumbnails but use something like this:

    my_image_wxh.jpg for example flower_80x80.jpg to request a 80 by 80 image of the base image flower.jpg

    once implemented properly this system really works very flexible :)
    Especially since you often find yourself using different dimensions throughout your website.

    ReplyDelete
  5. Thierry,

    I know this approach, used for example in sorl, but I wanted something more controled. I'll use it for corporate websites, and may be because of it I don't need something so flexible.

    What I'll probably do is allow more than one thumbnail, but always specified in the model fields, to have them more controlled.

    The problem generating thumbnails automatically from a request is that a very simple program could crash your server in minutes, requesting hundreds of thumbnails in few seconds. Specifying the size in templates is better, but I don't like the idea of specifying thumbnail size there (unorganized for me).

    ReplyDelete
  6. this is quite useful, thanks a lot

    ReplyDelete
  7. Marc,
    I will stress test it and i'll tell you the outputs. However, for the first stages of the project I will need images only for the user's avatar and the editor's tool, so performance won't be an issue.

    I also have done a small change in your widgets.py file, in order to show the thumbnail of the image instead of the path of the image. Have a look at my blog http://bcurtu.com/?p=385

    Cheers!

    ReplyDelete
  8. Thanks Marc, great work. Please consider adding the quality option to your save statements(quality=95) for sites where quality prevails size. It make a big improvement when resizing HQ pictures for photogalleries. Please also consider automatic unsharp masking using the PIL_usm module for the thumbnails. Makes a lot of difference in clarity of the TN

    ReplyDelete
  9. Just for the sake of clarification: sorl does not generate thumbnails based on requests, but based on templates, so is not affected by the problems outlined in Marcs reply above (I don't know if that's what Marc meant, but it would be easy to read that into it). Anyhow, this looks interesting, I'll have to take it for a spin!

    ReplyDelete
  10. Vidja, I thought on adding quality when saving pictures, and also on letting generate more than one thumbnail, but I'll implement in future versions. I never heard about the masking methods you explain, but you can be sure that I'll consider it in future versions of this packages (and probably I'll contact you for advice).

    Emil, not sure on how sorl works, but I can figure it out that it doesn't have the security problems that I mentioned. Those were refered to the described way to deal with images.

    ReplyDelete
  11. it's probably worth mentioning that this needs tehe newforms-admin branch to work.... ;)

    adeu,

    - bram

    ReplyDelete
  12. hi , i'd have some problems with the version of django trunk and i'd tried to fix them but there is a problem that i can't solve:

    "Signal receivers must accept keyword arguments (**kwargs)."
    AssertionError: Signal receivers must accept keyword arguments (**kwargs)

    it's in the fields.py file
    can you help me with this?

    gracias desde chile...

    ReplyDelete
  13. sorry for my bad english :)

    ReplyDelete
  14. Hi Oscar,

    I already have a ticket for it at http://code.google.com/p/django-stdimage/issues/detail?id=10

    but I hadn't time to fix it. I hope doing it soon, or at least just after 1.0 beta is reached.

    ReplyDelete
  15. Your code works well and generates thumbnails nicely,
    however when trying to display them from my templates it only seems to work for the main image (the thumbnail will not display).

    I try using but this does not work

    So I tried using {{ object.image_thumbnail.url }} but this outputs nothing

    {{ object.image.url }} does work correctly however...

    what am I missing here?

    ReplyDelete