Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import cStringIO
- import io
- import logging
- from PIL import Image, ImageOps
- class ImageWrapper(object):
- def __init__(self, img):
- """
- :param PIL.Image.Image img:
- :return:
- """
- self.img = img
- def apply_viewport(self, viewport):
- if viewport is None or (not viewport.get("crop", False) and not viewport.get("crop-x", False)):
- return
- (width, height) = self.img.size
- self.img = self.img.crop((
- 0,
- 0,
- int(viewport.get("width")) if viewport.get("crop-x", False) else width,
- int(viewport.get("height")) if viewport.get("crop", False) else height
- ))
- return self
- def thumb(self, width, height):
- self.img.thumbnail((width, height), Image.ANTIALIAS)
- return self
- def apply_background(self):
- try:
- non_transparent = Image.new('RGBA', self.img.size, (255, 255, 255, 255))
- non_transparent.paste(self.img, (0, 0), self.img)
- self.img = non_transparent
- except Exception, e:
- logging.error(e)
- return self
- def raw(self):
- return self.img
- def save(self, path):
- self.img.save(path)
- def close(self):
- self.img.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement