news 2026/2/10 6:29:54

开发剪纸图案生成器,输入关键词(福,喜)等等,自动生成不同风格的剪纸镂空图案,可直接打印DIY。

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
开发剪纸图案生成器,输入关键词(福,喜)等等,自动生成不同风格的剪纸镂空图案,可直接打印DIY。

我将为您开发一个剪纸图案生成器。这个程序能够根据关键词自动生成不同风格的剪纸图案,并提供打印功能。

项目结构

paper_cutting_generator/

├── main.py

├── generator.py

├── patterns.py

├── styles.py

├── exporter.py

├── config.py

├── templates/

│ ├── basic_shapes.py

│ └── traditional_elements.py

├── output/

└── README.md

1. config.py (配置文件)

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

剪纸图案生成器配置文件

包含样式配置、颜色方案、输出设置等

"""

# 图案样式配置

STYLE_CONFIG = {

"traditional": {

"name": "传统剪纸",

"line_thickness": 2,

"symmetry": "mirror", # mirror, rotational, none

"complexity": "medium",

"corner_style": "rounded",

"border_pattern": "flower",

"fill_pattern": "none" # none, dots, lines, flowers

},

"modern": {

"name": "现代简约",

"line_thickness": 1,

"symmetry": "rotational",

"complexity": "simple",

"corner_style": "sharp",

"border_pattern": "geometric",

"fill_pattern": "dots"

},

"folk": {

"name": "民俗风格",

"line_thickness": 3,

"symmetry": "mirror",

"complexity": "high",

"corner_style": "decorative",

"border_pattern": "cloud",

"fill_pattern": "lines"

},

"children": {

"name": "儿童趣味",

"line_thickness": 4,

"symmetry": "none",

"complexity": "very_simple",

"corner_style": "rounded",

"border_pattern": "stars",

"fill_pattern": "stars"

}

}

# 关键词映射配置

KEYWORD_MAPPINGS = {

# 基础汉字

"福": {"elements": ["bat", "fu_character", "coins"], "style": "traditional"},

"喜": {"elements": ["butterfly", "double_happiness", "flowers"], "style": "traditional"},

"春": {"elements": ["spring_character", "flowers", "birds"], "style": "folk"},

"寿": {"elements": ["peach", "crane", "pine"], "style": "traditional"},

"财": {"elements": ["gold_ingots", "coins", "rat"], "style": "folk"},

"龙": {"elements": ["dragon", "clouds", "pearl"], "style": "traditional"},

"凤": {"elements": ["phoenix", "phoenix_feathers", "fire"], "style": "traditional"},

"鱼": {"elements": ["fish", "water", "lotus"], "style": "folk"},

"花": {"elements": ["chrysanthemum", "peony", "lotus"], "style": "folk"},

"鸟": {"elements": ["magpie", "crane", "phoenix"], "style": "folk"},

# 祝福词汇

"恭喜发财": {"elements": ["coins", "gold_ingots", "rat", "fu_character"], "style": "folk"},

"年年有余": {"elements": ["fish", "lotus", "water"], "style": "folk"},

"花开富贵": {"elements": ["peony", "flowers", "gold_ingots"], "style": "traditional"},

"龙凤呈祥": {"elements": ["dragon", "phoenix", "clouds"], "style": "traditional"},

"福寿安康": {"elements": ["bat", "peach", "crane", "fu_character"], "style": "traditional"},

# 英文关键词

"fortune": {"elements": ["coins", "gold_ingots", "bat"], "style": "folk"},

"happiness": {"elements": ["butterfly", "flowers", "butterflies"], "style": "folk"},

"love": {"elements": ["double_happiness", "butterfly", "heart"], "style": "modern"},

"prosperity": {"elements": ["dragon", "phoenix", "gold_ingots"], "style": "traditional"}

}

# 颜色方案配置

COLOR_SCHEMES = {

"traditional_red": {

"name": "传统红",

"background": (255, 255, 255), # 白色背景

"foreground": (220, 20, 60), # 红色前景

"accent": (255, 215, 0) # 金色点缀

},

"modern_black": {

"name": "现代黑",

"background": (255, 255, 255), # 白色背景

"foreground": (0, 0, 0), # 黑色前景

"accent": (128, 128, 128) # 灰色点缀

},

"folk_multicolor": {

"name": "民俗彩",

"background": (255, 248, 220), # 米色背景

"foreground": (255, 105, 180), # 粉红色

"accent": (50, 205, 50) # 青绿色

},

"children_colorful": {

"name": "童趣彩",

"background": (240, 248, 255), # 浅蓝背景

"foreground": (255, 165, 0), # 橙色

"accent": (138, 43, 226) # 紫色

}

}

# 输出配置

OUTPUT_CONFIG = {

"default_size": (800, 600),

"dpi": 300,

"file_formats": ["png", "svg", "pdf"],

"paper_sizes": ["A4", "A3", "letter"],

"margin": 50, # 页边距

"grid_layout": True, # 是否网格布局

"patterns_per_page": 4

}

# 剪纸规则配置

PAPER_CUTTING_RULES = {

"min_line_length": 5, # 最小线条长度

"max_line_length": 200, # 最大线条长度

"min_area": 100, # 最小封闭区域面积

"connection_rules": True, # 连接规则

"balance_check": True, # 平衡性检查

"cultural_considerations": True # 文化考量

}

2. patterns.py (图案元素模块)

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

剪纸图案元素模块

定义各种传统剪纸元素和基本形状

"""

import math

import random

from typing import List, Tuple, Dict, Any

from abc import ABC, abstractmethod

class PatternElement(ABC):

"""图案元素基类"""

def __init__(self, name: str, style: str = "traditional"):

self.name = name

self.style = style

self.properties = {}

self.control_points = []

self.bounding_box = (0, 0, 0, 0)

@abstractmethod

def generate_path(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成图案路径"""

pass

@abstractmethod

def get_control_points(self) -> List[Tuple[float, float]]:

"""获取控制点"""

pass

def set_property(self, key: str, value: Any):

"""设置属性"""

self.properties[key] = value

def get_property(self, key: str, default=None):

"""获取属性"""

return self.properties.get(key, default)

class BasicShape(PatternElement):

"""基础几何形状"""

def __init__(self, name: str, shape_type: str):

super().__init__(name, "basic")

self.shape_type = shape_type

self.dimensions = {}

def generate_path(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

if self.shape_type == "circle":

return self._generate_circle(x, y, size)

elif self.shape_type == "square":

return self._generate_square(x, y, size)

elif self.shape_type == "triangle":

return self._generate_triangle(x, y, size)

elif self.shape_type == "diamond":

return self._generate_diamond(x, y, size)

elif self.shape_type == "oval":

return self._generate_oval(x, y, size)

else:

return self._generate_circle(x, y, size)

def _generate_circle(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成圆形路径"""

points = []

segments = 32

radius = size / 2

for i in range(segments):

angle = 2 * math.pi * i / segments

px = x + radius * math.cos(angle)

py = y + radius * math.sin(angle)

points.append((px, py))

return points

def _generate_square(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成正方形路径"""

half = size / 2

return [

(x - half, y - half),

(x + half, y - half),

(x + half, y + half),

(x - half, y + half),

(x - half, y - half)

]

def _generate_triangle(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成三角形路径"""

height = size * math.sqrt(3) / 2

half_base = size / 2

return [

(x, y - height/2), # 顶点

(x - half_base, y + height/2), # 左下

(x + half_base, y + height/2), # 右下

(x, y - height/2) # 回到顶点

]

def _generate_diamond(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成菱形路径"""

half = size / 2

return [

(x, y - half), # 上

(x + half, y), # 右

(x, y + half), # 下

(x - half, y), # 左

(x, y - half) # 回到上

]

def _generate_oval(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成椭圆形路径"""

points = []

segments = 32

radius_x = size / 2

radius_y = size / 3

for i in range(segments):

angle = 2 * math.pi * i / segments

px = x + radius_x * math.cos(angle)

py = y + radius_y * math.sin(angle)

points.append((px, py))

return points

def get_control_points(self) -> List[Tuple[float, float]]:

return self.control_points

class TraditionalElement(PatternElement):

"""传统剪纸元素"""

def __init__(self, name: str, element_type: str):

super().__init__(name, "traditional")

self.element_type = element_type

self.cultural_meaning = ""

self.symbolism = ""

def generate_path(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

if self.element_type == "bat":

return self._generate_bat(x, y, size)

elif self.element_type == "butterfly":

return self._generate_butterfly(x, y, size)

elif self.element_type == "dragon":

return self._generate_dragon(x, y, size)

elif self.element_type == "phoenix":

return self._generate_phoenix(x, y, size)

elif self.element_type == "fish":

return self._generate_fish(x, y, size)

elif self.element_type == "flower":

return self._generate_flower(x, y, size)

elif self.element_type == "fu_character":

return self._generate_fu_character(x, y, size)

elif self.element_type == "double_happiness":

return self._generate_double_happiness(x, y, size)

else:

return self._generate_generic_element(x, y, size)

def _generate_bat(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成蝙蝠图案(寓意福气)"""

points = []

wing_span = size

body_length = size * 0.6

# 身体

body_points = [

(x, y - body_length/2),

(x - size/6, y + body_length/2),

(x + size/6, y + body_length/2),

(x, y - body_length/2)

]

# 翅膀

wing_left = [

(x - size/6, y - body_length/4),

(x - wing_span/2, y - body_length/2),

(x - wing_span/2, y + body_length/4),

(x - size/6, y + body_length/4)

]

wing_right = [

(x + size/6, y - body_length/4),

(x + wing_span/2, y - body_length/2),

(x + wing_span/2, y + body_length/4),

(x + size/6, y + body_length/4)

]

points.extend(body_points)

points.extend(wing_left)

points.extend(wing_right)

return points

def _generate_butterfly(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成蝴蝶图案(寓意变化、美丽)"""

points = []

wing_size = size / 2

# 上翅膀

upper_wing_left = [

(x, y - size/4),

(x - wing_size, y - size/2),

(x - wing_size/2, y - size/8),

(x, y - size/4)

]

upper_wing_right = [

(x, y - size/4),

(x + wing_size, y - size/2),

(x + wing_size/2, y - size/8),

(x, y - size/4)

]

# 下翅膀

lower_wing_left = [

(x, y + size/4),

(x - wing_size*0.8, y + size/4),

(x - wing_size/2, y + size/8),

(x, y + size/4)

]

lower_wing_right = [

(x, y + size/4),

(x + wing_size*0.8, y + size/4),

(x + wing_size/2, y + size/8),

(x, y + size/4)

]

# 身体

body = [

(x, y - size/2),

(x, y + size/2)

]

points.extend(upper_wing_left)

points.extend(upper_wing_right)

points.extend(lower_wing_left)

points.extend(lower_wing_right)

points.extend(body)

return points

def _generate_dragon(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成龙图案(寓意权威、吉祥)"""

points = []

segments = 8

# 龙头

head_points = [

(x, y - size/2),

(x - size/4, y - size/4),

(x + size/4, y - size/4),

(x, y - size/2)

]

points.extend(head_points)

# 龙身(弯曲的线条)

for i in range(segments):

angle = math.pi * i / segments

px = x + size/3 * math.cos(angle)

py = y - size/4 + size/6 * math.sin(angle)

points.append((px, py))

# 龙尾

tail_points = [

(x + size/3, y + size/6),

(x + size/4, y + size/3),

(x + size/6, y + size/4),

(x + size/3, y + size/6)

]

points.extend(tail_points)

return points

def _generate_phoenix(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成凤凰图案(寓意重生、高贵)"""

points = []

# 凤头

head = [

(x, y - size/2),

(x - size/6, y - size/3),

(x + size/6, y - size/3),

(x, y - size/2)

]

# 凤冠

crown = [

(x - size/8, y - size/2),

(x - size/4, y - size/2 - size/8),

(x + size/4, y - size/2 - size/8),

(x + size/8, y - size/2)

]

# 凤羽(扇形)

for i in range(5):

angle = math.pi/6 + i * math.pi/6

inner_radius = size/3

outer_radius = size/2

px1 = x + inner_radius * math.cos(angle)

py1 = y - size/3 + inner_radius * math.sin(angle)

px2 = x + outer_radius * math.cos(angle)

py2 = y - size/3 + outer_radius * math.sin(angle)

points.append((px1, py1))

points.append((px2, py2))

points.extend(head)

points.extend(crown)

return points

def _generate_fish(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成鱼图案(寓意年年有余)"""

points = []

# 鱼身(椭圆形)

body_segments = 16

body_width = size * 0.6

body_height = size * 0.4

for i in range(body_segments):

angle = 2 * math.pi * i / body_segments

px = x + body_width/2 * math.cos(angle)

py = y + body_height/2 * math.sin(angle)

points.append((px, py))

# 鱼尾

tail_points = [

(x + body_width/2, y),

(x + body_width, y - body_height/2),

(x + body_width, y + body_height/2),

(x + body_width/2, y)

]

# 鱼鳍

fin_top = [

(x, y - body_height/2),

(x - size/4, y - body_height),

(x + size/4, y - body_height/2)

]

fin_bottom = [

(x, y + body_height/2),

(x - size/4, y + body_height),

(x + size/4, y + body_height/2)

]

points.extend(tail_points)

points.extend(fin_top)

points.extend(fin_bottom)

return points

def _generate_flower(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成花朵图案"""

points = []

petals = 5

petal_length = size / 2

# 花瓣

for i in range(petals):

angle = 2 * math.pi * i / petals

# 外弧

outer_x = x + petal_length * math.cos(angle)

outer_y = y + petal_length * math.sin(angle)

# 内弧

inner_x = x + petal_length * 0.3 * math.cos(angle)

inner_y = y + petal_length * 0.3 * math.sin(angle)

# 花瓣形状

petal_points = [

(inner_x, inner_y),

(outer_x, outer_y),

(inner_x + petal_length * 0.2 * math.cos(angle + 0.5),

inner_y + petal_length * 0.2 * math.sin(angle + 0.5)),

(inner_x, inner_y)

]

points.extend(petal_points)

# 花心

center_points = [

(x, y - size/8),

(x + size/8, y),

(x, y + size/8),

(x - size/8, y),

(x, y - size/8)

]

points.extend(center_points)

return points

def _generate_fu_character(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成福字图案(简化版)"""

# 这是一个简化的福字剪纸图案

thickness = size / 10

points = []

# 外框

outer_rect = [

(x - size/2, y - size/2),

(x + size/2, y - size/2),

(x + size/2, y + size/2),

(x - size/2, y + size/2),

(x - size/2, y - size/2)

]

# 内部的福字结构(极简版)

# 左边竖

left_vertical = [

(x - size/4, y - size/3),

(x - size/4 + thickness, y - size/3),

(x - size/4 + thickness, y + size/3),

(x - size/4, y + size/3),

(x - size/4, y - size/3)

]

# 右边结构

right_structure = [

(x + size/4, y - size/3),

(x + size/4 - thickness, y - size/3),

(x + size/4 - thickness, y - size/6),

(x + size/2 - thickness, y - size/6),

(x + size/2 - thickness, y + size/6),

(x + size/4 - thickness, y + size/6),

(x + size/4 - thickness, y + size/3),

(x + size/4, y + size/3),

(x + size/4, y - size/3)

]

# 中间横线

middle_line = [

(x - size/4, y),

(x + size/4, y),

(x + size/4, y + thickness),

(x - size/4, y + thickness),

(x - size/4, y)

]

points.extend(outer_rect)

points.extend(left_vertical)

points.extend(right_structure)

points.extend(middle_line)

return points

def _generate_double_happiness(self, x: float, y: float, size: float) -> List[Tuple[float, float]]:

"""生成双喜字图案"""

# 简化的双喜字,由两个喜字组成

single_size = size / 2

thickness = single_size / 8

points = []

# 第一个喜字(左侧)

left_x = x - single_size / 4

left_points = self._generate_single_xi(left_x, y, single_size, thickness)

# 第二个喜字(右侧)

right_x = x + single_size / 4

right_points = self._generate_single_xi(right_x, y, single_size, thickness)

points.extend(left_points)

points.extend(right_points)

return points

def _generate_single_xi(self, x: float, y: float, size: float, thickness: float) -> List[Tuple[float, float]]:

"""生成单个喜字"""

points = []

# 喜字的上半部分

top_part = [

(x - size/3, y - size/4),

(x + size/3, y - size/4),

(x + size/3, y - size/4 + thickness),

(x + size/6, y - size/4 + thickness),

(x + size/6, y + size/4 - thickness),

(x + size/3, y + size/4 - thickness),

(x + size/3, y + size/4),

(x - size/3, y + size/4),

(x - size/3, y + size/4 - thickness),

(x - size/6, y + size/4 - thickness),

(x - size/6, y - size/4 + thickness),

(x - size/3, y - size/4 + thickness),

(x - size/3, y - size/4)

]

# 喜字的下半部分(镜像)

bottom_part = [

(x - size/3, y + size/4),

(x + size/3, y + size/4),

(x + size/3, y + size/4 + thickness),

(x + size/6, y + size/4 + thickness),

(x + size/6, y + size/3),

(x + size/3, y + size/3),

(x + size/3, y + size/3 + thickness),

(x - size/3, y + size/3 + thickness),

(x - size/

关注我,有更多实用程序等着你!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/6 23:50:02

C#企业级模块划分实战指南(99%工程师忽略的关键设计点)

第一章:C#企业级模块划分的核心理念在构建大型C#应用程序时,合理的模块划分是确保系统可维护性、可扩展性和团队协作效率的关键。良好的模块设计不仅能够降低代码耦合度,还能提升单元测试的覆盖率和部署的灵活性。关注点分离 将系统按业务功能…

作者头像 李华
网站建设 2026/2/5 10:16:14

健身房会员卡识别:新用户注册时快速导入旧卡信息

健身房会员卡识别:新用户注册时快速导入旧卡信息 在健身房前台,一位刚搬来本地的会员正准备注册新账户。他掏出一张略显磨损的旧会员卡,工作人员接过卡片、打开系统、准备手动录入信息——姓名、手机号、卡号、有效期……不到十个字段&#x…

作者头像 李华
网站建设 2026/2/3 3:53:04

校园安全管理:学生出入登记表OCR识别留存电子档案

校园安全管理:学生出入登记表OCR识别留存电子档案 在一所普通中学的门卫室里,每天清晨和傍晚总能看到这样一幕:值班老师戴着老花镜,低头翻看一张张字迹各异的纸质《学生出入登记表》,然后手动将“张三、高三&#xff0…

作者头像 李华
网站建设 2026/2/5 0:03:30

盲人辅助阅读:手机拍摄书籍页面实时语音朗读OCR结果

盲人辅助阅读:手机拍摄书籍页面实时语音朗读OCR结果 在一间安静的图书馆里,一位视障学生举起手机,对准摊开的物理教材轻轻一拍。不到三秒后,耳机中传来清晰的人声:“麦克斯韦方程组描述了电场与磁场之间的关系……”没…

作者头像 李华
网站建设 2026/2/4 2:21:48

java计算机毕业设计学术团队资源管理系统 高校科研协作与资产一体化平台 基于SpringBoot的学术团队协同与资源共享系统

计算机毕业设计学术团队资源管理系统360369(配套有源码 程序 mysql数据库 论文) 本套源码可以在文本联xi,先看具体系统功能演示视频领取,可分享源码参考。在“双一流”建设背景下,科研资源的碎片化、信息孤岛化已成为制约高校学术…

作者头像 李华
网站建设 2026/2/3 23:31:29

【架构师亲授】:C# 12顶级语句在微服务项目中的高级用法

第一章:C# 12顶级语句概述与微服务架构融合趋势C# 12 引入的顶级语句(Top-level statements)进一步简化了应用程序的入口点定义,使开发者能够以更简洁的方式编写可执行代码,尤其适用于轻量级服务和微服务场景。在传统 …

作者头像 李华