blind_watermark icon indicating copy to clipboard operation
blind_watermark copied to clipboard

提取文本水印信息时出现乱码

Open TuoCao opened this issue 2 years ago • 2 comments

您好,我在通过bash使用这个库时出现了问题,希望您能帮我解答,非常感谢。 问题是:当水印信息为“watermark text”时,添加水印和提取水印都正常;但是当我把水印信息改为“this is the watermark text"之类的其它文本时,添加水印正常,但是提取水印时显示水印信息为乱码。 再次向您表示感谢。

TuoCao avatar Jun 07 '22 15:06 TuoCao

我也遇到了,后来发现是用的姿势不对 加水印时输出日志里有个 Put down watermark size: xxx 解水印时,有个参数 --wm_shape 得填成前面的这个输出

lvq410 avatar Jun 22 '22 04:06 lvq410

解析时,填写了 --wm_shape 参数,解析直接加密的原图可以正常解析,但是我截图加密后的图片,在解析,还是会乱码,哪里操作不对吗?

Liar1995 avatar Sep 15 '22 07:09 Liar1995

最好用原版代码来恢复,默认的那个命令行不好用,重点是“要把图片恢复成原始尺寸”,成功率在80%以上:

最重要的是要把图片恢复成原始尺寸后解密:

# recover from attack:
recover_crop(template_file='output/2022-09-30_15-20.png', output_file_name='output/截屏攻击2_还原.png',
             loc=(x1, y1, x2, y2), image_o_shape=image_o_shape)

https://github.com/guofei9987/blind_watermark/blob/master/examples/example_str.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# embed string
import numpy as np
from blind_watermark import WaterMark
from blind_watermark import att
from blind_watermark.recover import estimate_crop_parameters, recover_crop

import cv2

bwm = WaterMark(password_img=77782589, password_wm=7758258)
bwm.read_img('pic/1648799417649ee7d05.png')
wm = '1998-2022 Yafe Co., Limited'
bwm.read_wm(wm, mode='str')
bwm.embed('output/embedded.png')

len_wm = len(bwm.wm_bit)  # 解水印需要用到长度
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))

ori_img_shape = cv2.imread('pic/1648799417649ee7d05.png').shape[:2]  # 抗攻击有时需要知道原图的shape


# %% 解水印
bwm1 = WaterMark(password_img=77782589, password_wm=7758258)


wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str')
print("不攻击的提取结果:", wm_extract)
assert wm == wm_extract, '提取水印和原水印不一致'


# %% 截屏攻击 = 剪切攻击 + 缩放攻击 + 不知道攻击参数
'''
loc_r = ((0.1, 0.1), (0.7, 0.6))
scale = 0.7
_, (x1, y1, x2, y2) = att.cut_att2(input_filename='output/embedded.png', output_file_name='output/截屏攻击2.png',
                                   loc_r=loc_r, scale=scale)
print(f'Crop attack\'s real parameters: x1={x1},y1={y1},x2={x2},y2={y2}')
'''
# estimate crop attack parameters:
(x1, y1, x2, y2), image_o_shape, score, scale_infer = estimate_crop_parameters(original_file='pic/1648799417649ee7d05.png',
                                                                               template_file='output/2022-09-30_15-20.png',
                                                                               scale=(0.5, 2), search_num=200)

print(f'Crop attack\'s estimate parameters: x1={x1},y1={y1},x2={x2},y2={y2}. score={score}')

# recover from attack:
recover_crop(template_file='output/2022-09-30_15-20.png', output_file_name='output/截屏攻击2_还原.png',
             loc=(x1, y1, x2, y2), image_o_shape=image_o_shape)

bwm1 = WaterMark(password_wm=7758258, password_img=77782589)
wm_extract = bwm1.extract('output/截屏攻击2_还原.png', wm_shape=len_wm, mode='str')
print("截屏攻击,不知道攻击参数。提取结果:", wm_extract)
assert wm == wm_extract, '提取水印和原水印不一致'


'''


# %%缩放攻击
#att.resize_att(input_filename='output/embedded.png', output_file_name='output/缩放攻击.png', out_shape=(400, 300))
att.resize_att(input_filename='output/embedded1.png', output_file_name='output/缩放攻击_还原.png',
               out_shape=ori_img_shape[::-1])
# out_shape 是分辨率,需要颠倒一下

bwm1 = WaterMark(password_wm=7758258, password_img=77782589)
wm_extract = bwm1.extract('output/缩放攻击_还原.png', wm_shape=len_wm, mode='str')
print("缩放攻击后的提取结果:", wm_extract)
assert np.all(wm == wm_extract), '提取水印和原水印不一致'
# %%

'''

bhzhu203 avatar Oct 24 '22 03:10 bhzhu203