You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 line
3.5KB

  1. import cv2
  2. from PIL import Image, ImageDraw, ImageFont
  3. #################################
  4. # 给图片加上水印
  5. #################################
  6. class Water:
  7. def __init__(self):
  8. # 颜色对应http://www.yuangongju.com/color
  9. self.color_dict = {
  10. 'white':(255,255,255,255),
  11. 'black':(0,0,0,255),
  12. 'gray':(205,201,201,255),
  13. 'red':(255,0,0,255),
  14. 'yellow':(255,215,0,255),
  15. 'blue':(0,0,170,255),
  16. 'purple':(205,105,201,255),
  17. 'green':(0,205,0,255)
  18. }
  19. self.position_list = [1,2,3,4]
  20. def one_water(self, image, text, position=1, fontsize=20, fontcolor='black'):
  21. """
  22. 普通照片水印
  23. params:
  24. image:图片
  25. text:水印文字
  26. position:水印位置
  27. 1:左上
  28. 2:右上
  29. 3:右下
  30. 4:左下
  31. fontsize:字体大小
  32. fontcolor:字体颜色
  33. [white, black, gray, red, yellow, blue, purple, green]
  34. """
  35. if position not in self.position_list:
  36. position = 1
  37. h, w = image.size[:2]
  38. keys = self.color_dict.keys()
  39. if fontcolor not in keys:
  40. fontcolor = 'black'
  41. color = self.color_dict[fontcolor]
  42. fnt = ImageFont.truetype('../font/simsun.ttc', fontsize)
  43. im = image.convert('RGBA')
  44. mask = Image.new('RGBA', im.size, (0,0,0,0))
  45. d = ImageDraw.Draw(mask)
  46. size_h, size_w = d.textsize(text, font=fnt)
  47. alpha = 5
  48. if position == 1:
  49. weizhi = (0 + alpha, 0 + alpha)
  50. elif position == 2:
  51. weizhi = (h - size_h - alpha, 0 + alpha)
  52. elif position == 3:
  53. weizhi = (h - size_h - alpha, w - size_w - alpha)
  54. else:
  55. weizhi = (0 + alpha, w - size_w - alpha)
  56. # position 为左上角位置
  57. d.text(weizhi, text, font=fnt, fill=color)
  58. out = Image.alpha_composite(im, mask)
  59. return out
  60. def fill_water(self, image, text, fontsize):
  61. """
  62. 半透明水印,布满整张图,并且自动旋转45°
  63. params:
  64. image:图片
  65. text:文字
  66. fontsize:文字大小
  67. """
  68. font = ImageFont.truetype('../font/simsun.ttc', fontsize)
  69. # 添加背景
  70. new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (255, 255, 255, 255))
  71. new_img.paste(image, image.size)
  72. # 添加水印
  73. font_len = len(text)
  74. rgba_image = new_img.convert('RGBA')
  75. text_overlay = Image.new('RGBA', rgba_image.size, (0, 0, 0, 0))
  76. image_draw = ImageDraw.Draw(text_overlay)
  77. for i in range(0, rgba_image.size[0], font_len*40+100):
  78. for j in range(0, rgba_image.size[1], 200):
  79. # print(f'i:{i}, j:{j}, text:{text}, font:{font}')
  80. image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
  81. text_overlay = text_overlay.rotate(-45)
  82. image_with_text = Image.alpha_composite(rgba_image, text_overlay)
  83. image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
  84. return image_with_text
  85. if __name__ == '__main__':
  86. img = Image.open("../a.jpg")
  87. fontcolor = 'yellow'
  88. water = Water()
  89. text = "hello world"
  90. fill_img = water.fill_water(img, text, fontsize=36)
  91. # 一定要保存为png格式
  92. fill_img.save(u'aa.png')
  93. print('finish')