如果你想保护自己的原创图片,那最好的方式就是为图片添加盲水印,盲水印就是图片有水印但人眼看不出来,需要通过程序才能提取水印,相当于隐形“盖章”,可以用在数据泄露溯源、版权保护等场景。今天分享如何用 Python 为图片添加盲水印。
为图片添加盲水印,自然是搜索相关的工具,通常情况下,我会去 GitHub 输入英文关键字进行搜索,搜索结果如下:
选择 star 数量最多的通常错不了,guofei9987/blind_watermark 试了一下,果然是最好用的,特点:
以下是其使用方法,主要内容:
安装正式版:
pip install blind-watermark
我安装后使用的时候报错:ModuleNotFoundError: No module named 'pywt',此时,可以通过 pip install pywavelets
来解决。
安装开发版本:
git clone git@github.com:guofei9987/blind_watermark.git
cd blind_watermark
pip install .
该工具用到的其他库:
numpy>=1.17.0
opencv-python
setuptools
PyWavelets
添加文本水印:
from blind_watermark import WaterMark
## 设置密码,默认是 1
bwm1 = WaterMark(password_img=1, password_wm=1)
## 读取原始图片
bwm1.read_img('pic/ori_img.jpg')
## 定义水印文本
wm = '@guofei9987 开源万岁!'
## 合并文本并输出新的图片
bwm1.read_wm(wm, mode='str')
bwm1.embed('output/embedded.png')
## 输出结果
len_wm = len(bwm1.wm_bit)
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))
提取文本水印
bwm1 = WaterMark(password_img=1, password_wm=1)
wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str')
print(wm_extract)
添加图片水印
from blind_watermark import WaterMark
bwm1 = WaterMark(password_wm=1, password_img=1)
## 读取原始图片
bwm1.read_img('pic/ori_img.jpg')
## 读取水印图片
bwm1.read_wm('pic/watermark.png')
## 合并
bwm1.embed('output/embedded.png')
提取图片水印
bwm1 = WaterMark(password_wm=1, password_img=1)
# notice that wm_shape is necessary
bwm1.extract(filename='output/embedded.png', wm_shape=(128, 128), out_wm_name='output/extracted.png', )
附各攻击的方法:
(小波转换技术)[https://en.wikipedia.org/wiki/Wavelet_transform]
本文分享了 Python 为图片添加盲水印的方法,如果觉得有帮助,请点赞、在看、转发,关注公众号「Python七号」,每天学习一个小技术。
留言讨论
[1]
旋转攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/旋转攻击.py
[2]
多遮挡攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/多遮挡攻击.py
[3]
横向裁剪攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/横向裁剪攻击.py
[4]
纵向裁剪攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/纵向裁剪攻击.py
[5]
缩放攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/缩放攻击.py
[6]
椒盐击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/椒盐攻击.py
[7]
亮度调高攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/亮度调高攻击.py
[8]
亮度调暗攻击.py: https://github.com/guofei9987/blind_watermark/blob/master/examples/亮度调低攻击.py
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有