Created
July 31, 2023 19:34
-
-
Save swiftcafex/91e7a6a9decb6ce615cab61820d05f75 to your computer and use it in GitHub Desktop.
生成隐形水印,提取隐形水印
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
from PIL import Image | |
import numpy as np | |
import sys | |
import os | |
def extract_watermark(input_image_path): | |
# 打开带有隐形水印的图片 | |
watermarked_image = Image.open(input_image_path) | |
watermarked_pixels = np.array(watermarked_image) | |
# 提取红色通道 | |
red_channel = watermarked_pixels[:, :, 0] | |
# 根据红色通道的奇偶性创建二进制水印图像 | |
# 偶数表示白色(255),奇数表示黑色(0) | |
binary_watermark = (red_channel % 2 == 0).astype(np.uint8) * 255 | |
# 将二进制水印图像保存为新图像 | |
extracted_watermark_image = Image.fromarray(binary_watermark) | |
extracted_watermark_path = os.path.join(os.path.dirname(input_image_path), 'watermark.png') | |
extracted_watermark_image.save(extracted_watermark_path, format='PNG') | |
print(f"Extracted watermark image saved to {extracted_watermark_path}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python script.py <path_to_watermarked_image>") | |
sys.exit(1) | |
input_image_path = os.path.abspath(sys.argv[1]) # 支持相对路径 | |
extract_watermark(input_image_path) |
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
from PIL import Image, ImageDraw, ImageFont | |
import numpy as np | |
import sys | |
import os | |
def add_watermark(input_image_path, watermark_text): | |
# 打开原始图片 | |
original_image = Image.open(input_image_path) | |
original_pixels = np.array(original_image) | |
# 创建水印文字图片 | |
watermark_image = Image.new('L', original_image.size, 255) # 白色背景 | |
draw = ImageDraw.Draw(watermark_image) | |
font_size = 50 | |
font_path = "/System/Library/Fonts/Helvetica.ttc" # macOS 上的 Helvetica 字体路径 | |
font = ImageFont.truetype(font_path, font_size) | |
text_width, text_height = draw.textsize(watermark_text, font=font) | |
# 添加边距 | |
margin = 20 | |
text_width += margin * 2 | |
text_height += margin * 2 | |
# 以重复平铺的方式绘制水印文字 | |
for y in range(0, watermark_image.height, text_height): | |
for x in range(0, watermark_image.width, text_width): | |
draw.text((x + margin, y + margin), watermark_text, font=font, fill=0) # 使用黑色填充文字,添加边距 | |
watermark_pixels = np.array(watermark_image) | |
# 将原始图片的红色通道每个像素值向上加1或向下减1,根据水印文字的黑色和白色部分 | |
height, width, _ = original_pixels.shape | |
for i in range(height): | |
for j in range(width): | |
# 检查水印像素是否是白色 | |
if watermark_pixels[i, j] == 255: | |
# 如果原始红色通道值是奇数,则需要将其变为偶数 | |
if original_pixels[i, j, 0] % 2 == 1: | |
# 检查是否可以增加 1 而不超出 RGB 范围 | |
if original_pixels[i, j, 0] < 255: | |
original_pixels[i, j, 0] += 1 | |
else: | |
original_pixels[i, j, 0] -= 1 | |
else: | |
# 水印像素是黑色,需要将原始红色通道值变为奇数 | |
if original_pixels[i, j, 0] % 2 == 0: | |
# 检查是否可以增加 1 而不超出 RGB 范围 | |
if original_pixels[i, j, 0] < 255: | |
original_pixels[i, j, 0] += 1 | |
else: | |
original_pixels[i, j, 0] -= 1 | |
# 保存修改后的图片 | |
output_image = Image.fromarray(original_pixels) | |
output_image_path = os.path.join(os.path.dirname(input_image_path), 'output.png') | |
output_image.save(output_image_path, format='PNG') | |
print(f"Watermarked image saved to {output_image_path}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
print("Usage: python script.py <path_to_image> <watermark_text>") | |
sys.exit(1) | |
input_image_path = os.path.abspath(sys.argv[1]) # 支持相对路径 | |
watermark_text = sys.argv[2] | |
add_watermark(input_image_path, watermark_text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment