class ItemPreview models Model image models ImageField upload_to itemp

 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
class ItemPreview(models.Model):
image = models.ImageField(upload_to='itempreviews/originals/%Y%m%d')
thumbnail =
models.ImageField(upload_to='itempreviews/thumbnails/%Y%m%d',
editable=False)
def __str__(self):
return "%d" % self.id
def fit_image(self, path, size):
import PIL
im = PIL.Image.open(path)
im.thumbnail(size)
return im.tostring('jpeg', 'RGB')
def save(self):
if self.image:
import re
image_path = self.get_image_filename()
image_filename =
re.compile(r'.*[\\/](.*)$').match(image_path).group(1)
self.save_thumbnail_file('%s.thumbnail.jpg' % image_filename,
self.fit_image(image_path, settings.ITEM_THUMBNAIL_SIZE),
save=False)
super(ItemPreview, self).save()