File size: 1,024 Bytes
719eddc
4334a37
719eddc
4334a37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719eddc
4334a37
 
e8b0040
4334a37
e8b0040
 
 
4334a37
e8b0040
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gradio as gr
import os

def process_image(image):
    # 设置保存图片的路径,指向当前工程的 images 目录
    save_path = os.path.join(os.getcwd(), "images")
    # 确保目录存在
    os.makedirs(save_path, exist_ok=True)
    
    # 为每个上传的图片生成唯一的文件名
    from datetime import datetime
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    image_filename = f"uploaded_{timestamp}.png"
    image_path = os.path.join(save_path, image_filename)
    
    # 保存图片
    image.save(image_path)
    
    return f"图片已保存至 {image_path}"  # 返回保存的位置信息

# 定义输入:一个图片选择框
inputs = gr.inputs.Image(label="Upload Image", type="pil")

# 定义输出:一个文本框,用来显示确认信息或处理结果
outputs = gr.outputs.Textbox()

# 创建 Interface 对象,设置 live=False 以添加提交按钮
demo = gr.Interface(fn=process_image, inputs=inputs, outputs=outputs, live=False)

# 启动界面
demo.launch()