ILRDF-Lowking commited on
Commit
9a7e197
·
verified ·
1 Parent(s): f5f799e

Update visual_renderer.py

Browse files
Files changed (1) hide show
  1. visual_renderer.py +18 -13
visual_renderer.py CHANGED
@@ -8,21 +8,24 @@ def draw_glossing_card(data, output_path):
8
  data 包含: line1, line2, line3, translation
9
  """
10
  # 畫布設定
11
- width, height = 1200, 600
12
  bg_color = (253, 248, 241) # 米白色紙張感
13
- img = Image.Image.new('RGB', (width, height), color=bg_color)
 
 
14
  draw = ImageDraw.Draw(img)
15
 
16
- # 💡 請確保您的 Hugging Face 空間裡一個字型檔 (如 NotoSansTC-Regular.ttf)
17
  font_path = "NotoSansTC-Regular.ttf"
18
  font_size = 32
19
  try:
20
  font = ImageFont.truetype(font_path, font_size)
21
- except:
 
22
  font = ImageFont.load_default()
23
 
24
  x_offset = 80
25
- y_start = 120
26
  line_spacing = 60 # 四行等距
27
 
28
  # 畫出前三行 (對齊單詞)
@@ -32,17 +35,19 @@ def draw_glossing_card(data, output_path):
32
  w3 = data['glossing']['line3'][i]
33
 
34
  draw.text((x_offset, y_start), w1, fill=(0,0,0), font=font)
35
- draw.text((x_offset, y_start + line_spacing), w2, fill=(139,0,0), font=font)
36
- draw.text((x_offset, y_start + line_spacing * 2), w3, fill=(85,85,85), font=font)
37
 
38
  # 計算下一個單詞的間距 (取三行中最長者 + 緩衝)
39
- max_w = max(draw.textbbox((0,0), w1, font=font)[2],
40
- draw.textbbox((0,0), w2, font=font)[2],
41
- draw.textbbox((0,0), w3, font=font)[2])
42
- x_offset += max_w + 40
 
 
43
 
44
- # 畫出第四行 (整句意譯) - 無橫線,等距
45
- draw.text((80, y_start + line_spacing * 4), data['translation'], fill=(0,0,0), font=font)
46
 
47
  img.save(output_path)
48
  return output_path
 
8
  data 包含: line1, line2, line3, translation
9
  """
10
  # 畫布設定
11
+ width, height = 1200, 450 # 💡 高度微調,讓版面更緊湊
12
  bg_color = (253, 248, 241) # 米白色紙張感
13
+
14
+ # 💡 修正錯誤:使用 Image.new 而不是 Image.Image.new
15
+ img = Image.new('RGB', (width, height), color=bg_color)
16
  draw = ImageDraw.Draw(img)
17
 
18
+ # 💡 字體設定 (強烈建議要中文字型檔,否則中文會變方塊)
19
  font_path = "NotoSansTC-Regular.ttf"
20
  font_size = 32
21
  try:
22
  font = ImageFont.truetype(font_path, font_size)
23
+ except Exception as e:
24
+ print(f"⚠️ 找不到中文字型或載入失敗: {e}")
25
  font = ImageFont.load_default()
26
 
27
  x_offset = 80
28
+ y_start = 80
29
  line_spacing = 60 # 四行等距
30
 
31
  # 畫出前三行 (對齊單詞)
 
35
  w3 = data['glossing']['line3'][i]
36
 
37
  draw.text((x_offset, y_start), w1, fill=(0,0,0), font=font)
38
+ draw.text((x_offset, y_start + line_spacing), w2, fill=(139,0,0), font=font) # 暗紅色
39
+ draw.text((x_offset, y_start + line_spacing * 2), w3, fill=(85,85,85), font=font) # 深灰色
40
 
41
  # 計算下一個單詞的間距 (取三行中最長者 + 緩衝)
42
+ bbox1 = draw.textbbox((0,0), w1, font=font)
43
+ bbox2 = draw.textbbox((0,0), w2, font=font)
44
+ bbox3 = draw.textbbox((0,0), w3, font=font)
45
+
46
+ max_w = max(bbox1[2] - bbox1[0], bbox2[2] - bbox2[0], bbox3[2] - bbox3[0])
47
+ x_offset += max_w + 40 # 單詞之間的間距
48
 
49
+ # 畫出第四行 (整句意譯) - 無橫線,等距 (拉開半行的距離區隔)
50
+ draw.text((80, y_start + line_spacing * 3.5), data['translation'], fill=(0,0,0), font=font)
51
 
52
  img.save(output_path)
53
  return output_path