Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Package transforms :: Module piltransforms
[frames] | no frames]

Source Code for Module python-module-logilab-mtconverter-0.8.4.transforms.piltransforms

 1  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
 2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
 3  # 
 4  # This file is part of logilab-mtconverter. 
 5  # 
 6  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
 7  # under the terms of the GNU Lesser General Public License as published by the 
 8  # Free Software Foundation, either version 2.1 of the License, or (at your 
 9  # option) any later version. 
10  # 
11  # logilab-mtconverter is distributed in the hope that it will be useful, but 
12  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
13  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
14  # for more details. 
15  # 
16  # You should have received a copy of the GNU Lesser General Public License along 
17  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
18  """PIL based transformations for images""" 
19   
20  from StringIO import StringIO 
21   
22  from PIL import Image 
23   
24  from logilab.mtconverter.transform import Transform 
25   
26 -class PILTransform(Transform):
27 28 format = None # override in subclasses (make pylint happy) 29
30 - def _convert(self, trdata):
31 newwidth = trdata.get('width', None) 32 newheight = trdata.get('height', None) 33 pilimg = Image.open(StringIO(trdata.data)) 34 if self.format in ['jpeg', 'ppm']: 35 pilimg.draft("RGB", pilimg.size) 36 pilimg = pilimg.convert("RGB") 37 if newwidth or newheight: 38 pilimg.thumbnail((newwidth, newheight), Image.ANTIALIAS) 39 stream = StringIO() 40 pilimg.save(stream, self.format) 41 return stream.getvalue()
42 43
44 -class image_to_gif(PILTransform):
45 name = "image_to_gif" 46 inputs = ('image/*', ) 47 output = 'image/gif' 48 format = 'gif'
49
50 -class image_to_bmp(PILTransform):
51 name = "image_to_bmp" 52 inputs = ('image/*', ) 53 output = 'image/x-ms-bmp' 54 format = 'bmp'
55
56 -class image_to_jpeg(PILTransform):
57 name = "image_to_jpeg" 58 inputs = ('image/*', ) 59 output = 'image/jpeg' 60 format = 'jpeg'
61
62 -class image_to_pcx(PILTransform):
63 name = "image_to_pcx" 64 inputs = ('image/*', ) 65 output = 'image/pcx' 66 format = 'pcx'
67
68 -class image_to_png(PILTransform):
69 name = "image_to_png" 70 inputs = ('image/*', ) 71 output = 'image/png' 72 format = 'png'
73
74 -class image_to_ppm(PILTransform):
75 name = "image_to_ppm" 76 inputs = ('image/*', ) 77 output = 'image/x-portable-pixmap' 78 format = 'ppm'
79
80 -class image_to_tiff(PILTransform):
81 name = "image_to_tiff" 82 inputs = ('image/*', ) 83 output = 'image/tiff' 84 format = 'tiff'
85 86 transform_classes = [c for c in globals().values() 87 if isinstance(c, type) and issubclass(c, PILTransform) 88 and not c is PILTransform] 89