考虑的问题:给图片加水印,加什么文字、用什么字体、水印的颜色、水印的大小、水印的位置
本文将水印分为两种类型:
(1)可以指定文字、位置、大小、颜色的水印
(2)背景水印,在背景里加上半透明的水印,可以指定文字、大小
1、水印的颜色
本文暂时用了8种颜色,需要增加颜色的可以自己增加,分别是白色、黑色、灰色、红色、黄色、蓝色、紫色、绿色,对应的颜色的:
white | (255,255,255,255) |
black | (0,0,0,255) |
gray | (205,201,201,255) |
red | (255,0,0,255) |
yellow | (255,215,0,255) |
blue | (0,0,170,255) |
purple | (205,105,201,255) |
green | (0,205,0,255) |
2、水印的位置及大小
水印的大小由文字的大小决定,先获取水印的大小,才能在图片中确定位置(防止水印超出坐标),用PIL的ImageDraw.Draw.textsize()函数来取水印的大小。一般水印的位置在图片的四个角落,分别是左上(1)、右上(2)、右下(3)、左下(4),括号中为编码
3、效果展示(来几张刘天仙的绝美照片)
扫描二维码关注公众号,回复:
14693955 查看本文章
4、代码
import cv2
from PIL import Image, ImageDraw, ImageFont
#################################
# 给图片加上水印
#################################
class Water:
def __init__(self):
# 颜色对应http://www.yuangongju.com/color
self.color_dict = {
'white':(255,255,255,255),
'black':(0,0,0,255),
'gray':(205,201,201,255),
'red':(255,0,0,255),
'yellow':(255,215,0,255),
'blue':(0,0,170,255),
'purple':(205,105,201,255),
'green':(0,205,0,255)
}
self.position_list = [1,2,3,4]
def one_water(self, image, text, position=1, fontsize=20, fontcolor='black'):
"""
普通照片水印
params:
image:图片
text:水印文字
position:水印位置
1:左上
2:右上
3:右下
4:左下
fontsize:字体大小
fontcolor:字体颜色
[white, black, gray, red, yellow, blue, purple, green]
"""
if position not in self.position_list:
position = 1
h, w = image.size[:2]
keys = self.color_dict.keys()
if fontcolor not in keys:
fontcolor = 'black'
color = self.color_dict[fontcolor]
fnt = ImageFont.truetype('./fonts/FZYTK.TTF', fontsize)
im = image.convert('RGBA')
mask = Image.new('RGBA', im.size, (0,0,0,0))
d = ImageDraw.Draw(mask)
size_h, size_w = d.textsize(text, font=fnt)
alpha = 5
if position == 1:
weizhi = (0 + alpha, 0 + alpha)
elif position == 2:
weizhi = (h - size_h - alpha, 0 + alpha)
elif position == 3:
weizhi = (h - size_h - alpha, w - size_w - alpha)
else:
weizhi = (0 + alpha, w - size_w - alpha)
# position 为左上角位置
d.text(weizhi, text, font=fnt, fill=color)
out = Image.alpha_composite(im, mask)
return out
def fill_water(self, image, text, fontsize):
"""
半透明水印,布满整张图,并且自动旋转45°
params:
image:图片
text:文字
fontsize:文字大小
"""
font = ImageFont.truetype('./fonts/FZYTK.TTF', fontsize)
# 添加背景
new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (255, 255, 255, 255))
new_img.paste(image, image.size)
# 添加水印
font_len = len(text)
rgba_image = new_img.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (0, 0, 0, 0))
image_draw = ImageDraw.Draw(text_overlay)
for i in range(0, rgba_image.size[0], font_len*40+100):
for j in range(0, rgba_image.size[1], 200):
# print(f'i:{i}, j:{j}, text:{text}, font:{font}')
image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
text_overlay = text_overlay.rotate(-45)
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
return image_with_text
if __name__ == '__main__':
img = Image.open("imgs/water/1.jpg")
fontcolor = 'yellow'
water = Water()
text = "hello world"
fill_img = water.fill_water(img, text, fontsize=36)
# 一定要保存为png格式
fill_img.save(u'imgs/water/fill_img.png')
print('finish')