Login

[UPDATE]Filter to resize a ImageField on demand + RATIO OPTION + CLEANUP

Author:
karlsberg
Posted:
August 12, 2010
Language:
Python
Version:
Not specified
Tags:
template image thumbnail cleanup
Score:
0 (after 0 ratings)

This is the snippet of rafacbd but upgraded... (http://djangosnippets.org/snippets/955/)

Now support keep the aspect ratio or not, changing the size string (x1, x0) this choice use pil.resize or pil.thumbnail.

Remember change the variable CAPS NAMES of the cleanup code.

This cleanup function get the original main image file name and create a regexp similar to "[filename]_[width]x[height]x[ratio].[ext]" and remove all thumbs.

By example if the main image is spain_rulez.jpg the script remove by example spain_rulez_100x100x1.jpg spain_rulez_200x150x0.jpg etc... etc...

 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import os.path
import re
import Image
from django.template import Library

register = Library()

def thumbnail(file, size='100x100x1'):
    # defining the size
    x, y, ratio = [int(x) for x in size.split('x')]
    # defining the filename and the miniature filename
    filehead, filetail = os.path.split(file.path)
    basename, format = os.path.splitext(filetail)
    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):
        image = Image.open(filename)
        if ratio == 0:
            image = image.resize([x, y], Image.ANTIALIAS)
        else:
            image.thumbnail([x, y], Image.ANTIALIAS)        
        try:
            image.save(miniature_filename, image.format, quality=90, optimize=1)
        except:
            image.save(miniature_filename, image.format, quality=90)

    return miniature_url


register.filter(thumbnail)



# CLEAN CODE (CHANGE THE VARIABLES)

from os import listdir, remove
from os.path import basename, splitext
from re import search

from django.db.models.signals import post_save, pre_delete

from eduardomillan import settings


    

def clean_thumb(sender, instance, **kwargs):
    name, ext = splitext(basename(instance.IMAGE_FIELD.name))
    exp = '^%s_\d+x\d+x[0-1]{1}\%s' % (name, ext)
    for file_path in listdir(settings.MEDIA_ROOT):
        if search(exp, file_path):
            remove(settings.MEDIA_ROOT + file_path)



post_save.connect(clean_thumb, sender=FIELD_MODEL)
pre_delete.connect(clean_thumb, sender=FIELD_MODEL)

More like this

  1. Dynamic thumbnail generator by semente 7 years, 2 months ago
  2. ImageWithThumbsField by zenx 6 years, 5 months ago
  3. Effective content caching for mass-load site using redirect feature by nnseva 3 years, 10 months ago
  4. Django filter stack to cleanup WYSIWYG output by jbergantine 3 years, 8 months ago
  5. Admin page without inlines dinamically by davmuz 2 years, 11 months ago

Comments

Please login first before commenting.