Advertisement
Guest User

Untitled

a guest
May 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import base64
  2. import cStringIO
  3. import io
  4. import logging
  5.  
  6. from PIL import Image, ImageOps
  7.  
  8.  
  9. class ImageWrapper(object):
  10. def __init__(self, img):
  11. """
  12. :param PIL.Image.Image img:
  13. :return:
  14. """
  15. self.img = img
  16.  
  17. def apply_viewport(self, viewport):
  18. if viewport is None or (not viewport.get("crop", False) and not viewport.get("crop-x", False)):
  19. return
  20.  
  21. (width, height) = self.img.size
  22. self.img = self.img.crop((
  23. 0,
  24. 0,
  25. int(viewport.get("width")) if viewport.get("crop-x", False) else width,
  26. int(viewport.get("height")) if viewport.get("crop", False) else height
  27. ))
  28.  
  29. return self
  30.  
  31. def thumb(self, width, height):
  32. self.img.thumbnail((width, height), Image.ANTIALIAS)
  33.  
  34. return self
  35.  
  36. def apply_background(self):
  37. try:
  38. non_transparent = Image.new('RGBA', self.img.size, (255, 255, 255, 255))
  39. non_transparent.paste(self.img, (0, 0), self.img)
  40.  
  41. self.img = non_transparent
  42. except Exception, e:
  43. logging.error(e)
  44.  
  45. return self
  46.  
  47. def raw(self):
  48. return self.img
  49.  
  50. def save(self, path):
  51. self.img.save(path)
  52.  
  53. def close(self):
  54. self.img.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement