代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/12/18 5:51 下午
# @Author : Colin
# @Email : [email protected]
# @File : testImageWatermark.py
# @Software : PyCharm
#
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# 定义字体
font_path = "/Users/colinspace/Library/Fonts/LXGWWenKai-Regular.ttf"
font = ImageFont.truetype(font_path, 20)
# 定义一个水印
# watermark = "Hello Python!"
watermark = "我爱跑步!"
# 定义边距,水印举例原始图片的边距
border = 10
# 先画一个尺寸为200*300背景色为灰色的图片
img = Image.new('RGBA', (300, 200), 'gray')
# img = Image.new('RGBA', (900, 600), 'gray')
# img = Image.new('RGBA', (3000, 2000), 'gray')
text_layer = ImageDraw.Draw(img)
# 计算水印文字的大小,为了更精确的存放水印位置
text_xy = text_layer.textsize(watermark, font=font)
# 注意计算位置,添加水印一般都是 左上角、右上角、右下角、左下角或者中心位置等特殊点;最常见的就是在右下角
# 计算水印在右下角的位置
# watermark_xy = (img.size[0] - text_xy[0] - 10, img.size[1] - text_xy[1] - 10)
# print("原始图片的大小: ", img.size)
# print("水印文字的大小: ", text_xy)
# print("水印文字的位置: ", watermark_xy)
text_layer.text((100, 50), watermark, fill='orange', font=font)
img.show()
img.save("result.png")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。