cpuai commited on
Commit
f8ef2aa
·
verified ·
1 Parent(s): 094bc26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # Hugging Face Spaces (Gradio / CPU) 单文件应用:
3
+ # 上传图片 -> 点击按钮 -> 输出 Base64 字符串(可选 data URI 前缀、可选换行)
4
+
5
+ import base64
6
+ import mimetypes
7
+ import textwrap
8
+ from pathlib import Path
9
+ import gradio as gr
10
+
11
+
12
+ def image_to_base64(
13
+ image_path: str | None,
14
+ add_data_uri_prefix: bool,
15
+ wrap_lines: bool,
16
+ wrap_width: int,
17
+ ) -> tuple[str, int]:
18
+ """
19
+ 将图片文件转换为 Base64 字符串。
20
+ - image_path: gr.Image(type="filepath") 传入的本地临时文件路径
21
+ - add_data_uri_prefix: 是否加 data:{mime};base64, 前缀
22
+ - wrap_lines: 是否按固定宽度换行(便于复制到某些工具/配置文件)
23
+ - wrap_width: 换行宽度(常见 76/80)
24
+ """
25
+ if not image_path:
26
+ return "请先上传一张图片。", 0
27
+
28
+ p = Path(image_path)
29
+ if not p.exists():
30
+ return f"文件不存在:{image_path}", 0
31
+
32
+ # 读入原始二进制数据(保持原格式,不做重编码)
33
+ data = p.read_bytes()
34
+
35
+ # 猜测 MIME(无法识别时给一个保守默认值)
36
+ mime, _ = mimetypes.guess_type(str(p))
37
+ if not mime:
38
+ mime = "image/png"
39
+
40
+ # Base64 编码
41
+ b64 = base64.b64encode(data).decode("utf-8")
42
+
43
+ # 是否加 data URI 前缀
44
+ result = f"data:{mime};base64,{b64}" if add_data_uri_prefix else b64
45
+
46
+ # 是否换行(注意:若加了 data URI 前缀,换行也会一起换行)
47
+ if wrap_lines:
48
+ w = int(wrap_width) if wrap_width else 76
49
+ w = max(20, min(w, 500)) # 做个合理范围保护
50
+ result = "\n".join(textwrap.wrap(result, width=w))
51
+
52
+ return result, len(result)
53
+
54
+
55
+ with gr.Blocks(title="Image → Base64 (CPU)") as demo:
56
+ gr.Markdown(
57
+ "# Image → Base64\n"
58
+ "上传图片后点击 **转换**,即可得到 Base64 字符串。\n\n"
59
+ "- 支持可选 `data:image/...;base64,` 前缀\n"
60
+ "- 支持可选按固定宽度换行\n\n"
61
+ "提示:图片较大时,Base64 字符串会非常长(体积约增加 1/3)。"
62
+ )
63
+
64
+ with gr.Row():
65
+ inp = gr.Image(
66
+ label="上传图片",
67
+ type="filepath", # 关键:回调函数收到的是文件路径字符串
68
+ sources=["upload"],
69
+ )
70
+
71
+ with gr.Row():
72
+ add_prefix = gr.Checkbox(value=False, label="添加 data URI 前缀(data:image/...;base64,)")
73
+ wrap = gr.Checkbox(value=False, label="按固定宽度换行")
74
+ width = gr.Slider(
75
+ minimum=40,
76
+ maximum=200,
77
+ value=76,
78
+ step=1,
79
+ label="换行宽度(wrap width)",
80
+ )
81
+
82
+ with gr.Row():
83
+ btn = gr.Button("转换", variant="primary")
84
+ clear = gr.Button("清空")
85
+
86
+ out = gr.Textbox(
87
+ label="Base64 输出(可直接复制)",
88
+ lines=14,
89
+ interactive=False,
90
+ placeholder="这里会显示 Base64 字符串……",
91
+ )
92
+ out_len = gr.Number(label="输出字符数", value=0, interactive=False)
93
+
94
+ btn.click(
95
+ fn=image_to_base64,
96
+ inputs=[inp, add_prefix, wrap, width],
97
+ outputs=[out, out_len],
98
+ )
99
+
100
+ def _clear():
101
+ return None, False, False, 76, "", 0
102
+
103
+ clear.click(
104
+ fn=_clear,
105
+ inputs=[],
106
+ outputs=[inp, add_prefix, wrap, width, out, out_len],
107
+ )
108
+
109
+ if __name__ == "__main__":
110
+ demo.launch()