image-base64 / app.py
cpuai's picture
Update app.py
97ef833 verified
# app.py
# Hugging Face Spaces (Gradio / CPU) 单文件应用:
# 上传图片 -> 点击按钮 -> 输出 Base64 字符串(可选 data URI 前缀、可选换行)
import base64
import mimetypes
import textwrap
from pathlib import Path
import gradio as gr
def image_to_base64(
image_path: str | None,
add_data_uri_prefix: bool,
wrap_lines: bool,
wrap_width: int,
) -> tuple[str, int]:
"""
将图片文件转换为 Base64 字符串。
- image_path: gr.Image(type="filepath") 传入的本地临时文件路径
- add_data_uri_prefix: 是否加 data:{mime};base64, 前缀
- wrap_lines: 是否按固定宽度换行(便于复制到某些工具/配置文件)
- wrap_width: 换行宽度(常见 76/80)
"""
if not image_path:
return "请先上传一张图片。", 0
p = Path(image_path)
if not p.exists():
return f"文件不存在:{image_path}", 0
# 读入原始二进制数据(保持原格式,不做重编码)
data = p.read_bytes()
# 猜测 MIME(无法识别时给一个保守默认值)
mime, _ = mimetypes.guess_type(str(p))
if not mime:
mime = "image/png"
# Base64 编码
b64 = base64.b64encode(data).decode("utf-8")
# 是否加 data URI 前缀
result = f"data:{mime};base64,{b64}" if add_data_uri_prefix else b64
# 是否换行(注意:若加了 data URI 前缀,换行也会一起换行)
if wrap_lines:
w = int(wrap_width) if wrap_width else 76
w = max(20, min(w, 500)) # 做个合理范围保护
result = "\n".join(textwrap.wrap(result, width=w))
return result, len(result)
with gr.Blocks(title="Image → Base64 (CPU)") as demo:
gr.Markdown(
"# Image → Base64\n"
"上传图片后点击 **转换**,即可得到 Base64 字符串。\n\n"
"- 支持可选 `data:image/...;base64,` 前缀\n"
"- 支持可选按固定宽度换行\n\n"
"提示:图片较大时,Base64 字符串会非常长(体积约增加 1/3)。"
)
with gr.Row():
inp = gr.Image(
label="上传图片",
type="filepath", # 关键:回调函数收到的是文件路径字符串
sources=["upload"],
)
with gr.Row():
add_prefix = gr.Checkbox(value=False, label="添加 data URI 前缀(data:image/...;base64,)")
wrap = gr.Checkbox(value=False, label="按固定宽度换行")
width = gr.Slider(
minimum=40,
maximum=200,
value=76,
step=1,
label="换行宽度(wrap width)",
)
with gr.Row():
btn = gr.Button("转换", variant="primary")
clear = gr.Button("清空")
out = gr.Textbox(
label="Base64 输出(可直接复制)",
lines=14,
interactive=False,
placeholder="这里会显示 Base64 字符串……",
)
out_len = gr.Number(label="输出字符数", value=0, interactive=False)
btn.click(
fn=image_to_base64,
inputs=[inp, add_prefix, wrap, width],
outputs=[out, out_len],
)
def _clear():
return None, False, False, 76, "", 0
clear.click(
fn=_clear,
inputs=[],
outputs=[inp, add_prefix, wrap, width, out, out_len],
)
if __name__ == "__main__":
demo.launch(mcp_server=True)