From 1e494d89332da194dd1f1723209f0e66270fcc67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E9=A3=9E?= Date: Sun, 9 Apr 2023 13:07:22 +0000 Subject: [PATCH] demonstrate multiprocessing and multithreading --- .github/workflows/tests.yml | 1 + examples/example_str_multi.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 examples/example_str_multi.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c6b301b..65b3add 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -25,6 +25,7 @@ jobs: python examples/example_img.py python examples/example_no_writing.py python examples/example_str.py + python examples/example_str_multi.py # pytest --cov . # - name: Upload coverage reports to Codecov with GitHub Action # uses: codecov/codecov-action@v3 diff --git a/examples/example_str_multi.py b/examples/example_str_multi.py new file mode 100644 index 0000000..82f9ef9 --- /dev/null +++ b/examples/example_str_multi.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +''' +demonstrate multiprocessing and multithreading +''' +# embed string +from blind_watermark import WaterMark +import cv2 +import os + +os.chdir(os.path.dirname(__file__)) + +mode = 'multiprocessing' +bwm = WaterMark(password_img=1, password_wm=1, processes=mode) +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 +h, w = ori_img_shape + +# %% 解水印 +bwm1 = WaterMark(password_img=1, password_wm=1, processes=mode) +wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str') +print("不攻击的提取结果:", wm_extract) + +assert wm == wm_extract, '提取水印和原水印不一致' + +mode = 'multithreading' + +bwm = WaterMark(password_img=1, password_wm=1, processes=mode) +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 +h, w = ori_img_shape + +# %% 解水印 +bwm1 = WaterMark(password_img=1, password_wm=1, processes=mode) +wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str') +print("不攻击的提取结果:", wm_extract) + +assert wm == wm_extract, '提取水印和原水印不一致'