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 | def __getattr__(self, name):
match = re.match(GET_THUMB_PATTERN, name)
if match is None:
raise AttributeError, name
width, height, method = match.groups()
size = int(width), int(height)
def get_photo_thumbnail_filename():
file, ext = path.splitext(self.get_photo_filename())
return file + '_%sx%s' % size + ext
def get_photo_thumbnail_url():
url, ext = path.splitext(self.get_photo_url())
return url + '_%sx%s' % size + ext
thumbnail = get_photo_thumbnail_filename()
if not path.exists(thumbnail):
img = Image.open(self.get_photo_filename())
img.thumbnail(size, Image.ANTIALIAS)
img.save(thumbnail)
if method == "url":
return get_photo_thumbnail_url
else:
return get_photo_thumbnail_filename
|
Comments
Fixed a bug:
#
Nice, I can see this coming in very handy and it's a lot more straightforward than I expected it to be (a testament to the very powerful features and libraries of Python).
#
Is it possible to make it delete the generated images if the parent field is edited or deleted?
#
Andrew: check out sorl-thumbnail -- it's a drop-in django thumbnail app; it automatically updates cached thumbnails if they're older than their source images (among other things).
#