📅  最后修改于: 2023-12-03 15:27:42.047000             🧑  作者: Mango
背面加减指的是通过给定一张图片,在图片背面加上一定的数字或符号,从而达到加减的目的。这种方式主要用于保护数据的安全性,避免数据暴露,同时也可以防止非法篡改。
在给定的图片背面添加一个数字或者字符串。在阅读图片时,将这个数字或字符串与原图背景颜色合并,得到加法结果。
具体流程如下:
代码实现(Python):
from PIL import Image, ImageDraw, ImageFont
def add(img_path: str, num: int, font_path: str, output_path: str):
img = Image.open(img_path).convert('RGBA')
w, h = img.size
num_img = Image.new('RGBA', (w, h), (0, 0, 0, 0))
ImageDraw.Draw(num_img).text((0, 0), str(num), fill=(255, 255, 255, 255), font=ImageFont.truetype(font_path, 50))
img_data = img.load()
num_data = num_img.load()
new_img = Image.new('RGBA', (w, h), (0, 0, 0, 0))
new_data = new_img.load()
for x in range(w):
for y in range(h):
r1, g1, b1, a1 = img_data[x, y]
r2, g2, b2, a2 = num_data[x, y]
r3 = min(255, r1 + r2)
g3 = min(255, g1 + g2)
b3 = min(255, b1 + b2)
a3 = max(a1, a2)
new_data[x, y] = (r3, g3, b3, a3)
new_img.save(output_path)
在给定的图片背面添加一个数字或者字符串。在阅读图片时,将这个数字或字符串与原图背景颜色按像素逐个相减(RGB 通道),得到减法结果。
具体流程如下:
代码实现(Python):
from PIL import Image, ImageDraw, ImageFont
def subtract(img_path: str, num: int, font_path: str, output_path: str):
img = Image.open(img_path).convert('RGBA')
w, h = img.size
num_img = Image.new('RGBA', (w, h), (0, 0, 0, 0))
ImageDraw.Draw(num_img).text((0, 0), str(num), fill=(255, 255, 255, 255), font=ImageFont.truetype(font_path, 50))
img_data = img.load()
num_data = num_img.load()
new_img = Image.new('RGBA', (w, h), (0, 0, 0, 0))
new_data = new_img.load()
for x in range(w):
for y in range(h):
r1, g1, b1, a1 = img_data[x, y]
r2, g2, b2, a2 = num_data[x, y]
r3 = max(0, r1 - r2)
g3 = max(0, g1 - g2)
b3 = max(0, b1 - b2)
a3 = max(a1, a2)
new_data[x, y] = (r3, g3, b3, a3)
new_img.save(output_path)