暂无描述

watermark_utils.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. try:
  3. import Image
  4. import ImageEnhance
  5. except ImportError:
  6. from PIL import Image, ImageEnhance
  7. def reduce_opacity(im, opacity):
  8. """Returns an image with reduced opacity."""
  9. assert 0 <= opacity <= 1
  10. im = im.convert('RGBA') if im.mode != 'RGBA' else im.copy()
  11. alpha = im.split()[3]
  12. alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  13. im.putalpha(alpha)
  14. return im
  15. def watermark(im, mark, position, opacity=1, maxsize=(0, 0), possize=(0, 0, None, None)):
  16. """ Add watermark to image """
  17. if opacity < 1:
  18. mark = reduce_opacity(mark, opacity)
  19. # if im.mode != 'RGBA':
  20. # im = im.convert('RGBA')
  21. # Resize mark
  22. w, h = int(min(mark.size[0], maxsize[0]) if maxsize[0] else mark.size[0]), int(min(mark.size[1], maxsize[1]) if maxsize[1] else mark.size[1])
  23. mark = mark.resize((w, h))
  24. # Create a transparent layer the size of the image
  25. # Draw the watermark in that layer.
  26. layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
  27. if position == 'tile':
  28. for y in range(0, im.size[1], mark.size[1]):
  29. for x in range(0, im.size[0], mark.size[0]):
  30. layer.paste(mark, (x, y))
  31. elif position == 'scale':
  32. # Scale, but preserve the aspect ratio
  33. ratio = min(float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
  34. w, h = int(mark.size[0] * ratio), int(mark.size[1] * ratio)
  35. w, h = int(min(w, maxsize[0]) if maxsize[0] else w), int(min(h, maxsize[1]) if maxsize[1] else h)
  36. mark = mark.resize((w, h))
  37. possize += (None, ) * (4 - len(possize))
  38. lx, ly, rx, ry = possize
  39. if rx is None or ry is None:
  40. layer.paste(mark, ((im.size[0] - (lx or w)) / 2, (im.size[1] - (ly or h)) / 2))
  41. else:
  42. layer.paste(mark, ((im.size[0] - rx - w), ry))
  43. else:
  44. layer.paste(mark, position)
  45. # Composite the watermark with the layer
  46. return Image.composite(layer, im, layer)
  47. def watermark_wrap(im_path, mark_path, save_path=''):
  48. im, mark = Image.open(im_path), Image.open(mark_path)
  49. # new_im = watermark(im, mark, (50, 50), 0.5)
  50. # new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 505.2), possize=(400, 400))
  51. new_im = watermark(im, mark, position='scale', opacity=1.0, maxsize=(200, 247.1875), possize=(0, 0, 15, 15))
  52. new_im.save(save_path or im_path)
  53. def watermark_test():
  54. im, mark = Image.open('original_CGzC_10a50000c8811190.jpg'), Image.open('paiai_water_mark.png')
  55. watermark(im, mark, position='tile', opacity=0.5, maxsize=(40, 49.4375)).show()
  56. watermark(im, mark, position='scale', opacity=1.0, maxsize=(400, 494.375), possize=(400, 400)).show()
  57. watermark(im, mark, position='scale', opacity=1.0, maxsize=(200, 247.1875), possize=(0, 0, 10, 10)).show()
  58. watermark(im, mark, position=(50, 50), opacity=0.5, maxsize=(40, 49.4375)).show()
  59. if __name__ == '__main__':
  60. # watermark_test()
  61. watermark_wrap('original_CGzC_10a50000c8811190.jpg', 'paiai_water_mark.png')