-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new algorithm: watermark based on LSB
- Loading branch information
1 parent
b5e44c8
commit ec8aa0a
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ Info Hiding Library | |
|
||
|
||
|
||
|
||
安装 | ||
``` | ||
pip install HideInfo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from hide_info import img_watermark | ||
|
||
# 嵌入隐式水印 | ||
img_watermark.file_encode(img_filename="图片.png", watermark_filename="watermark.png", img_filename_new="图片_打入水印.png") | ||
|
||
# 提取隐式水印 | ||
img_watermark.file_decode(img_filename="图片_打入水印.png", wm_extract="解出的水印.png") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import numpy | ||
import numpy as np | ||
from PIL import Image | ||
|
||
|
||
def encode(img: numpy.ndarray, watermark: numpy.ndarray, n: int): | ||
height, width = img.shape[:2] | ||
height0, width0 = watermark.shape | ||
|
||
a1 = np.uint8(1 << n) | ||
a2 = ~(np.uint8(a1)) | ||
|
||
for i in range(height): | ||
for j in range(width): | ||
if watermark[i % height0, j % width0]: | ||
for channel in range(3): | ||
img[i, j, channel] |= a1 | ||
else: | ||
for channel in range(3): | ||
img[i, j, channel] &= a2 | ||
|
||
|
||
def decode(img, n): | ||
height, width = img.shape[:2] | ||
a1 = np.uint8(1 << n) | ||
watermark_extract = np.zeros(shape=(height, width), dtype=np.uint8) | ||
|
||
for i in range(height): | ||
for j in range(width): | ||
tmp = sum(img[i, j, channel] & a1 for channel in range(3)) | ||
watermark_extract[i, j] = int(tmp * 85) # 85 = 255 / 3 | ||
|
||
return watermark_extract | ||
|
||
|
||
def file_encode(img_filename: str, watermark_filename: str, img_filename_new: str, n: int = 0): | ||
# n:0~3,越大水印强度越高 | ||
watermark = np.array(Image.open(watermark_filename).convert('1')) | ||
img = np.array(Image.open(img_filename)) | ||
encode(img, watermark, n) | ||
Image.fromarray(img).save(img_filename_new) | ||
|
||
|
||
def file_decode(img_filename: str, wm_extract: str, n: int = 0): | ||
img = np.array(Image.open(img_filename)) | ||
watermark_extract = decode(img, n) | ||
Image.fromarray(watermark_extract).save(wm_extract) | ||
|