from blind_watermark import WaterMark
import numpy as np
import imageio.v2 as imageio
import random
seed = 13
random.seed( seed )
np.random.seed( 13 )
orig_image_path = './wm1.jpg'
orig_embed_image_path = './wm1.embed.jpg'
attack_embed_image_path = './wm1.embed_attack.jpg'
bwm1 = WaterMark(password_img=1, password_wm=1)
bwm1.read_img( orig_image_path )
wm = '@guofei9987 开源万岁!'
bwm1.read_wm(wm, mode='str')
bwm1.embed( orig_embed_image_path )
len_wm = len(bwm1.wm_bit)
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))
def extract_watermark( image_path ):
bwm1 = WaterMark(password_img=1, password_wm=1)
wm_extract = bwm1.extract(image_path, wm_shape=len_wm, mode='str')
return wm_extract
def periodic_noise( shape ):
ans = np.zeros( shape, dtype='float64' )
ans = ans.reshape( ( ans.size, ) )
loops = 10
for idx in range( loops ):
dat = np.arange( ans.size, dtype='float64' )
dat = 0.025 * (idx+1+random.random()*0.0345) * dat * dat + random.random()*6.28
ans += np.sin( dat )
ans = np.abs( ans.reshape( shape ) )
ans /= np.amax( ans )
return ans
def lsb_attack( image_path, bit = 3, output_path = None ):
image = 1.0 * np.asarray( imageio.imread( image_path ) )
max_peak = 2**bit
image /= max_peak
image = 1.0 * np.asarray( image, dtype='uint8' ) + periodic_noise( image.shape )
#image = 1.0 * np.asarray( image, dtype='uint8' )
image *= max_peak
image = np.asarray( image, dtype='uint8' )
if output_path is not None:
imageio.imwrite( output_path, image )
return image
lsb_attack( orig_embed_image_path, 3, attack_embed_image_path )
print( 'before attack: ', extract_watermark('./wm1.embed.jpg') )
print( 'after attack: ', extract_watermark(attack_embed_image_path) )
输出为:
Welcome to use blind-watermark, version = 0.4.1
Make sure the version is the same when encode and decode
Your star means a lot: https://github.com/guofei9987/blind_watermark
This message only show once. To close it: `blind_watermark.bw_notes.close()`
Put down the length of wm_bit 215
before attack: @guofei9987 开源万岁!
after attack: @gTOne�8902 开�P�(�岁o��
将最低三位用周期噪音代替后水印就失效了,大致代码如下:
输出为:
测试图片选择分辨率为 2048x2048 以尽量方便水印隐藏: https://github.com/fengwang/blind_watermark/blob/master/examples/pic/wm1.jpg