🧧 籤詩產生器:Python 程式碼分享
本程式使用 Python + PIL 套件,搭配標楷體字型與傳統背景圖片,實現個人化籤詩圖片生成,支援直書排列、紅圈標註、輸入四句七言詩等功能。
✨ 功能說明
- 直式排列 7x4 籤詩字
- 主、副標題可自訂
- 支援紅圈標註(如:罷、免、糞、埽)
- 自動對齊每格文字,圖片自動儲存
💻 Python 程式碼
# === 掛載 Google Drive ===
from google.colab import drive
import os
from PIL import Image, ImageDraw, ImageFont
drive.mount('/content/drive')
# === 使用者輸入 ===
print("請輸入籤詩內容與參數:")
user_input = input("請依序輸入主標題、副標題,接著是28字詩句(四句,每句七字),逗號分隔(如:大罷免,大成宮,投共立委毋是款,扶挺中國真齊全,全民罷免鎮叛亂,團結一致守台灣): ")
parts = user_input.strip().split(',')
header1 = parts[0] if len(parts) > 0 else ""
header2 = parts[1] if len(parts) > 1 else ""
# === 處理四句七言詩句 ===
lines = parts[2:6] if len(parts) >= 6 else [" "*7]*4
lines = [line.replace("\n", "").replace(" ", "") for line in lines]
lines = [line.ljust(7, " ")[:7] for line in lines] # 每行剛好七字,不足補全,多餘截斷
# 七列四欄(直書排列)
columns = [[line[row] for line in lines] for row in range(7)]
highlight_chars = input("請輸入要加紅圈的字,用逗號分隔(如:罷,免,投,共,立,委):").strip().split(",")
output_filename = input("請輸入輸出圖檔檔名(如:my_qian.png):").strip()
if not output_filename.endswith(".png"):
output_filename += ".png"
# === 檔案與資源路徑設定 ===
base_path = "/content/drive/MyDrive/Colab Notebooks"
bg_path = os.path.join(base_path, "background.png")
font_path = os.path.join(base_path, "kaiu.ttf")
output_path = os.path.join(base_path, output_filename)
# === 驗證資源存在 ===
assert os.path.exists(bg_path), f"背景圖不存在:{bg_path}"
assert os.path.exists(font_path), f"字型檔不存在:{font_path}"
# === 開啟背景圖與畫布 ===
image = Image.open(bg_path).convert("RGB")
draw = ImageDraw.Draw(image)
W, H = image.size
# === 載入字型 ===
def load_font(size):
return ImageFont.truetype(font_path, size)
# === 格式與樣式設定 ===
n_rows, n_cols = 7, 4 # 七列、四欄(每列四字)
text_color = (0, 0, 0)
highlight_color = (255, 0, 0)
header1_fontsize = 180
header2_fontsize = 160
header1_y = 100
header2_y = 300
margin_top = 500 # more to down
cell_height = 135
column_x = [250, 415, 580, 745] # 四欄固定 x 座標
column_fontsizes = [75] * n_cols
circle_offset_x = 0
circle_offset_y = -5
circle_padding = 10
# === 畫主標題與副標題 ===
font_h1 = load_font(header1_fontsize)
bbox1 = draw.textbbox((0, 0), header1, font=font_h1)
draw.text(((W - (bbox1[2] - bbox1[0])) / 2, header1_y), header1, font=font_h1, fill=text_color)
font_h2 = load_font(header2_fontsize)
bbox2 = draw.textbbox((0, 0), header2, font=font_h2)
draw.text(((W - (bbox2[2] - bbox2[0])) / 2, header2_y), header2, font=font_h2, fill=text_color)
# === 畫籤詩內容與紅圈 ===
for row in range(n_rows):
font = load_font(column_fontsizes[0]) # 假設四欄字型大小一致
for col in range(n_cols):
char = columns[row][col]
x_center = column_x[col]
y_center = margin_top + row * cell_height + cell_height // 2
bbox = font.getbbox(char)
text_w = bbox[2] - bbox[0]
text_h = bbox[3] - bbox[1]
if char in highlight_chars:
radius = max(text_w, text_h) // 2 + circle_padding
draw.ellipse([
(x_center + circle_offset_x - radius, y_center + circle_offset_y - radius),
(x_center + circle_offset_x + radius, y_center + circle_offset_y + radius)
], outline=highlight_color, width=4)
draw.text((x_center, y_center), char, font=font, fill=text_color, anchor="mm")
# === 儲存與顯示 ===
image.save(output_path)
image.show()
print("✅ 產圖完成,已儲存於:", output_path)
📌 注意
本程式需先上傳背景圖(background.png)與標楷體(kaiu.ttf)至 Google Drive 並指定正確路徑。Colab 環境需安裝 Pillow 套件(已內建)。