Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import hashlib
|
|
| 3 |
import io
|
| 4 |
import requests
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
def get_image_md5(image_url):
|
| 8 |
"""
|
|
@@ -12,7 +13,7 @@ def get_image_md5(image_url):
|
|
| 12 |
image_url (str): 图片URL地址
|
| 13 |
|
| 14 |
Returns:
|
| 15 |
-
str:
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
# 从URL获取图片
|
|
@@ -22,26 +23,33 @@ def get_image_md5(image_url):
|
|
| 22 |
# 打开图片
|
| 23 |
image = Image.open(io.BytesIO(response.content))
|
| 24 |
|
| 25 |
-
# 转换为RGB模式(确保可以保存为JPEG)
|
| 26 |
-
if image.mode != 'RGB':
|
| 27 |
-
image = image.convert('RGB')
|
| 28 |
-
|
| 29 |
# 计算MD5值
|
| 30 |
buffered = io.BytesIO()
|
| 31 |
-
image.save(buffered, format=
|
| 32 |
md5_hash = hashlib.md5(buffered.getvalue()).hexdigest()
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# 创建Gradio界面
|
| 39 |
app = gr.Interface(
|
| 40 |
fn=get_image_md5,
|
| 41 |
inputs=gr.Textbox(label="图片URL", placeholder="请输入图片URL地址"),
|
| 42 |
-
outputs=gr.Textbox(label="
|
| 43 |
title="图片MD5生成器",
|
| 44 |
-
description="输入图片URL,获取图片的MD5值"
|
| 45 |
)
|
| 46 |
|
| 47 |
# 启动应用
|
|
|
|
| 3 |
import io
|
| 4 |
import requests
|
| 5 |
from PIL import Image
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
def get_image_md5(image_url):
|
| 9 |
"""
|
|
|
|
| 13 |
image_url (str): 图片URL地址
|
| 14 |
|
| 15 |
Returns:
|
| 16 |
+
str: JSON格式的结果,包含isSuccess、msg和data字段
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
# 从URL获取图片
|
|
|
|
| 23 |
# 打开图片
|
| 24 |
image = Image.open(io.BytesIO(response.content))
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# 计算MD5值
|
| 27 |
buffered = io.BytesIO()
|
| 28 |
+
image.save(buffered, format=image.format)
|
| 29 |
md5_hash = hashlib.md5(buffered.getvalue()).hexdigest()
|
| 30 |
|
| 31 |
+
result = {
|
| 32 |
+
"isSuccess": True,
|
| 33 |
+
"msg": "处理成功",
|
| 34 |
+
"data": md5_hash
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 38 |
except Exception as e:
|
| 39 |
+
result = {
|
| 40 |
+
"isSuccess": False,
|
| 41 |
+
"msg": f"处理失败: {str(e)}",
|
| 42 |
+
"data": None
|
| 43 |
+
}
|
| 44 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 45 |
|
| 46 |
# 创建Gradio界面
|
| 47 |
app = gr.Interface(
|
| 48 |
fn=get_image_md5,
|
| 49 |
inputs=gr.Textbox(label="图片URL", placeholder="请输入图片URL地址"),
|
| 50 |
+
outputs=gr.Textbox(label="结果 (JSON格式)", lines=10),
|
| 51 |
title="图片MD5生成器",
|
| 52 |
+
description="输入图片URL,获取图片的MD5值,返回JSON格式结果"
|
| 53 |
)
|
| 54 |
|
| 55 |
# 启动应用
|