拼豆设计生成器(支持大写字母、数字,颜色自定义)
拼豆设计生成器(Perler Bead Pattern Generator)
这是一个用 Python 编写的简易拼豆图案生成工具,可根据输入的英文字母与数字自动生成对应的 5×5 像素化拼豆模板图,并导出为高清 PNG 文件。
运行环境
- Python ≥ 3.7
- 依赖库:
pip install matplotlib numpy
运行后在终端的输入输出:
=== 拼豆设计生成器 ===
请输入要生成的文本:
2025
请输入拼豆颜色(十六进制代码,例如#E08E3F,直接回车使用默认颜色):
#FF4500
正在生成拼豆设计...
设计已保存为 bead_design_2025.png
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle# 定义字母的5x5网格表示
# 1表示放置拼豆,0表示不放置
characters = {# 大写字母'A': [[0, 1, 1, 1, 0],[1, 0, 0, 0, 1],[1, 1, 1, 1, 1],[1, 0, 0, 0, 1],[1, 0, 0, 0, 1]],'B': [[1, 1, 1, 1, 0],[1, 0, 0, 0, 1],[1, 1, 1, 1, 0],[1, 0, 0, 0, 1],[1, 1, 1, 1, 0]],'C': [[0, 1, 1, 1, 0],[1, 0, 0, 0, 1],[1, 0, 0, 0, 0],[1, 0, 0, 0, 1],[0, 1, 1, 1, 0]],'D': [[1, 1, 1, 1, 0],[1, 0, 0, 0, 1],[1, 0, 0, 0, 1],[1, 0, 0, 0, 1],[1, 1, 1, 1, 0]],'E': [[1, 1, 1, 1, 1],[1, 0, 0, 0, 0],[1, 1, 1, 1, 0],[1, 0, 0, 0, 0],[1, 1, 1, 1, 1]],'F': [[1, 1, 1, 1, 1],[1, 0, 0, 0, 0],[1, 1, 1, 1, 0],[1, 0, 0, 0, 0],[1, 0, 0, 0, 0]],'G': [[0, 1, 1, 1, 0],[1, 0, 0, 0, 0],[1, 0, 1, 1, 1],[1, 0, 0, 0, 1],[0, 1, 1, 1, 0]],'H': [[1, 0, 0, 0, 1],[1, 0, 0, 0, 1],[1, 1, 1, 1, 1],[1, 0, 0, 0, 1],[1, 0, 0, 0, 1]],'I': [[1, 1, 1, 1, 1],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, 0],[0, 1, 0, 0, 0]],'8': [[0, 1, 1, 1, 0],[1, 0, 0, 0, 1],[0, 1, 1, 1, 0],[1, 0, 0, 0, 1],[0, 1, 1, 1, 0]],'9': [[0, 1, 1, 1, 0],[1, 0, 0, 0, 1],[0, 1, 1, 1, 1],[0, 0, 0, 0, 1],[0, 1, 1, 1, 0]]}def create_bead_design(text, bead_color='#E08E3F', background_color='#F5F5DC'):"""创建拼豆设计参数:text: 要显示的文本bead_color: 拼豆的颜色 (十六进制代码)background_color: 背景颜色 (十六进制代码)"""# 创建图形fig, ax = plt.subplots(figsize=(max(6, len(text) * 0.6), 3))ax.set_aspect('equal')ax.axis('off')# 设置背景颜色fig.patch.set_facecolor(background_color)def draw_bead(x, y, color=bead_color):"""绘制单个拼豆"""rect = Rectangle((x, y), 0.9, 0.9, facecolor=color, edgecolor='#000000', linewidth=0.5)ax.add_patch(rect)def draw_text(text, start_x, start_y):"""绘制文字"""x_offset = start_xfor char in text:if char in characters:grid = characters[char]for y in range(5):for x in range(5):if grid[4-y][x] == 1: # 反转y轴以使字母正立draw_bead(x_offset + x, start_y + y)x_offset += 6 # 5个格子 + 1个间隔else:# 如果字符未定义,使用空格x_offset += 6# 绘制文本draw_text(text.upper(), 0, 0)# 计算总宽度total_width = len(text) * 6 - 1# 设置坐标轴范围plt.xlim(-1, total_width + 1)plt.ylim(-1, 6)# 添加说明文字plt.tight_layout()return figdef main():"""主函数,获取用户输入并生成拼豆设计"""print("=== 拼豆设计生成器 ===")print("请输入要生成的文本:")text = input().strip()print("请输入拼豆颜色(十六进制代码,例如#E08E3F,直接回车使用默认颜色):")color_input = input().strip()bead_color = color_input if color_input else '#E08E3F'print("正在生成拼豆设计...")# 生成设计fig = create_bead_design(text, bead_color)# 保存图像filename = f"bead_design_{text.replace(' ', '_')}.png"plt.savefig(filename, dpi=300, bbox_inches='tight')print(f"设计已保存为 {filename}")# 显示图像plt.show()if __name__ == "__main__":main()