thumbnail generator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def create_thumb(sender, instance, signal, *args, **kwargs):
if path.exists(instance.thumb_file):
return
image = PIL.open(instance.image_file)
image.thumbnail((256, 192), PIL.ANTIALIAS)
image.save(instance.thumb_file)
def delete_thumb(sender, instance, signal, *args, **kwargs):
if path.exists(instance.thumb_file):
remove(instance.thumb_file)
print 'delete', instance.pk, image
dispatcher.connect(create_thumb, signal=models.signals.post_save, sender=Image)
dispatcher.connect(delete_thumb, signal=models.signals.post_delete, sender=Image)