def mod_3(): attempts = 0 success = False while attempts < 3000 and not success: try: print("图片水印解密模式:") print("请将你需要解密文件放置于jiemi文件夹下") jiemi_name = "abc" jiemi_name = get_str(jiemi_name,"输入你想要解密的文件名,要带后缀,例如:abc.png/abc.jpg|不支持带汉字文件名") jiemi_name = 'jiemi/'+jiemi_name print("正在寻找"+jiemi_name+"待解密文件") password_wm = 123 password_img = 123 password_wm = get_value(password_wm,"请输入水印加密密码,只能是数字组合") password_img = get_value(password_img,"请输入原图加密密码,只能是数字组合") bwm1 = WaterMark(password_wm, password_img) print("请输入你曾经对这张图加的水印尺寸:例如如果你的水印是640 x 480的,则它的长是640,宽是480,如果是使用默认水印,请两次输入100 \n") x = 0 y = 0 x = get_value(x,"请输入长:") y = get_value(y,"请输入宽:") print("你输入的长宽是%dx%d"%(x,y)) print("正在运行。请等待结束提示") bwm1.extract(filename=jiemi_name, wm_shape=(x, y), out_wm_name='shuchu/jiemi.png', ) print("解密数据已经输出到shuichu文件夹下,名称为jiemi.png\n\n\n\n") except: print("程序出错,可能是输入文件名称或水印参数错误,,请从头开始") attempts += 1 if attempts == 3000: break return 0
def batch_extract(): password_wm = int(input('算法采用双密码系统,请设定密码1:(必须是数字)')) password_img = int(input('算法采用双密码系统,请设定密码2:(必须是数字)')) bwm1 = WaterMark(password_wm=password_wm, password_img=password_img) watermark_shape = input('请输入水印大小,例如 128x128') watermark_shape = tuple(int(i) for i in watermark_shape.split('x')) # read images _, _, images = list(os.walk("images"))[0] if len(images) == 0: print('images 文件夹中没有图片!,即将退出!') return else: print('已经读取打上水印的图:{}个,分别是 {}'.format(len(images), ', '.join(images))) for image in images: bwm1.extract(filename='images/{image}'.format(image=image), wm_shape=watermark_shape, out_wm_name='output/{image}'.format(image=image)) print('images/{image} 已解出水印,水印放入 output/{image}'.format(image=image))
def extract_text_mark(path_to_image, password=None): import re lg.info(password) if password: _tmp = password else: _tmp = path_to_image match = re.match(r'.*_(\d+)_(\d+)_(\d+)\.\D{3,4}', _tmp) lg.info(match) if not match: raise Exception # a, b, length = (int(x) for x in password.split('_')) a, b, length = (int(x) for x in match.groups()) lg.info(f'{a}, {b}, {length}') wm = WaterMark(a, b) result = wm.extract(path_to_image, length, mode='str') lg.info(f'Extracted text watermark: {result}') return result
# -*- coding: utf-8 -*- # run origin.py to generate the embedded image from blind_watermark import att # 亮度调高攻击 att.bright_att('output/embedded.png', 'output/亮度调高攻击.png', ratio=1.1) #%% 提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(password_wm=1, password_img=1) bwm1.extract(filename='output/亮度调高攻击.png', wm_shape=(128, 128), out_wm_name='output/亮度调高攻击_提取水印.png')
from blind_watermark import att #%% 亮度调低攻击 att.bright_att('output/打上水印的图.png', 'output/亮度调低攻击.png', ratio=0.9) #%% 提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128)) bwm1.extract('output/亮度调低攻击.png', 'output/亮度调低攻击_提取水印.png')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import cv2 from blind_watermark import WaterMark bwm = WaterMark(password_wm=1, password_img=1) # 读取原图 bwm.read_img(filename='pic/ori_img.jpeg') # 读取水印 bwm.read_wm('pic/watermark.png') # 打上盲水印 bwm.embed('output/embedded.png') wm_shape = cv2.imread('pic/watermark.png', flags=cv2.IMREAD_GRAYSCALE).shape # %% 解水印 bwm1 = WaterMark(password_wm=1, password_img=1) # 注意需要设定水印的长宽wm_shape bwm1.extract('output/embedded.png', wm_shape=wm_shape, out_wm_name='output/wm_extracted.png', mode='img')
from blind_watermark import WaterMark bwm1 = WaterMark(password_wm=1, password_img=1) # 读取原图 bwm1.read_img('pic/原图.jpg') # 读取水印 bwm1.read_wm('pic/水印.png') # 打上盲水印 bwm1.embed('output/打上水印的图.png') # %% 解水印 bwm1 = WaterMark(password_wm=1, password_img=1) # 注意需要设定水印的长宽wm_shape bwm1.extract( filename='output/打上水印的图.png', wm_shape=(128, 128), out_wm_name='output/解出的水印.png', )
from blind_watermark import att import numpy as np # 一次横向裁剪打击 att.cut_att_width('output/打上水印的图.png', 'output/横向裁剪攻击.png', ratio=0.5) att.anti_cut_att('output/横向裁剪攻击.png', 'output/横向裁剪攻击_填补.png', origin_shape=(1200, 1920)) # %%提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128)) bwm1.extract("output/横向裁剪攻击_填补.png", "output/横向裁剪攻击_提取水印.png")
from blind_watermark import att import numpy as np # 一次横向裁剪打击 att.cut_att_width('output/打上水印的图.png', 'output/横向裁剪攻击.png', ratio=0.5) att.anti_cut_att('output/横向裁剪攻击.png', 'output/横向裁剪攻击_填补.png', origin_shape=(1200, 1920)) # %%提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(password_wm=1, password_img=1) bwm1.extract(filename="output/横向裁剪攻击_填补.png", wm_shape=(128, 128), out_wm_name="output/横向裁剪攻击_提取水印.png")
from blind_watermark import att # 缩放攻击 att.resize_att('output/打上水印的图.png', 'output/缩放攻击.png', out_shape=(800, 600)) att.resize_att('output/缩放攻击.png', 'output/缩放攻击_还原.png', out_shape=(1920, 1200)) # out_shape 是分辨率,需要颠倒一下 # %%提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128)) bwm1.extract("output/缩放攻击_还原.png", "output/缩放攻击_提取水印.png")
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 除了嵌入图片,也可以嵌入比特类数据 import numpy as np from blind_watermark import WaterMark bwm1 = WaterMark(password_img=1, password_wm=1) # 读取原图 bwm1.read_img('pic/ori_img.jpg') # 读取水印 wm = [True, False, True, True, True, False, True, True, False, True] bwm1.read_wm(wm, mode='bit') # 打上盲水印 bwm1.embed('output/embedded.png') # %% 解水印 # 注意设定水印的长宽wm_shape bwm1 = WaterMark(password_img=1, password_wm=1) wm_extract = bwm1.extract('output/embedded.png', wm_shape=10, mode='bit') print(wm_extract) assert np.all(wm == (wm_extract > 0.5)), '提取水印和原水印不一致'
print("请将你的原图放置于yuantu文件夹下") yuantu_name = str(input("输入你的原图文件名,要带后缀哦,比如:“测试图.png“:\n")) yuantu_name = 'yuantu/'+yuantu_name print("正在寻找"+yuantu_name+"原图文件") bwm1.read_img(yuantu_name) shuiyin_name = str(input("输入你的水印文件名,要带后缀哦,比如:“shuiyin.png“:\n")) shuiyin_name = 'shuiyin/'+shuiyin_name print("正在寻找"+shuiyin_name+"水印文件") bwm1.read_wm(shuiyin_name) shuchu_name = str(input("输入你想要输出的文件名,要带后缀哦,比如:“img_1.png“:\n")) shuchu_name = 'shuchu/'+shuchu_name bwm1.embed(shuchu_name) print("正在输出"+shuchu_name+"已加水印文件") if work_mode == 3: print("图片水印解密模式:") print("请将你需要解密文件放置于jiemi文件夹下") jiemi_name = str(input("输入你的原图文件名,要带后缀哦,比如:“测试图.png“:\n")) jiemi_name = 'jiemi/'+jiemi_name print("正在寻找"+jiemi_name+"待解密文件") password_wm = int(input("请输入水印加密密码:\n")) password_img = int(input("请输入原图加密密码:\n")) bwm1 = WaterMark(password_wm, password_img) print("请输入你曾经加的水印尺寸:例如如果你的水印是640 x 480的,则它的长是640,宽是480 \n") x=int(input("请输入长:")) y=int(input("\n请输入宽:")) print("你输入的长宽是%dx%d"%(x,y)) bwm1.extract(filename=jiemi_name, wm_shape=(x, y), out_wm_name='shuchu/jiemi.png', ) print("解密数据已经输出到shuichu文件夹下,名称为jiemi.png") input("程序执行完成,请检查文件夹下已经生成文件,按回车退出")
from blind_watermark import att import numpy as np # 一次纵向裁剪打击 att.cut_att_height('output/打上水印的图.png', 'output/纵向裁剪打击.png') att.anti_cut_att('output/纵向裁剪打击.png', 'output/纵向裁剪打击_填补.png', origin_shape=(1200, 1920)) # %%纵向裁剪打击.png from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128)) bwm1.extract("output/纵向裁剪打击_填补.png", "output/纵向裁剪打击_提取水印.png")
wm = '@guofei9987 开源万岁!' ori_img_shape = ori_img.shape[:2] # 抗攻击有时需要知道原图的shape # %% embed string into image whose format is numpy.array bwm = WaterMark(password_img=1, password_wm=1) bwm.read_img(img=ori_img) bwm.read_wm(wm, mode='str') embed_img = bwm.embed() len_wm = len(bwm.wm_bit) # 解水印需要用到长度 print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm)) # %% extract from image whose format is numpy.array bwm1 = WaterMark(password_img=1, password_wm=1) wm_extract = bwm1.extract(embed_img=embed_img, wm_shape=len_wm, mode='str') print("不攻击的提取结果:", wm_extract) assert wm == wm_extract, '提取水印和原水印不一致' # %%截屏攻击 = 裁剪攻击 + 缩放攻击 + 知道攻击参数(按照参数还原) loc = ((0.1, 0.1), (0.5, 0.5)) resize = 0.7 img_attacked = att.cut_att(input_img=embed_img, output_file_name=None, loc=loc, resize=resize) bwm1 = WaterMark(password_wm=1, password_img=1) wm_extract = bwm1.extract(embed_img=img_attacked, wm_shape=len_wm, mode='str')
# 除了嵌入图片,也可以嵌入比特类数据 import numpy as np from blind_watermark import WaterMark bwm1 = WaterMark(password_img=1, password_wm=1) # 读取原图 bwm1.read_img('pic/原图.jpg') # 读取水印 wm = [True, False, True, True, True, False, True, True, False, True] bwm1.read_wm(wm, mode='bit') # 打上盲水印 bwm1.embed('output/打上水印的图.png') # %% 解水印 # 注意设定水印的长宽wm_shape bwm1 = WaterMark(password_img=1, password_wm=1) wm_extract = bwm1.extract('output/打上水印的图.png', wm_shape=10, mode='bit') print(wm_extract) assert np.all(wm == (wm_extract > 0.5)), '提取水印和原水印不一致'
# -*- coding: utf-8 -*- # run origin.py to generate the embedded image from blind_watermark import att # 缩放攻击 att.resize_att('output/embedded.png', 'output/缩放攻击.png', out_shape=(800, 600)) att.resize_att('output/缩放攻击.png', 'output/缩放攻击_还原.png', out_shape=(1920, 1200)) # out_shape 是分辨率,需要颠倒一下 # %%提取水印 from blind_watermark import WaterMark bwm1 = WaterMark(password_wm=1, password_img=1) bwm1.extract(filename="output/缩放攻击_还原.png", wm_shape=(128, 128), out_wm_name="output/缩放攻击_提取水印.png")
from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20,block_shape=(4,4)) # 读取原图 bwm1.read_ori_img('pic/原图.jpg') # 读取水印 bwm1.read_wm('pic/水印.png') # 打上盲水印 bwm1.embed('output/打上水印的图.png') # %% 解水印 bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128),block_shape=(4,4)) # 注意需要设定水印的长宽wm_shape bwm1.extract('output/打上水印的图.png', 'output/解出的水印.png')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from blind_watermark import WaterMark bwm1 = WaterMark(password_wm=1, password_img=1) # 读取原图 bwm1.read_img('pic/ori_img.jpeg') # 读取水印 bwm1.read_wm('pic/watermark.png') # 打上盲水印 bwm1.embed('output/embedded.png') # %% 解水印 bwm1 = WaterMark(password_wm=1, password_img=1) # 注意需要设定水印的长宽wm_shape bwm1.extract( filename='output/embedded.png', wm_shape=(64, 64), out_wm_name='output/wm_extracted.png', )
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # embed string import numpy as np from blind_watermark import WaterMark 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) assert wm == wm_extract, '提取水印和原水印不一致'
import cv2 bwm = WaterMark(password_img=1, password_wm=1) bwm.read_img('pic/ori_img.jpeg') wm = '@guofei9987 开源万岁!' 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/ori_img.jpeg').shape[:2] # 抗攻击有时需要知道原图的shape # %% 解水印 bwm1 = WaterMark(password_img=1, password_wm=1) wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str') print("不攻击的提取结果:", wm_extract) assert wm == wm_extract, '提取水印和原水印不一致' # %%截屏攻击 = 裁剪攻击 + 缩放攻击 + 知道攻击参数(按照参数还原) loc = ((0.1, 0.1), (0.5, 0.5)) resize = 0.7 att.cut_att(input_filename='output/embedded.png', output_file_name='output/截屏攻击.png', loc=loc, resize=resize) bwm1 = WaterMark(password_wm=1, password_img=1) wm_extract = bwm1.extract('output/截屏攻击.png', wm_shape=len_wm, mode='str')
from blind_watermark import att import numpy as np import cv2 # %% # 攻击 att.shelter_att('output/打上水印的图.png', 'output/多遮挡攻击.png', ratio=0.1, n=10) # %%多遮挡攻击.png from blind_watermark import WaterMark bwm1 = WaterMark(4399, 2333, 36, 20, wm_shape=(128, 128)) bwm1.extract('output/多遮挡攻击.png', 'output/多遮挡攻击_提取水印.png')