class PlacePicture models Model original models ImageField upload_to p

 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
class PlacePicture(models.Model):
original = models.ImageField(upload_to="%s/places_original/" % settings.MEDIA_ROOT, blank=True)
big = models.ImageField(upload_to="%s/places_big/" % settings.MEDIA_ROOT, blank=True)
thumbnail = models.ImageField(upload_to="%s/places_thumbnails/" % settings.MEDIA_ROOT, blank=True)
date = models.DateTimeField()
place = models.ForeignKey(Place)
def create_thumbnail(self, path):
picture = Image.open(path)
x,y = picture.size
min_size = min(x, y)
region = picture.crop((0, 0, min_size, min_size))
size = (180, 180)
region.thumbnail(size, resample=Image.ANTIALIAS)
return region.tostring()
def create_big(self, path):
picture = Image.open(path)
size = (1024, 768)
picture.thumbnail(size, Image.ANTIALIAS)
return picture.tostring()
def save(self):
if self.original:
image_path = self.get_original_filename()
file_name = "%s%s" % (md5.new(str(time.time())).hexdigest(), image_path[image_path.rfind("."):])
self.save_big_file(file_name, self.create_big(image_path), save=False)
self.save_thumbnail_file(file_name, self.create_thumbnail(image_path), save=False)
super(PlacePicture, self).save()