#!/usr/bin/env python3 """ 创建项目模块的占位图片 """ from PIL import Image, ImageDraw, ImageFont import os # 创建输出目录 output_dir = "static/images/projects" os.makedirs(output_dir, exist_ok=True) # 颜色方案 colors = [ ("#6200EE", "#03DAC6"), # Material Design 主色 ("#FF6B6B", "#4ECDC4"), # 红-青 ("#45B7D1", "#96E6A1"), # 蓝-绿 ("#FAD0C4", "#FFD1FF"), # 粉-紫 ("#A1C4FD", "#C2E9FB"), # 蓝-浅蓝 ("#FF9A9E", "#FAD0C4"), # 红-粉 ("#A3BFFA", "#6991C7"), # 蓝-深蓝 ("#FFECD2", "#FCB69F"), # 橙-粉 ] # 项目标题 project_titles = [ "Material Design", "性能优化", "Flutter电商", "Compose组件", "Kotlin协程", "架构设计", "动画效果", "网络请求" ] def create_gradient_image(width, height, color1, color2, text, filename): """创建渐变背景图片""" # 创建新图片 img = Image.new('RGB', (width, height), color1) draw = ImageDraw.Draw(img) # 创建渐变效果 for i in range(height): # 计算当前行的颜色 ratio = i / height r = int(int(color1[1:3], 16) * (1 - ratio) + int(color2[1:3], 16) * ratio) g = int(int(color1[3:5], 16) * (1 - ratio) + int(color2[3:5], 16) * ratio) b = int(int(color1[5:7], 16) * (1 - ratio) + int(color2[5:7], 16) * ratio) # 绘制水平线 draw.line([(0, i), (width, i)], fill=(r, g, b)) # 添加文字 try: # 尝试加载字体 font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 40) except: # 回退到默认字体 font = ImageFont.load_default() # 计算文字位置 bbox = draw.textbbox((0, 0), text, font=font) text_width = bbox[2] - bbox[0] text_height = bbox[3] - bbox[1] x = (width - text_width) // 2 y = (height - text_height) // 2 # 绘制文字阴影 draw.text((x+2, y+2), text, font=font, fill=(0, 0, 0, 128)) # 绘制文字 draw.text((x, y), text, font=font, fill=(255, 255, 255)) # 保存图片 img.save(filename) print(f"创建图片: {filename}") def main(): # 创建项目封面图片 for i in range(8): color1, color2 = colors[i % len(colors)] title = project_titles[i % len(project_titles)] filename = os.path.join(output_dir, f"project{i+1}.png") create_gradient_image(400, 300, color1, color2, title, filename) # 创建默认项目图片 default_img = Image.new('RGB', (400, 300), (245, 245, 245)) draw = ImageDraw.Draw(default_img) # 绘制Material Design风格的图标 draw.rectangle([100, 100, 300, 200], fill=(98, 0, 238), outline=None) draw.rectangle([120, 120, 280, 180], fill=(3, 218, 198), outline=None) # 添加文字 try: font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 30) except: font = ImageFont.load_default() draw.text((150, 220), "玩Android", font=font, fill=(98, 0, 238)) default_img.save(os.path.join(output_dir, "project-default.png")) print(f"创建默认图片: {output_dir}/project-default.png") print("\n所有图片创建完成!") if __name__ == "__main__": main()