news 2026/5/3 18:10:04

开发剪纸图案生成器,输入关键词(福,喜)等等,自动生成不同风格的剪纸镂空图案,可直接打印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/4/28 23:31:49

UltraISO注册码最新版不可靠?推荐使用开源OCR替代商业软件

拥抱开源OCR:为何应放弃非法注册码,转向本地化智能识别 在企业数字化转型加速的今天,一份纸质发票、一张身份证照片、一段视频字幕,都可能成为信息流转的关键节点。如何高效、安全地将这些图像中的文字提取出来?这早已…

作者头像 李华
网站建设 2026/4/19 15:54:41

海洋科考船日志:航海手稿OCR识别保存珍贵历史资料

海洋科考船日志:航海手稿OCR识别保存珍贵历史资料 在国家海洋博物馆的恒温档案室里,一摞泛黄的航海日志静静躺在防光盒中。这些来自上世纪50年代“东方红号”科考船的手写记录,字迹已被岁月晕染成模糊的墨团,纸张边缘布满虫蛀孔洞…

作者头像 李华
网站建设 2026/4/30 20:08:52

在AI技术唾手可得的时代,真正的难点在于挖掘新需求——某知名AI开发平台用户需求深度解析

a. 内容描述核心功能定位: 该项目是一个集成的AI开发平台,主要作为统一的API网关和开发工具集合。它致力于简化AI应用的开发流程,为开发者和企业提供一个管理多AI提供商模型访问、请求追踪和项目协作的中心化解决方案。其定位类似于应用商店中主流的开发…

作者头像 李华
网站建设 2026/4/27 20:53:59

建筑图纸信息提取:施工图中标注文字识别与BIM系统对接

建筑图纸信息提取:施工图中标注文字识别与BIM系统对接 在大型建筑项目中,工程师面对的往往不是一张干净整洁的数字模型,而是一摞摞泛黄的纸质施工图——上面布满手写批注、模糊的尺寸标注和密密麻麻的构件编号。这些图纸承载着关键工程信息&a…

作者头像 李华
网站建设 2026/5/2 6:52:20

跨境电商报关提速:发票与装箱单多语言OCR识别一体化处理

跨境电商报关提速:发票与装箱单多语言OCR识别一体化处理 在跨境物流的日常运转中,一个看似不起眼的环节——报关文档录入,正悄然成为制约效率的关键瓶颈。每天成千上万份商业发票、装箱单从全球各地涌入,格式五花八门,…

作者头像 李华