import os
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
# 输入图片路径、水印文字、水印位置和透明度
path = input('请输入要添加水印的图片所在路径:')
text = input('请输入水印文字:')
position_flag = int(input('请输入水印位置(0:左上角,1:左下角,2:右上角,3:右下角,4:居中):'))
alpha_value = float(input('请输入水印透明度(范围在0—1之间的1位小数):'))
# 设置使用的字体
font = ImageFont.truetype('simkai.ttf', 24)
def text_mark(image_path):
"""在图片上添加文字水印"""
try:
im = Image.open(image_path).convert('RGBA')
new_img = Image.new('RGBA', im.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(new_img)
img_width, img_height = im.size
txt_width = font.getsize(text)[0]
txt_height = font.getsize(text)[1]
# 设置水印文字的位置
positions = {
0: (0, 0), # 左上角
1: (0, img_height - txt_height), # 左下角
2: (img_width - txt_width, 0), # 右上角
3: (img_width - txt_width, img_height - txt_height), # 右下角
4: (img_width / 2 - txt_width / 2, img_height / 2 - txt_height / 2) # 居中
}
position = positions[position_flag]
# 绘制文字水印
image_draw.text(position, text, font=font, fill=(255, 0, 0, int(255 * alpha_value)))
# 合并图层并保存图片
combined = Image.alpha_composite(im, new_img)
combined.save(image_path, 'PNG')
except Exception as e:
print(e)
try:
files = os.listdir(path)
for file in files:
file_path = os.path.join(path, file)
if os.path.isfile(file_path) and file_path.endswith('.png'):
text_mark(file_path)
print('批量添加水印完成')
except Exception as e:
print('请输入一个有效路径……')
没有合适的资源?快使用搜索试试~ 我知道了~
使用python给图片加水印的简单案例
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共10个文件
xml:5个
ttf:1个
gitignore:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 0 下载量 9 浏览量
2024-04-18
15:41:41
上传
评论
收藏 18.02MB ZIP 举报
温馨提示
特点: 批量处理: 一次操作可对多张图片添加水印,节省时间和精力。 自定义水印: 用户可以根据自己的喜好设置水印的文字、字体大小和颜色。 可调透明度: 调整水印的不透明度,确保其在不压倒原始图像的情况下良好融合。 灵活的定位: 提供五个预设位置(左上角、左下角、右上角、右下角、中心),确保水印精准放置。 针对PNG格式设计: 专为处理PNG图片设计,确保输出高质量的带透明背景的图像。 易于使用: 简单的提示引导用户完成过程,即使是技术知识有限的用户也能轻松操作。 系统要求: 操作系统: 支持安装Python的任何操作系统,如Windows、macOS、Linux。 Python版本: 推荐使用Python 3.6或更高版本。 外部库依赖: 需要安装Pillow库(pip install Pillow)进行图像处理。 字体文件: 需要simkai.ttf字体文件,或者用户可以修改脚本以使用系统中已有的其他字体文件。
资源推荐
资源详情
资源评论
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 10 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/63a5e228ee3f4008b4c92bd85a7f72f5_qq_42452134.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
E寻数据
- 粉丝: 5639
- 资源: 40
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)