Skip to content

Commit

Permalink
new algorithm: watermark based on LSB
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Oct 22, 2023
1 parent b5e44c8 commit ec8aa0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Info Hiding Library




安装
```
pip install HideInfo
Expand Down
7 changes: 7 additions & 0 deletions example/example_img_watermark.py
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")
Binary file added example/watermark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions hide_info/img_watermark.py
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)

0 comments on commit ec8aa0a

Please sign in to comment.