save code

This commit is contained in:
colomi
2026-01-03 21:49:32 +08:00
parent 355b10cc24
commit 4c5dcaa993
3 changed files with 31 additions and 6 deletions
+24 -2
View File
@@ -7,7 +7,8 @@ import random
# url = 'http://172.17.110.92/hairColor/v2'
url = 'https://693565557084165-http-8801.northwest1.gpugeek.com:8443/hairColor/v2'
#url = 'https://693565557084165-http-8801.northwest1.gpugeek.com:8443/hairColor/v2'
url = 'http://localhost:8801/hairColor/v2'
@@ -51,7 +52,7 @@ def image_to_base64(file_path, mime_type=None):
# 组合成Data URI格式
return f"data:{mime_type};base64,{encoded_string}"
img64_str = image_to_base64("aaa.jpg")
img64_str = image_to_base64("qwerqwe.jpg")
data['img'] = img64_str
# 存储所有请求的响应
@@ -115,10 +116,31 @@ avgTime = totalReqTime/len(responses_time)
print(f"平均相应时间:{avgTime}")
def save_base64_image(base64_str, filename):
"""
将 Base64 编码的图片字符串保存为本地 JPG 文件。
:param base64_str: Base64 编码的图片字符串(可能包含 data:image/jpeg;base64, 前缀)
:param filename: 保存的文件名(例如 'image.jpg'
"""
# 如果包含 data URL 前缀,去掉它
if base64_str.startswith("data:image"):
base64_str = base64_str.split(",", 1)[1]
try:
image_data = base64.b64decode(base64_str)
with open(filename, "wb") as f:
f.write(image_data)
print(f"图片已保存为 {filename}")
except Exception as e:
print(f"保存图片失败: {e}")
# 打印所有响应
for i, resp in enumerate(responses, 1):
print(f"请求 {i} 结果:", {resp.status_code, resp.text[:128]})
json_obj = json.loads(resp.text)
data_string = json_obj['result']
save_base64_image(data_string, f"color_result{i}.jpg")
print(f"num_requests:{num_requests} total_time:{total_time} qps:{qps}")
print(f"平均相应时间:{avgTime}")