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 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 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_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 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
#!/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))
#!/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', )
# 除了嵌入图片,也可以嵌入比特类数据 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(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')
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 str(shuiyin_name) str(shuchu_name) print(shuiyin_name+"水印读取中") print(shuchu_name+"输出图生成中") print(str(i/shu_liang*100)+"%已经完成") bwm1.read_wm(shuiyin_name) bwm1.embed(shuchu_name) i =i+ 1 if work_mode == 2: print("自定义工作模式:") print("提示:此模式可以添加任意水印到任意图中,请注意本程序限制了水印的大小\n 以防止过大水印造成的画质损失,水印推荐使用透明底黑色标记符号或高反差图像,\n 原图越大,你可以写入的水印就越大,对于更大的图片,建议放置白底黑色二维码,\n 如果程序窗口出错,请注意出错内容中,可容纳大小提示") password_wm = int(input("请输入水印加密密码,(请记住此密码,解密时需要)只可以任意长度数字即可:\n")) password_img = int(input("请输入原图加密密码,(请记住此密码,解密时需要)只可以任意长度数字即可:\n")) bwm1 = WaterMark(password_wm, password_img) print("请将你的原图放置于yuantu文件夹下") yuantu_name = str(input("输入你的原图文件名,要带后缀哦,比如:“测试图.png“:\n")) yuantu_name = 'yuantu/'+yuantu_name print("正在寻找"+yuantu_name+"原图文件") bwm1.read_img(yuantu_name)
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