from cStringIO import StringIO from . import translators, formats def pil_scale(im,arg): from PIL import Image w,h,force = translators.parse_dimension(arg) i = Image.open(im.asPath()) fmt = i.format if w is None: w = i.size[0] if h is None: h = i.size[1] if force: i = i.resize((w,h),Image.ANTIALIAS) else: i.thumbnail((w,h),Image.ANTIALIAS) buf= StringIO() width, height = i.size i.save(buf, format=fmt) im.setData(buf.getvalue(),im.getExtension()) im.metadata()['width'] = width im.metadata()['height'] = height # FILTERS maps a format to a dictionary of filter names to functions that # take an Intermed that refers to something of that format and a filter arg # and sets the Intermed appropriately # The filter will also be in the Intermed's metadata, but doesn't have # to be removed FILTERS = {".svg" : {'d': lambda im,arg:translators.rsvg_convert(im,'.svg')}, } for f in formats.PIL_RASTER_FORMATS: assert f not in FILTERS FILTERS[f] = {'d': pil_scale}