comparison montage/formatters.py @ 8:93af41dcff3a

now creates thumbnails using cropresize package
author k0s <k0scist@gmail.com>
date Fri, 25 Dec 2009 18:27:45 -0500
parents d60a5ffbf4f4
children 73d18f0cf8bb
comparison
equal deleted inserted replaced
7:70ead14c7d43 8:93af41dcff3a
1 """
2 formatter for decoupage
3 """
4
1 import mimetypes 5 import mimetypes
6 import os
7 import Image
8 from cropresize import crop_resize
2 from decoupage.formatters import FormatterBase 9 from decoupage.formatters import FormatterBase
3 10
4 class Images(FormatterBase): 11 class Images(FormatterBase):
5 """display images with thumbnails""" 12 """display images with thumbnails"""
6 13
7 defaults = { 'size': 'x', 14 defaults = { 'size': 'x',
8 'columns': None } 15 'columns': None,
16 'thumb_dir': 'thumbs',
17 'thumb_prefix': 'thumb_'}
9 18
10 def __init__(self, arg): 19 def __init__(self, arg):
11 FormatterBase.__init__(self, arg) 20 FormatterBase.__init__(self, arg)
12 21
13 # get image size for display 22 # get image size for display
14 width, height = [ i.strip() for i in self.size.split('x', 1) ] 23 width, height = [ i.strip() for i in self.size.split('x', 1) ]
15 self.width = width or None 24 self.width = width and int(width) or None
16 self.height = height or None 25 self.height = height and int(height) or None
17 26
18 def __call__(self, request, data): 27 def __call__(self, request, data):
19 28
20 # add width + height data 29 # add width + height data
21 data['width'] = self.width 30 data['width'] = self.width
22 data['height'] = self.height 31 data['height'] = self.height
37 data['columns'] = int(self.columns) 46 data['columns'] = int(self.columns)
38 47
39 # thumbnails 48 # thumbnails
40 if 'thumbnails' not in self.args: 49 if 'thumbnails' not in self.args:
41 return 50 return
51 thumb_dir = self.thumb_dir
52 if not os.path.isabs(thumb_dir):
53 thumb_dir = os.path.join(data['directory'], self.thumb_dir)
54 else:
55 raise NotImplementedError
56 if not os.path.exists(thumb_dir):
57 os.mkdir(thumb_dir)
42 for f in data['files']: 58 for f in data['files']:
43 raise NotImplementedError 59 filepath = os.path.join(data['directory'], f['name'])
44 import pdb; pdb.set_trace() 60 thumbnail = '%s%s' % (self.thumb_prefix, f['name'])
61 thumbnail_file = os.path.join(data['directory'], thumb_dir, thumbnail)
62 create_thumbnail = False
63 if os.path.exists(thumbnail_file):
64
65 # ensure the size is smaller than the specified size
66 try:
67 thumb = Image.open(thumbnail_file)
68 except:
69 continue
70 if self.width and thumb.size[0] > self.width:
71 create_thumbnail = True
72 if self.height and thumb.size[1] > self.height:
73 create_thumbnail = True
74
75 # ensure the original file has not been modified
76 if os.path.getatime(thumbnail_file) < os.path.getatime(filepath):
77 create_thumbnail = True
78 else:
79 # create a new thumbnail
80 create_thumbnail = True
81
82 if create_thumbnail: # do thumbnail creation
83 image = Image.open(filepath)
84
85 try:
86 thumbnail_image = crop_resize(image, (self.width, self.height))
87 except:
88 continue
89 fd = file(thumbnail_file, 'w')
90 thumbnail_image.save(fd)
91 fd.close()
92
93 # fix the path to point to the thumbnail
94 f['path'] = '%s/%s/%s' % (f['path'].rsplit('/', 1)[0],
95 self.thumb_dir,
96 thumbnail)
97