]> git.0d.be Git - panikweb.git/blob - panikweb/paniktags/templatetags/thumbnails.py
add cell renderer for episodes
[panikweb.git] / panikweb / paniktags / templatetags / thumbnails.py
1 # initiated from http://djangosnippets.org/snippets/2145/
2
3 import os
4 import re
5 from PIL import Image
6
7 from django.conf import settings
8
9 from django.db.models.signals import post_save, pre_delete
10 from django.template import Library
11
12 register = Library()
13
14 def thumbnail(image, size='100x100'):
15     # defining the size
16     x, y = [int(x) for x in size.split('x')]
17     # defining the filename and the miniature filename
18     try:
19         filehead, filetail = os.path.split(image.path)
20     except (AttributeError, ValueError):
21         # return transparent pixel if the image doesn't actually exist
22         return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='
23     basename, format = os.path.splitext(filetail)
24     if format.lower() not in ('.jpg', '.jpeg'):
25         format = '.png'
26     miniature = basename + '__' + size + format
27     filename = image.path
28     miniature_filename = os.path.join(filehead, miniature)
29     filehead, filetail = os.path.split(image.url)
30     miniature_url = filehead + '/' + miniature
31     if os.path.exists(miniature_filename) and \
32             os.path.getmtime(filename) > os.path.getmtime(miniature_filename):
33         os.unlink(miniature_filename)
34     # if the image wasn't already resized, resize it
35     if not os.path.exists(miniature_filename) and os.path.exists(filename):
36         image = Image.open(filename)
37         if abs( (1.0*x/y) - (1.0*image.size[0]/image.size[1]) ) > 0.1:
38             # aspect ratio change, crop the image first
39             box = [0, 0, image.size[0], int(image.size[0] * (1.0*y/x))]
40
41             if box[2] > image.size[0]:
42                 box = [int(t*(1.0*image.size[0]/box[2])) for t in box]
43             if box[3] > image.size[1]:
44                 box = [int(t*(1.0*image.size[1]/box[3])) for t in box]
45
46             if image.size[0] > image.size[1]: # landscape
47                 box[0] = (image.size[0] - box[2]) / 2 # keep the middle
48                 box[2] += box[0]
49             else:
50                 box[1] = (image.size[1] - box[3]) / 4 # keep mostly the top
51                 box[3] += box[1]
52
53             image = image.crop(box)
54         image = image.resize([x, y], Image.ANTIALIAS)
55         try:
56             image.save(miniature_filename, image.format, quality=90, optimize=1)
57         except:
58             image.save(miniature_filename, image.format, quality=90)
59
60     return miniature_url
61
62
63 register.filter(thumbnail)
64
65
66 def clean_thumb(sender, instance, **kwargs):
67     if not hasattr(instance, 'image'):
68         return
69     if not instance.image:
70         return
71     name, ext = os.path.splitext(os.path.basename(instance.image.name))
72     exp = '^%s__\d+x\d+x[0-1]{1}\%s' % (name, ext)
73     for file_path in os.listdir(settings.MEDIA_ROOT):
74         if re.search(exp, file_path):
75             os.unlink(settings.MEDIA_ROOT + file_path)
76
77
78 post_save.connect(clean_thumb)
79 pre_delete.connect(clean_thumb)