1 # initiated from http://djangosnippets.org/snippets/2145/
7 from django.conf import settings
9 from django.db.models.signals import post_save, pre_delete
10 from django.template import Library
14 def thumbnail(image, size='100x100'):
16 x, y = [int(x) for x in size.split('x')]
17 # defining the filename and the miniature filename
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'):
26 miniature = basename + '__' + size + format
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))]
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]
46 if image.size[0] > image.size[1]: # landscape
47 box[0] = (image.size[0] - box[2]) / 2 # keep the middle
50 box[1] = (image.size[1] - box[3]) / 4 # keep mostly the top
53 image = image.crop(box)
54 image = image.resize([x, y], Image.ANTIALIAS)
56 image.save(miniature_filename, image.format, quality=90, optimize=1)
58 image.save(miniature_filename, image.format, quality=90)
63 register.filter(thumbnail)
66 def clean_thumb(sender, instance, **kwargs):
67 if not hasattr(instance, 'image'):
69 if not instance.image:
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)
78 post_save.connect(clean_thumb)
79 pre_delete.connect(clean_thumb)