使用Python中PIL库给图片添加文本水印
作者:空空star 时间:2021-09-07 19:09:52
前言
大家好,本篇给大家分享一下通过Python的PIL库给图片添加文本水印。
一、PIL是什么?
PIL是Python Imaging Library的缩写,它是Python语言中常用的图像处理库之一。它提供了丰富的图像处理功能,包括打开、保存、裁剪、旋转、缩放等操作,并支持多种图像格式。
二、安装PIL
pip install pillow
三、查看PIL版本
pip show pillow
Name: Pillow
Version: 9.4.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (PIL Fork Author)
Author-email: aclark@python-pillow.org
License: HPND
Requires:
Required-by: image, imageio, matplotlib, pytesseract, wordcloud
四、使用PIL库给图片添加文本水印
1.引入库
from PIL import Image, ImageDraw, ImageFont
2.打开图片文件
local = '/Users/kkstar/Downloads/video/pic/'
image = Image.open(local+"demo.jpg")
3.新建一个Draw对象
draw = ImageDraw.Draw(image)
4.设置水印文字、字体、大小
text = '@空空star'
font = ImageFont.truetype('STHeitiMedium.ttc', size=80)
5.设置水印颜色
5.1通过名称设置颜色
# 通过名称设置颜色-黄色
color = 'yellow'
5.2通过RGB值设置颜色
# 通过RGB值设置颜色-红色
color = (255, 0, 0)
5.3通过RGBA值设置颜色
# 通过RGBA值设置颜色-白色
color = (255,255,255,0)
5.4通过十六进制设置颜色
# 通过十六进制设置颜色-绿色
color = '#6FE000'
6.获取水印文字的尺寸
text_width, text_height = draw.textsize(text, font)
7.设置水印位置
7.1左上
x = 30
y = 30
7.2右下
x = image.width-text_width-30
y = image.height-text_height-30
其他位置调整x、y的值即可。这个30是我这样设置的,你也可以根据自己的喜好来调整。
8.添加水印
draw.text((x, y), text, font=font, fill=color)
9.保存图片
image.save(local+'image_with_watermark.jpg')
来源:https://blog.csdn.net/weixin_38093452/article/details/130204442
标签:pil库,图片,文字水印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中set()函数简介及实例解析
2022-05-15 17:12:24
加快Firefox 3.5启动速度的方法
2009-07-16 15:22:00
ASP导出Excel数据的四种办法
2007-08-26 18:32:00
Django 项目重命名的实现步骤解析
2021-01-13 14:05:49
![](https://img.aspxhome.com/file/2023/5/68315_0s.png)
python3中的函数与参数及空值问题
2023-08-10 00:26:18
![](https://img.aspxhome.com/file/2023/8/107228_0s.png)
列表模块是否需要标题
2009-06-25 14:11:00
![](https://img.aspxhome.com/file/UploadPic/20096/25/g2009620101624-60s.jpg)
python实现自动化脚本编写
2023-11-13 14:58:14
pycharm新建一个python工程步骤
2023-08-22 17:42:54
![](https://img.aspxhome.com/file/2023/5/62565_0s.jpg)
6个卓越Web设计细节
2010-03-29 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20103/29/01-23s.jpg)
Mysql日期和时间函数大全
2011-03-08 09:52:00
一个asp版的xheditor上传图片服务器端文件
2009-12-21 14:18:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/200982514518-20s.jpg)
Python+Django搭建自己的blog网站
2022-02-24 16:32:45
![](https://img.aspxhome.com/file/2023/8/92338_0s.png)
position两三事
2009-02-16 15:23:00
pyspark操作hive分区表及.gz.parquet和part-00000文件压缩问题
2022-02-07 14:35:53
![](https://img.aspxhome.com/file/2023/0/79580_0s.png)
使用python实现飞机大战游戏
2021-05-11 12:02:16
![](https://img.aspxhome.com/file/2023/1/97111_0s.jpg)
Matplotlib使用Cursor实现UI定位的示例代码
2022-04-18 15:27:13
![](https://img.aspxhome.com/file/2023/4/90534_0s.png)
Python编程学习之如何判断3个数的大小
2022-03-06 18:10:04
找Python安装目录,设置环境路径以及在命令行运行python脚本实例
2022-06-01 23:28:56
![](https://img.aspxhome.com/file/2023/6/96976_0s.jpg)
人民币的符号的正确表示法?一杠?两杠?¥还是¥呢?
2010-03-24 12:21:00
PHP实现从上往下打印二叉树的方法
2023-06-25 17:40:08