overtime7819 commited on
Commit
fed85b9
·
verified ·
1 Parent(s): 10380bf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ import csv, io
4
+
5
+ def generate_techflow_table(csv_file, background_image, font_size, row_height):
6
+ # 1. 载入背景图
7
+ bg = Image.open(background_image).convert("RGBA")
8
+ bg_w, bg_h = bg.size
9
+ draw = ImageDraw.Draw(bg)
10
+
11
+ # 2. 加载字体
12
+ try:
13
+ font_title = ImageFont.truetype("PingFang.ttc", font_size + 6)
14
+ font_body = ImageFont.truetype("PingFang.ttc", font_size)
15
+ except:
16
+ font_title = ImageFont.load_default()
17
+ font_body = ImageFont.load_default()
18
+
19
+ # 3. 读取 CSV 内容
20
+ csv_data = csv_file.decode('utf-8').splitlines()
21
+ reader = csv.reader(csv_data)
22
+ rows = list(reader)
23
+ headers = rows[0]
24
+ data = rows[1:]
25
+
26
+ # 4. 表格布局参数
27
+ start_x, start_y = 100, 230
28
+ col_spacing = 60
29
+
30
+ # 计算列宽
31
+ col_widths = []
32
+ for c in range(len(headers)):
33
+ max_width = max(draw.textlength(r[c], font=font_body) for r in rows)
34
+ col_widths.append(max_width + col_spacing)
35
+
36
+ # 5. 绘制标题
37
+ title_text = "隐私币近期表现"
38
+ title_font = ImageFont.truetype("PingFang.ttc", font_size + 10)
39
+ draw.text((start_x, 130), title_text, font=title_font, fill="white")
40
+
41
+ # 6. 绘制表头背景条
42
+ header_y = start_y
43
+ table_width = sum(col_widths)
44
+ draw.rectangle(
45
+ [start_x - 20, header_y - 10, start_x + table_width, header_y + row_height],
46
+ fill=(30, 50, 120, 200)
47
+ )
48
+
49
+ # 7. 绘制表头
50
+ for i, text in enumerate(headers):
51
+ x = start_x + sum(col_widths[:i])
52
+ draw.text((x, header_y), text, font=font_body, fill="white")
53
+
54
+ # 8. 绘制表格内容
55
+ for r, row in enumerate(data):
56
+ y = start_y + (r + 1) * row_height
57
+ for c, text in enumerate(row):
58
+ x = start_x + sum(col_widths[:c])
59
+ draw.text((x, y), text, font=font_body, fill="white")
60
+
61
+ # 9. 绘制表格边界线(可选)
62
+ y_end = start_y + (len(data) + 1) * row_height
63
+ draw.line([start_x - 20, y_end, start_x + table_width, y_end], fill=(200, 200, 200, 100), width=2)
64
+
65
+ # 10. 加上 logo(右下角)
66
+ try:
67
+ logo = Image.open("logo.png").convert("RGBA")
68
+ logo_w, logo_h = logo.size
69
+ scale = 0.25
70
+ logo = logo.resize((int(logo_w * scale), int(logo_h * scale)))
71
+ pos = (bg_w - logo.width - 80, bg_h - logo.height - 60)
72
+ bg.paste(logo, pos, logo)
73
+ except:
74
+ pass # 如果没有 logo 文件则忽略
75
+
76
+ # 11. 输出图片
77
+ output = io.BytesIO()
78
+ bg.save(output, format="PNG")
79
+ output.seek(0)
80
+ return output
81
+
82
+ # Gradio 界面
83
+ demo = gr.Interface(
84
+ fn=generate_techflow_table,
85
+ inputs=[
86
+ gr.File(label="上传 CSV 文件(UTF-8)"),
87
+ gr.Image(label="背景图", type="filepath", value="background.png"),
88
+ gr.Slider(20, 60, value=36, step=2, label="字体大小"),
89
+ gr.Slider(60, 140, value=100, step=5, label="行距"),
90
+ ],
91
+ outputs=gr.Image(label="生成结果", type="pil"),
92
+ title="📊 TechFlow 深潮风格表格生成器",
93
+ description="上传 CSV 文件,自动生成品牌风格的表格图片(背景与字体自动排版)"
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()