文章最后更新时间为:2022 年 04 月 23 日 14:50:03 Loading... ```python from PIL import Image, ImageDraw, ImageFont def add_water(image, text, fontsize): # 字体文件 font = ImageFont.truetype('1641789485961483.otf', 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 + 300): for j in range(0, rgba_image.size[1], 300): # 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__': pic = '1.png' # 需要加水印的图片 text = "我是水印" # 水印文字 fontsize = 32 # 文字大小 img = Image.open(pic) fill_img = add_water(img, text, fontsize) # 一定要保存为png格式 fill_img.save(u'2.png') ``` <div class="tip inlineBlock info"> 自行修改注释地方 </div> ![屏幕截图 2022-02-27 103358.jpg][1] [1]: https://pan.roaing.com/uploads/2022/02/57425219.png Last modification:April 23, 2022 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 如果觉得我的文章对你有用,请随意赞赏