views:

59

answers:

1

Hello, this seems like quite an easy problem but I can't figure out what is going on here. Basically, what I'd like to do is create two different thumbnails from one image on a Django model. What ends up happening is that it seems to be looping and recreating the same image (while appending an underscore to it each time) until it throws up an error that the filename is to big. So, you end up something like:

OSError: [Errno 36] File name too long: 'someimg________________etc.jpg'

Here is the code (the save method is on the Artist model):

def save(self, *args, **kwargs):

  if self.image:
    iname = os.path.split(self.image.name)[-1]
    fname, ext = os.path.splitext(iname)
    tlname, tsname = fname + '_thumb_l' + ext, fname + '_thumb_s' + ext
    self.thumb_large.save(tlname, make_thumb(self.image, size=(250,250)))
    self.thumb_small.save(tsname, make_thumb(self.image, size=(100,100)))
  super(Artist, self).save(*args, **kwargs)

 def make_thumb(infile, size=(100,100)):
   infile.seek(0)
   image = Image.open(infile)

   if image.mode not in ('L', 'RGB'):
     image.convert('RGB')

   image.thumbnail(size, Image.ANTIALIAS)

   temp = StringIO()
   image.save(temp, 'png')

   return ContentFile(temp.getvalue())

I didn't show imports for the sake of brevity. Assume there are two ImageFields on the Artist model: thumb_large, and thumb_small.

The way I am testing if this works is, in the shell:

artist = Artist.objects.get(id=1)
artist.save() 
#error here after a little wait (until I assume it generates enough images that the OSError gets raised)

If this isn't the correct way to do it, I'd appreciate any feedback. Thanks!

+3  A: 

Generally I like to give thumbnailing capabilities to the template author as much as possible. That way they can adjust the size of the things in the template. Whereas building it into the business logic layer is more fixed. You might have a reason though.

This template filter should generate the file on first load then load the file on future loads. Its borrowed from some blog a long time back although I think I added the center crop feature. There are most likely others with even more features.

{% load thumbnailer %}
...
<img src="{{someimage|thumbnail_crop:'200x200'}}" />

file appname/templatetags/thumbnailer.py

import os
import Image
from django.template import Library

register.filter(thumbnail)
from settings import MEDIA_ROOT, MEDIA_URL

def thumbnail_crop(file, size='104x104', noimage=''):
    # defining the size
    x, y = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    try:
        filehead, filetail = os.path.split(file.path)
    except:
        return '' # '/media/img/noimage.jpg'

    basename, format = os.path.splitext(filetail)
    #quick fix for format
    if format.lower() =='.gif':
        return (filehead + '/' + filetail).replace(MEDIA_ROOT, MEDIA_URL)

    miniature = basename + '_' + size + format
    filename = file.path
    miniature_filename = os.path.join(filehead, miniature)
    filehead, filetail = os.path.split(file.url)
    miniature_url = filehead + '/' + miniature
    if os.path.exists(miniature_filename) and os.path.getmtime(filename)>os.path.getmtime(miniature_filename):
        os.unlink(miniature_filename)
    # if the image wasn't already resized, resize it
    if not os.path.exists(miniature_filename):
        try:
            image = Image.open(filename)
        except:
            return noimage

        src_width, src_height = image.size
        src_ratio = float(src_width) / float(src_height)
        dst_width, dst_height = x, y
        dst_ratio = float(dst_width) / float(dst_height)

        if dst_ratio < src_ratio:
            crop_height = src_height
            crop_width = crop_height * dst_ratio
            x_offset = float(src_width - crop_width) / 2
            y_offset = 0
        else:
            crop_width = src_width
            crop_height = crop_width / dst_ratio
            x_offset = 0
            y_offset = float(src_height - crop_height) / 3
        image = image.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height)))
        image = image.resize((dst_width, dst_height), Image.ANTIALIAS)
        try:
            image.save(miniature_filename, image.format, quality=90, optimize=1)
        except:
            try:
                image.save(miniature_filename, image.format, quality=90)
            except:
                return '' #'/media/img/noimage.jpg'

    return miniature_url

register.filter(thumbnail_crop)
michael
+1 for showing me something I'd not thought of doing. I tend to do this sort of thing in the model layer, but I'm now thinking your template approach might make more sense (though if you've got a relatively high-traffic site you'll want to cache that like crazy, since checking file-modification times is not free).
Dominic Rodger
@dominic I would agree with the caching, though it should be easy enough to just cache the modified date, and invalidate it manually whenever the user makes a change (which hopefully isn't very often, for your sake ;)
Jiaaro
Good point. The getmtime clause could probably be removed or altered. All that is needed is a cleanup routine when the image field is modified. I think on my system if the image field file is altered it will resulit in a new filename (and miniature_filename) anyway.
michael