Back to index

plone3  3.1.7
piltransform.py
Go to the documentation of this file.
00001 from Products.PortalTransforms.interfaces import itransform
00002 from StringIO import StringIO
00003 import PIL.Image
00004 
00005 class PILTransforms:
00006     __implements__ = itransform
00007     __name__  = "piltransforms"
00008     def __init__(self, name=None):
00009          if name is not None:
00010             self.__name__ = name
00011 
00012     def name(self):
00013         return self.__name__
00014 
00015     def convert(self, orig, data, **kwargs):
00016         imgio = StringIO()
00017         orig = StringIO(orig)
00018         newwidth = kwargs.get('width',None)
00019         newheight = kwargs.get('height',None)
00020         pil_img = PIL.Image.open(orig)
00021         if(self.format in ['jpeg','ppm']):
00022             pil_img.draft("RGB", pil_img.size)
00023             pil_img = pil_img.convert("RGB")
00024         if(newwidth or newheight):
00025             pil_img.thumbnail((newwidth,newheight),PIL.Image.ANTIALIAS)
00026         pil_img.save(imgio,self.format)
00027         data.setData(imgio.getvalue())
00028         return data
00029 
00030 
00031 
00032 def register():
00033     return PILTransforms()