from PIL import Image, ImageDraw, ImageFont
import os

class AncientStudentIDCard:
    def __init__(self, width=600, height=900):
        self.width = width
        self.height = height
        self.card = Image.new("RGB", (width, height), "#f5e8c8")
        self.draw = ImageDraw.Draw(self.card)

    def set_font(self, size=40):
        try:
            return ImageFont.truetype("simsun.ttc", size)
        except:
            try:
                return ImageFont.truetype("STHUPO.TTF", size)
            except:
                return ImageFont.load_default(size=size)

    def draw_background(self):
        bg_color = "#f5e8c8"
        self.card = Image.new("RGB", (self.width, self.height), bg_color)
        self.draw = ImageDraw.Draw(self.card)

    def draw_border(self):
        border_color = "#8b4513"
        self.draw.rectangle(
            [20, 20, self.width - 20, self.height - 20],
            outline=border_color,
            width=3
        )
        corner_size = 60
        self.draw.line([20, 20, 20, 20 + corner_size], fill=border_color, width=4)
        self.draw.line([20, 20, 20 + corner_size, 20], fill=border_color, width=4)
        self.draw.line([self.width - 20, 20, self.width - 20, 20 + corner_size], fill=border_color, width=4)
        self.draw.line([self.width - 20, 20, self.width - 20 - corner_size, 20], fill=border_color, width=4)
        self.draw.line([20, self.height - 20, 20, self.height - 20 - corner_size], fill=border_color, width=4)
        self.draw.line([20, self.height - 20, 20 + corner_size, self.height - 20], fill=border_color, width=4)
        self.draw.line([self.width - 20, self.height - 20, self.width - 20, self.height - 20 - corner_size], fill=border_color, width=4)
        self.draw.line([self.width - 20, self.height - 20, self.width - 20 - corner_size, self.height - 20], fill=border_color, width=4)

    def draw_title(self, title="古风学堂学生证"):
        font = self.set_font(48)
        bbox = self.draw.textbbox((0, 0), title, font=font)
        text_w = bbox[2] - bbox[0]
        x = (self.width - text_w) // 2
        self.draw.text((x, 40), title, font=font, fill="#5a2e0a")

    def draw_default_avatar(self, size=180):
        ax = (self.width - size) // 2
        ay = 120
        self.draw.ellipse([ax, ay, ax + size, ay + size], fill="#e8d9bc", outline="#8b4513", width=4)
        font = self.set_font(26)
        text = "头像"
        bbox = self.draw.textbbox((0, 0), text, font=font)
        tw = bbox[2] - bbox[0]
        th = bbox[3] - bbox[1]
        tx = ax + (size - tw) // 2
        ty = ay + (size - th) // 2
        self.draw.text((tx, ty), text, font=font, fill="#8b4513")

    def draw_info(self, info_dict):
        font = self.set_font(28)
        start_y = 350
        line_h = 50
        for k, v in info_dict.items():
            text = f"{k}：{v}"
            bbox = self.draw.textbbox((0, 0), text, font=font)
            tw = bbox[2] - bbox[0]
            x = (self.width - tw) // 2
            self.draw.text((x, start_y), text, font=font, fill="#3a1f08")
            start_y += line_h

    def draw_seal(self, text="学堂认证"):
        seal_size = 100
        sx = self.width - seal_size - 40
        sy = self.height - seal_size - 40
        self.draw.rectangle([sx, sy, sx + seal_size, sy + seal_size], outline="#b22222", width=5)
        font = self.set_font(20)
        bbox = self.draw.textbbox((0, 0), text, font=font)
        tw = bbox[2] - bbox[0]
        th = bbox[3] - bbox[1]
        tx = sx + (seal_size - tw) // 2
        ty = sy + (seal_size - th) // 2
        self.draw.text((tx, ty), text, font=font, fill="#b22222")

    # 移除保存文件功能，仅展示窗口
    def show(self):
        self.card.show()

# ==================== 主程序【仅窗口展示、不保存文件】 ====================
if __name__ == "__main__":
    try:
        card = AncientStudentIDCard()
        card.draw_background()
        card.draw_border()
        card.draw_title("古风学堂学生证")
        card.draw_default_avatar()

        # 学生信息可自行修改
        student_info = {
            "姓名": "林婉儿",
            "学号": "G20260719",
            "班级": "国风一班",
            "院系": "古典文学院",
            "入学年份": "2026年"
        }

        card.draw_info(student_info)
        card.draw_seal("国风书院")
        # 只弹出图片窗口，不生成任何本地文件
        card.show()
        print("✅ 古风学生证窗口已打开，无本地文件保存！")

    except Exception as e:
        print("❌ 程序出错：", e)

    # 防闪退暂停
    os.system("pause")
       