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