You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.1 KiB

1 year ago
import json
import base64
import os.path
import uuid
from datetime import datetime
import requests
from PIL import Image
import io
def submit_post(url: str, data: dict):
"""
Submit a POST request to the given URL with the given data.
"""
return requests.post(url, data=json.dumps(data))
def save_encoded_image(b64_image: str, output_path: str):
"""
Save the given image to the given output path.
"""
with open(output_path, "wb") as image_file:
image_file.write(base64.b64decode(b64_image))
def encode_image(image_path):
with open(image_path, "rb") as i:
b64 = base64.b64encode(i.read())
return b64.decode("utf-8")
# 打开一个JSON文件用于读取
def getJson(json_path):
with open(json_path, 'r', encoding='utf-8') as f:
# 从文件中加载JSON数据到字典
data = json.load(f)
return data
def printf(str):
# 获取当前时间
now = datetime.now()
# 格式化时间
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
# 打印当前时间
print(current_time + ' ' + str)
# 获取图片的Base64编码
def get_image_base64_str(image_path):
with open(image_path, 'rb') as image_file:
# 使用Pillow库打开图像文件
image = Image.open(image_file)
# 转换图像为RGB模式因为JPEG不支持透明度
image = image.convert('RGB')
# 创建一个字节流对象
buffer = io.BytesIO()
# 将图像保存到字节流中格式为JPEG
image.save(buffer, format='JPEG')
# 移动到字节流的开始位置
buffer.seek(0)
# 将字节流中的数据编码为base64
encoded_image = base64.b64encode(buffer.read())
# 将编码后的图像数据转换为字符串并构建完整的URL格式
base64_image_url = 'data:image/jpeg;base64,' + encoded_image.decode('utf-8')
return base64_image_url
# 将图片一切为四
def split_4_image(image_path, out_path):
res = []
# 获取图片的宽度
img = Image.open(image_path)
width, height = img.size
new_width, new_height = width // 2, height // 2
# 左上角的图片
top_left = img.crop((0, 0, new_width, new_height))
f_name = str(uuid.uuid4()) + '.png'
fi = os.path.join(out_path, f_name)
res.append(f_name)
top_left.save(fi)
# 右上角的图片
top_right = img.crop((new_width, 0, width, new_height))
f_name = str(uuid.uuid4()) + '.png'
fi = os.path.join(out_path, f_name)
res.append(f_name)
top_right.save(fi)
# 左下角的图片
bottom_left = img.crop((0, new_height, new_width, height))
f_name = str(uuid.uuid4()) + '.png'
fi = os.path.join(out_path, f_name)
res.append(f_name)
bottom_left.save(fi)
# 右下角的图片
bottom_right = img.crop((new_width, new_height, width, height))
f_name = str(uuid.uuid4()) + '.png'
fi = os.path.join(out_path, f_name)
res.append(f_name)
bottom_right.save(fi)
return res
1 year ago