1 Star 2 Fork 0

colin/cwsTools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
testImageWatermark.py 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
colin 提交于 2021-12-18 19:06 +08:00 . update python/testImageWatermark.py.
#!/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")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/colin5063/cws-tools.git
[email protected]:colin5063/cws-tools.git
colin5063
cws-tools
cwsTools
master

搜索帮助