文章目录
- 前言
- 一、filestools库简介
- 二、安装filestools
- 三、查看filestools版本
- 四、图片添加全图水印
- 1.引入库
- 2.添加水印
- 3.效果
- 五、参数调整对比
- 1.水印颜色
- 1.1通过名称设置颜色
- 1.2通过RGB值设置颜色
- 1.3通过十六进制设置颜色
- 2.水印字体的大小
- 3.水印的透明度
- 4.水印直接的间隔
- 5.水印旋转角度
- 总结
前言
大家好,我是空空star,本篇给大家分享一下通过Python的filestools库给图片添加全图水印。
一、filestools库简介
filestools目前包含四个工具包,分别是树形目录显示、文件差异比较、图片加水印和curl请求转python代码。
二、安装filestools
python">pip install filestools
三、查看filestools版本
python">pip show filestools
Name: filestools
Version: 0.1.3
Summary: 仿Linux的tree命令、文件差异比较工具、图片加水印和curl请求转python代码。
Home-page: UNKNOWN
Author: 小小明
License: GPLv3
Requires: cchardet, Pillow, pyperclip, rich
Required-by:
四、图片添加全图水印
1.引入库
python">from watermarker.marker import add_mark
2.添加水印
python">add_mark(file="demo.jpg",
out="out",
mark="空空star",
color="white",
size=30,
opacity=0.3,
angle=45,
space=75)
3.效果
![](https://img-blog.csdnimg.cn/188d8e5ae88e461684c43f69dac47a58.jpeg)
五、参数调整对比
1.水印颜色
默认#8B8B1B
1.1通过名称设置颜色
python"># 通过名称设置颜色-黄色
color = 'yellow'
![](https://img-blog.csdnimg.cn/c0c8e1880b8748858cfbe58c20fceb47.jpeg)
1.2通过RGB值设置颜色
python"># 通过RGB值设置颜色-红色
color = (255, 0, 0)
![](https://img-blog.csdnimg.cn/04ef5f431f67463e8ad400c988377fad.jpeg)
1.3通过十六进制设置颜色
python"># 通过十六进制设置颜色-绿色
color = '#6FE000'
![](https://img-blog.csdnimg.cn/70fc7d50487c4db1bec0b8727bdec98f.jpeg)
2.水印字体的大小
默认30
python"># 左
size=30
python"># 右
size=50
3.水印的透明度
默认0.15
python"># 左
opacity=0.3
python"># 右
opacity=0.6
4.水印直接的间隔
默认75个空格
python"># 左
space=75
python"># 右
space=100
5.水印旋转角度
默认30度
python"># 左
angle=45
python"># 右
angle=-45
总结
查看marker.py,可以发现,这个水印处理就是基于PIL库做的。也能看到水印字体的默认值。
![](https://img-blog.csdnimg.cn/e0b8b070e3b147b08ac66bdbd06b8f71.png)
从以下代码中,也可以看到相关参数的默认值。
python">def add_mark(file, mark, out="output", color="#8B8B1B", size=30, opacity=0.15, space=75, angle=30):
if os.path.isdir(file):
names = os.listdir(file)
for name in names:
image_file = os.path.join(file, name)
add_mark2file(image_file, mark, out, color, size, opacity, space, angle)
else:
add_mark2file(file, mark, out, color, size, opacity, space, angle)