Resizing Image on upload in Django

by Dave Dash 21Feb09

I had trouble wrapping my head around Django ORM's handling of Images.

The first hurdle was realizing that there is no easy way to store images in the database. I couldn't be too upset with that, it was a flawed concept at best. I wrote a dump script to fix this, and converted my app to read from disk. One of those "good" problems.

When it came to uploading new content... I seemed to hit every roadblock. So I thought this guide might be of use for people (other than future-me). What I want to do is upload an image, resize it per the requirements of my app, and save the resized image.

The Template

The template was fairly easy, yet I forgot to specify the enctype attribute. Here's what you need:

Resizing an image

Resizing an image was straightforward. The Python Imaging Library is very easy to work with, and very similar to the PHP GD Library. There was one caveat...

I'm using OS X and didn't have libjpeg installed. The recommended way was to install it via fink.

Generating a filename

I like using MD5 to generate filenames for images because MD5 almost guarantees uniqueness ("unique enough").

Saving the file via the model

The model is supposed to put the files in the right spot and populate the meta data.

You can supposedly do this via:

That will also take care of my_object.save(). The trick is that content needs to be a django.core.files.File object or things will never work. Further more, you can't use a StringIO for your File object. A true file object is best. Hence rewriting the JPEG:

Note: I also had to reopen the imagefile.

All together now

I'm sure there's room to optimize this flow. But this gets the job done. I do wish I could have used a StringIO instead of an actual file to save the image. Writing to disk twice seems silly.


Where am I?

This is a single entry in the weblog.

"Resizing Image on upload in Django" is filed under spindrop. It was published in February 2009.

February 2009
M T W T F S S
« Dec   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  

Tags

&& && &&

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

2 Responses to “Resizing Image on upload in Django”


  1. 1 DB Posted August 23rd, 2009 - 9:17 pm

    What’s “scaled_dimensions”? I get the following?

    global name ’scale_dimensions’ is not defined

  2. 2 Sergio Bastias Posted August 26th, 2009 - 8:30 am

    There is a way!

    thumbImage = Image.open(StringIO(contentString)) thumbImage = thumbImage.resize((100,100))

    thumbfile = StringIO() thumbImage.save(thumbfile, “JPEG”)

    thumbcontent = ContentFile(thumbfile.getvalue())

    newphoto.thumb.save(filename, thumbcontent) new_photo.save()

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.