From 98a023676217d3f79c85fa5accdcf9cfc42351c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Fri, 23 Aug 2024 15:59:01 +0800 Subject: [PATCH] 'commit' --- BaiHu/Util/WriteYinCangWenZi.py | 99 +++--------------- .../WriteYinCangWenZi.cpython-310.pyc | Bin 3150 -> 3150 bytes BaiHu/doTask.py | 25 ++++- 3 files changed, 34 insertions(+), 90 deletions(-) diff --git a/BaiHu/Util/WriteYinCangWenZi.py b/BaiHu/Util/WriteYinCangWenZi.py index b9e71d29..065b8e5b 100644 --- a/BaiHu/Util/WriteYinCangWenZi.py +++ b/BaiHu/Util/WriteYinCangWenZi.py @@ -1,85 +1,14 @@ -from PIL import Image - - -def makeImageEven(image): - """ - 将图像像素值变为偶数值(最低有效位为0) - """ - # 获取图像所有像素值的列表:[(r,g,b,t),(r,g,b,t)...] - pixels = list(image.getdata()) - # 将所有像素值变为偶数(通过移位) - evenPixels = [(r >> 1 << 1, g >> 1 << 1, b >> 1 << 1, t >> 1 << 1) for [r, g, b, t] in pixels] - # 创建一个与原图像大小相同的副本 - evenImage = Image.new(image.mode, image.size) - # 将偶数像素值放入副本中 - evenImage.putdata(evenPixels) - return evenImage - - -def constLenBin(int): - """ - 返回固定长度的二进制字符串,替代内置函数bin() - """ - # 使用二进制表示整数,不足8位在左侧补0 - binary = "0" * (8 - (len(bin(int)) - 2)) + bin(int).replace('0b', '') - return binary - - -def encodeDataInImage(image, data): - """ - 将字符串编码到图片中 - """ - # 获取最低有效位为0的图片副本 - evenImage = makeImageEven(image) - # 将需要隐藏的字符串转换成二进制字符串 - binary = ''.join(map(constLenBin, bytearray(data, 'utf-8'))) - if len(binary) > len(image.getdata()) * 4: - # 若无法编码全部数据,抛出异常 - raise Exception("Error: Can't encode more than" + len(evenImage.getdata()) * 4 + " bits in this image. ") - # 将二进制信息编码进像素值中 - encodedPixels = [(r + int(binary[index * 4 + 0]), g + int(binary[index * 4 + 1]), b + int(binary[index * 4 + 2]), - t + int(binary[index * 4 + 3])) if index * 4 < len(binary) else (r, g, b, t) for - index, (r, g, b, t) in enumerate(list(evenImage.getdata()))] - # 创建新图片以存放编码后的像素 - encodedImage = Image.new(evenImage.mode, evenImage.size) - # 添加编码后的数据 - encodedImage.putdata(encodedPixels) - return encodedImage - - -def binaryToString(binary): - """ - 从二进制字符串转为 UTF-8 字符串 - """ - index = 0 - string = [] - rec = lambda x, i: x[2:8] + (rec(x[8:], i - 1) if i > 1 else '') if x else '' - fun = lambda x, i: x[i + 1:8] + rec(x[8:], i - 1) - while index + 1 < len(binary): - chartype = binary[index:].index('0') # 存放字符所占字节数,一个字节的字符会存为0 - length = chartype * 8 if chartype else 8 - string.append(chr(int(fun(binary[index:index + length], chartype), 2))) - index += length - return ''.join(string) - - -def decodeImage(image): - """ - 解码隐藏数据 - """ - pixels = list(image.getdata()) # 获取像素列表 - # 提取图片中所有最低有效位中的数据 - binary = ''.join([str(int(r >> 1 << 1 != r)) + str(int(g >> 1 << 1 != g)) + str(int(b >> 1 << 1 != b)) + str( - int(t >> 1 << 1 != t)) for (r, g, b, t) in pixels]) - # 找到数据截止处的索引 - locationDoubleNull = binary.find('0000000000000000') - endIndex = locationDoubleNull + ( - 8 - (locationDoubleNull % 8)) if locationDoubleNull % 8 != 0 else locationDoubleNull - data = binaryToString(binary[0:endIndex]) - return data - - -# 调用函数对图片进行数据编码并保存 -#encodeDataInImage(Image.open("d:/9.png"), 'https://www.hzkjai.com 吉林绘智科技有限公司').save('d:/9.png') -# 解码图片中隐藏的数据并打印 -#print(decodeImage(Image.open("d:/9.png"))) +# cd D:\py310\Scripts +# pip install stegano -i https://pypi.tuna.tsinghua.edu.cn/simple/ +# 安装stegano的步骤 +from stegano import lsb # pip install stegano + +message = "绘鸭AI相机" # 要写入的文本 +origin_file = "d:/9fc0cb1d-12ec-4ed8-8199-b7ccd88da535.png" # 原图 +new_name = "d:/10.png" # 要保存的新图片名称 +encoding = "UTF-32LE" # 文本包含中文的话,需指定为该编码 +secret = lsb.hide(origin_file, message, encoding=encoding) # 往图片写入文本 +secret.save(new_name) # 保持到本地 + +unpack_msg: str = lsb.reveal(new_name, encoding=encoding) # 从本地图片里读出文本 +print(f"{unpack_msg = }") diff --git a/BaiHu/Util/__pycache__/WriteYinCangWenZi.cpython-310.pyc b/BaiHu/Util/__pycache__/WriteYinCangWenZi.cpython-310.pyc index b9202af93a49c0862e2554fe689d4c9a6160dace..e1e463fb33b70600d5319fc953fc8921111eae9f 100644 GIT binary patch delta 18 YcmX>naZZ9WpO=@50SJOOa=P*W04p*B#{d8T delta 18 YcmX>naZZ9WpO=@50SM$ba=P*W04gp6lK=n! diff --git a/BaiHu/doTask.py b/BaiHu/doTask.py index 7b966da7..595b7986 100644 --- a/BaiHu/doTask.py +++ b/BaiHu/doTask.py @@ -9,7 +9,11 @@ from Util.CommonUtil import * from Util.OssUtil import uploadOss from Util.PngUtil import * from PIL import Image, ImageDraw, ImageFont -from Util.WriteYinCangWenZi import * +# cd D:\py310\Scripts +# pip install stegano -i https://pypi.tuna.tsinghua.edu.cn/simple/ +# 安装stegano的步骤 +from stegano import lsb # pip install stegano + def WriteWenZi(image_path): # 打开图片 @@ -23,8 +27,10 @@ def WriteWenZi(image_path): # 设置文本内容和坐标 text = 'AI生成' - x = image.width - 300 - y = image.height - 130 + # x = image.width - 300 + # y = image.height - 130 + x = 50 + y = 50 # 绘制文本 draw.text((x, y), text, font=font, fill='white') @@ -32,13 +38,22 @@ def WriteWenZi(image_path): # 保存修改后的图片 image.save(image_path) image.close() + + def upload_img_file(file_path, model_id): png_file = os.path.basename(file_path) # 添加水印信息 WriteWenZi(file_path) # 添加隐藏信息 # 调用函数对图片进行数据编码并保存 - encodeDataInImage(Image.open(file_path), 'https://www.hzkjai.com 吉林绘智科技有限公司').save(file_path) + message = "绘鸭AI相机" # 要写入的文本 + encoding = "UTF-32LE" # 文本包含中文的话,需指定为该编码 + secret = lsb.hide(file_path, message, encoding=encoding) # 往图片写入文本 + secret.save(file_path) # 保持到本地 + # 从本地图片里读出文本 + # unpack_msg: str = lsb.reveal(file_path, encoding=encoding) + # print(f"{unpack_msg = }") + # 清除Exif信息 clearExif(file_path, file_path) # 清除Exif信息 # 上传到OSS @@ -47,7 +62,7 @@ def upload_img_file(file_path, model_id): # # 生成下载链接 url = 'http://hzkc.oss-cn-beijing.aliyuncs.com/' + key # 删除本地文件 - os.remove(file_path) + # os.remove(file_path) printf("成功生成1张图片,地址:" + url) return url