def __init__(self, password_wm, password_img): self._password_wm = password_wm self._password_img = password_img self.bwm = WaterMark(password_wm=password_wm, password_img=password_img) self._bit_length = 0 self._path_to_image = ''
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_embed(): password_wm = int(input('算法采用双密码系统,请设定密码1:(必须是数字)')) password_img = int(input('算法采用双密码系统,请设定密码2:(必须是数字)')) bwm1 = WaterMark(password_wm=password_wm, password_img=password_img) _, _, files = list(os.walk("watermark"))[0] if len(files) == 0: print('wartermark 文件夹中没有图片!,即将退出!') return else: watermark = files[0] print('水印是 {}'.format(watermark)) # read watermark bwm1.read_wm('watermark/{watermark}'.format(watermark=watermark)) watermark_shape = cv2.imread( 'watermark/{watermark}'.format(watermark=watermark)).shape[:] print('请记下水印大小,解密的时候需要输入: {}x{}'.format(*watermark_shape)) # 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.read_img('images/{image}'.format(image=image)) bwm1.embed('output/{image}'.format(image=image)) print('images/{image} 已经被打上水印,并放入 output/{image}'.format(image=image))
def savefile(request): if request.method == 'POST': print(request) print(request.FILES) file_obj = request.FILES.get('img') print(file_obj) file_path = os.path.join('static/uploadfiles/', file_obj.name) with open(file_path, 'wb') as f: for chunk in file_obj.chunks(): f.write(chunk) # return HttpResponse(file_path) bwm1 = WaterMark(password_img=1, password_wm=1) bwm1.read_img('static/uploadfiles/' + file_obj.name) wm = '@zipu888 大神万岁!' bwm1.read_wm(wm, mode='str') bwm1.embed('static/uploadfiles/bwfiles/' + file_obj.name) len_wm = len(bwm1.wm_bit) print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm)) img = cv.imread('static/uploadfiles/bwfiles/' + file_obj.name) # 降噪处理 dst = cv.fastNlMeansDenoisingColored(img, None, 7, 7, 7, 21) cv.imwrite('static/uploadfiles/sfiles/' + file_obj.name, dst, params=None) data = { 'srcImg': '/static/uploadfiles/' + file_obj.name, 'desImg': '/static/uploadfiles/bwfiles/' + file_obj.name } result = {'code': 0, 'msg': 'success', 'data': data} return HttpResponse(json.dumps(result), content_type="application/json")
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
def mod_1(): attempts = 0 success = False while attempts < 3000 and not success: try: print("默认工作模式:") 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("请将你的原图放置于yuantu文件夹下") yuantu_name_head = "abc" yuantu_name_head = get_str(yuantu_name_head,"输入你的原图文件名,要带后缀,例如:abc.png/abc.jpg|不支持带汉字文件名") yuantu_name = 'yuantu/'+yuantu_name_head check_mark(yuantu_name) print("正在寻找"+yuantu_name+"文件") bwm1.read_img(yuantu_name) print("图片已经被读取") shu_liang = 50 shu_liang = get_value(shu_liang,"输入你想生成的图片数量,目前最大支持60张,\n 你可以自行放入wm_61/wm_62...,然后输入数量即可") i = 1 int(i) int(shu_liang) while i < shu_liang+1: ge_shi = str('.png') shuiyin_tu = str('shuiyin/wm_') shuiyin_name = shuiyin_tu+str(i)+ge_shi shuchu_tu = str('shuchu/img_') shuchu_name = shuchu_tu+str(i)+"_"+yuantu_name_head str(shuiyin_name) str(shuchu_name) print(shuiyin_name+"水印读取中") print(shuchu_name+"输出图生成中") bwm1.read_wm(shuiyin_name) bwm1.embed(shuchu_name) print(str(i/shu_liang*100)+"%已经完成") i =i+ 1 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 test_add_text_watermark_with_cipher(self): marker = self.marker marker._read_data(self.image, self.text) a, b, c = marker._get_encryption_code() self.assertEqual(a, 123) self.assertEqual(b, 321) wm = WaterMark() wm.read_img(self.image) wm.read_wm(self.text, mode='str') bit_length = len(wm.wm_bit) self.assertEqual(c, bit_length)
def mod_2(): attempts = 0 success = False while attempts < 3000 and not success: try: print("自定义工作模式:") print("提示:此模式可以添加任意水印到任意图中,请注意本程序限制了水印的大小\n 以防止过大水印造成的画质损失,水印推荐使用透明底黑色标记符号或高反差图像,\n 原图越大,你可以写入的水印就越大,对于更大的图片,建议放置白底黑色二维码,\n 如果程序窗口出错,请注意出错内容中,可容纳大小提示") 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("请将你的原图放置于yuantu文件夹下") yuantu_name = "abc" yuantu_name = get_str(yuantu_name,"输入你的原图文件名,要带后缀,例如:abc.png/abc.jpg|不支持带汉字文件名") yuantu_name = 'yuantu/'+yuantu_name print("正在寻找"+yuantu_name+"原图文件") bwm1.read_img(yuantu_name) shuiyin_name = "abc" shuiyin_name = get_str(shuiyin_name,"输入你的水印文件名,要带后缀,例如:abc.png/abc.jpg|不支持带汉字文件名") shuiyin_name = 'shuiyin/'+shuiyin_name check_mark(yuantu_name) print("正在寻找"+shuiyin_name+"水印文件") bwm1.read_wm(shuiyin_name) shuchu_name = "abc" shuchu_name = get_str(shuchu_name,"输入你想要的输出文件名,要带后缀,例如:abc.png/abc.jpg|不支持带汉字文件名") shuchu_name = 'shuchu/'+shuchu_name print("正在输出"+shuchu_name+"已加水印文件,请等待结束提示") bwm1.embed(shuchu_name) print("输出完毕\n\n\n\n") except: print("程序出错,可能是你输入的图片过小,没有足够空间添加水印,或输入文件名称错误,请从头开始") attempts += 1 if attempts == 3000: break return 0
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")
#!/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', )
# -*- 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')
# 除了嵌入图片,也可以嵌入比特类数据 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)), '提取水印和原水印不一致'
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 #%% 亮度调低攻击 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')
The format of images is numpy.array. This may be useful if you want to use blind-watermark in another project. """ from blind_watermark import WaterMark from blind_watermark import att from blind_watermark.recover import estimate_crop_parameters, recover_crop import cv2 import numpy as np ori_img = cv2.imread('pic/ori_img.jpeg', flags=cv2.IMREAD_UNCHANGED) 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, '提取水印和原水印不一致'
#!/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, '提取水印和原水印不一致'
class Marker: def __init__(self, password_wm, password_img): self._password_wm = password_wm self._password_img = password_img self.bwm = WaterMark(password_wm=password_wm, password_img=password_img) self._bit_length = 0 self._path_to_image = '' def add_text_mark_write(self, path_to_image, wm_text, path_to_output, is_with_passcode=False): lg.info(f'Working directory: {os.getcwd()}') lg.info(f'Parameters: {path_to_image}, {wm_text}, {path_to_output}') self._path_to_image = path_to_image self._read_data(path_to_image, wm_text) if is_with_passcode: path = self._write_to_with_passcode(path_to_output) else: path = self._write_to(path_to_output) lg.info(f'File saved to: {os.path.abspath(path_to_output)}') lg.info(f'Watermark is: {wm_text}') return path def _write_to(self, path_to_output): self.bwm.embed(path_to_output) return path_to_output def _write_to_with_passcode(self, path_to_output): basename = os.path.basename(path_to_output) dirname = os.path.dirname(path_to_output) filename, suffix = os.path.splitext(basename) lg.info(f'Suffix: {suffix}') path_to_output_with_passcode = f'{os.path.join(dirname, filename)}' \ f'_{self._password_wm}' \ f'_{self._password_img}' \ f'_{self._bit_length}' \ f'{suffix}' lg.info(f'Output: {path_to_output_with_passcode}') lg.info( f'Don\'t forget your password pair for this image: {self._password_wm}, {self._password_img}' ) return self._write_to(path_to_output_with_passcode) @staticmethod 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 def _read_data(self, path_to_image, wm_text): if not os.path.isfile(path_to_image): raise FileNotFoundError self.bwm.read_img(path_to_image) self.bwm.read_wm(wm_text, mode='str') self._bit_length = len(self.bwm.wm_bit) def _get_encryption_code(self): if self._bit_length == 0: raise NotImplementedError return self._password_wm, self._password_img, self._bit_length
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")
print("开发者:evenif/风栖木兮") print("开源代码地址:https://github.com/guofei9987/blind_watermark") print("程序开始运行:") print("如程序出错,请检查你的输入内容,重新打开此程序即可") print("请注意,本程序需要和其下文件夹配合工作他们分别是:\n yuantu:放置原图\n shuiyin;存放水印\n jiemi存放等待解密文件\n shuichu输出文件夹\n 复制此程序时,请直接打包本程序所在文件夹") #选择工作模式 #选择引擎 print("请选择程序工作模式:\n 1: 默认工作模式(自动添加一定数量水印)\n 2: 自定义模式(请在文件夹添加你的自定义水印)\n 3: 水印解密模式(需要输入密码)") work_mode = int(input("请输入序号")) if work_mode == 1: print("默认工作模式:") password_wm = int(input("请输入水印加密密码,(请记住此密码,解密时需要)只可以任意长度数字即可:\n")) password_img = int(input("请输入原图加密密码,(请记住此密码,解密时需要)只可以任意长度数字即可:\n")) bwm1 = WaterMark(password_wm, password_img) print("请将你的原图放置于yuantu文件夹下") yuantu_name_head = str(input("输入你的原图文件名,要带后缀哦,比如:“测试图.png“:\n")) yuantu_name = 'yuantu/'+yuantu_name_head print("正在寻找"+yuantu_name+"文件") bwm1.read_img(yuantu_name) shu_liang = int(input("输入你想生成的图片数量,目前最大支持50张哦\n")) i = 1 int(i) int(shu_liang) while i < shu_liang+1: ge_shi = str('.png') shuiyin_tu = str('shuiyin/wm_') shuiyin_name = shuiyin_tu+str(i)+ge_shi shuchu_tu = str('shuchu/img_') shuchu_name = shuchu_tu+str(i)+"_"+yuantu_name_head
#!/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=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))
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')