Filter to resize a ImageField on demand

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import Image
from django.template import Library


register = Library()


def thumbnail(file, size='200x200'):
    # defining the size
    x, y = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    basename, format = file.rsplit('.', 1)
    miniature = basename + '_' + size + '.' +  format
    miniature_filename = os.path.join(settings.MEDIA_ROOT, miniature)
    miniature_url = os.path.join(settings.MEDIA_URL, miniature)
    # if the image wasn't already resized, resize it
    if not os.path.exists(miniature_filename):
        print '>>> debug: resizing the image to the format %s!' % size
        filename = os.path.join(settings.MEDIA_ROOT, file)
        image = Image.open(filename)
        image.thumbnail([x, y]) # generate a 200x200 thumbnail
        image.save(miniature_filename, image.format)
    return miniature_url


register.filter(thumbnail)

Comments

Archatas (on April 19, 2007):

Shouldn't there be something like

thumbnail = register.filter(thumbnail)

at the end of the snippet?

#

alexkoval (on July 9, 2007):

the first thing which I've noticed is that thumbnail was not regenerated when I replaced main image. I suggest fix so thumbnail is removed (and then regenerated) when its outdated. I also had to move up filename= line.

miniature = basename + '_' + size + '.' +  format
filename = os.path.join(settings.MEDIA_ROOT, file)
miniature_filename = os.path.join(settings.MEDIA_ROOT, miniature)
miniature_url = os.path.join(settings.MEDIA_URL, miniature)
if os.path.exists(miniature_filename) and \
       os.path.getmtime(filename)>os.path.getmtime(miniature_filename):
    os.unlink(miniature_filename)

#

alexkoval (on July 9, 2007):

also... Thumbnails are bad quality, I recommend adding anti aliasing when resizing:

    image.thumbnail([x, y],Image.ANTIALIAS) # generate a 200x200 thumbnail

#

javinievas (on October 22, 2007):

For people like me using filestorage (http://code.djangoproject.com/ticket/5361), this is the working modified code.

def thumbnail(file, size='100x150'):
    # defining the size
    x, y = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    basename, format = file.rsplit('.', 1)
    miniature = basename + '_' + size + '.' +  format
    miniature_nomedia = miniature[len(settings.MEDIA_URL):]
    file_nomedia = file[len(settings.MEDIA_URL):]
    filename = os.path.join(settings.MEDIA_ROOT, file_nomedia)
    miniature_filename = os.path.join(settings.MEDIA_ROOT, miniature_nomedia)
    miniature_url = 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):
        image = Image.open(filename)
        image.thumbnail([x, y], Image.ANTIALIAS) # generate a 200x200 thumbnail
        image.save(miniature_filename, image.format)
    return miniature_url

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.