当前位置: 首页 > news >正文

【教学类-88-01】20250105折纸窗花01——AI剪纸窗花(团花)——01图形的提取

背景需求:

1、新年剪纸

元旦了需要孩子们一起贺新年。搭档买了一套新年窗户剪纸

有一半的图纸都是对称花纹(团花)样式。便于小朋友操作(一些镂空有难度,老师代为操作)

2、消耗积分

每天积累50+20+50分,用不掉,30天过期。最近过年,我想试试生成中心对称的窗花纹样

操作过程

一、通义万相2.0

关键词:一个非常简单的窗花团花图案造型,中国传统剪纸,中点对称图案,弧线,简单传统纹样,,红色剪纸艺术,白色背景,幼儿剪纸图案,不要镂空,不要精细,不要细节,容易剪,不要花蕊,不要锯齿

二、下载图片

使用UIBOT将所有红色图片依次下载(识图不准,每次运行都要重新选择“无水印下载”文字)

团花团有两千多张(500次)

三、修改白色背景为(255,255,255)

使用UIBOT和PS进行修图

UIBOT代码

Dim arrayRet,iRet,图片名,iPID,temp
Rem 把输入法改成英文键盘
iPID=App.Start('''C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Photoshop.exe''', 1, 3)Delay(10000)
地址 = '''D:\红色剪纸\00白色'''
文件名=File.DirFileOrFolder(地址,"file",{"hasPath":true})
TracePrint(文件名)
张=UBound(文件名)TracePrint(张)Mouse.Move(8, 625, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Mouse.Move(27, 623, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
取色点坐标 = [486,236] //正方1:1
// 取色点坐标 = [830,554]//  正方1:1中心白色
// 取色点坐标 = [230,210]// 正方16:9
// 取色点坐标 = [604,133]// 正方9:16
// 取色点坐标 = [822,516]// 正方9:16 中心白色
// 取色点坐标 = [531,183]// 正方3:4
// 取色点坐标 = [500,220]// 正方4:3For i = 0 To 张 step 1If i=0 Rem 打开图片
Keyboard.Press("O", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 3000, "sSimulate": "simulate"})Keyboard.InputText(@ui"窗口1",地址,true,20,10000,{"bContinueOnError": false, "iDelayAfter": 500, "iDelayBefore": 2000, "bSetForeground": true, "sSimulate": "message", "bValidate": false, "bClickBeforeInput": false})Mouse.Action(@ui"窗口_打开(O)","left","click",10000,{"bContinueOnError": false, "iDelayAfter": 300, "iDelayBefore": 200, "bSetForeground": true, "sCursorPosition": "Center", "iCursorOffsetX": 0, "iCursorOffsetY": 0, "sKeyModifiers": [],"sSimulate": "simulate", "bMoveSmoothly": false})
图片名=Right(文件名[i],8)
Keyboard.InputText(@ui"窗口",图片名,true,20,10000,{"bContinueOnError": false, "iDelayAfter": 300, "iDelayBefore": 500, "bSetForeground": true, "sSimulate": "message", "bValidate": false, "bClickBeforeInput": false})
Mouse.Action(@ui"窗口_打开(O)","left","click",10000,{"bContinueOnError": false, "iDelayAfter": 300, "iDelayBefore": 200, "bSetForeground": true, "sCursorPosition": "Center", "iCursorOffsetX": 0, "iCursorOffsetY": 0, "sKeyModifiers": [],"sSimulate": "simulate", "bMoveSmoothly": false})
Rem 魔棒
Mouse.Move(15, 182, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 图像
Mouse.Move(取色点坐标[0], 取色点坐标[1], false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 选取相似
Keyboard.Press("S", "press", ["Alt"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Keyboard.Press("R", "down", [],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Rem 新建图层
Mouse.Move(1855, 1037, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 填充
Mouse.Move(21, 394, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 图像
Mouse.Move(取色点坐标[0], 取色点坐标[1], false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 合并
Keyboard.Press("E", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Rem 取消框线保存
Keyboard.Press("D", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})Keyboard.Press("S", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 1000, "sSimulate": "simulate"})
Rem 关闭
Mouse.Move(165, 72, false,{"iDelayAfter": 1000, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})End IfIf i>0 Rem 打开图片
Keyboard.Press("O", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 3000, "sSimulate": "simulate"})
图片名=Right(文件名[i],8)
Keyboard.InputText(@ui"窗口",图片名,true,20,10000,{"bContinueOnError": false, "iDelayAfter": 300, "iDelayBefore": 500, "bSetForeground": true, "sSimulate": "message", "bValidate": false, "bClickBeforeInput": false})
Mouse.Action(@ui"窗口_打开(O)","left","click",10000,{"bContinueOnError": false, "iDelayAfter": 300, "iDelayBefore": 200, "bSetForeground": true, "sCursorPosition": "Center", "iCursorOffsetX": 0, "iCursorOffsetY": 0, "sKeyModifiers": [],"sSimulate": "simulate", "bMoveSmoothly": false})
Rem 魔棒
Mouse.Move(15, 182, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 图像
Mouse.Move(取色点坐标[0], 取色点坐标[1], false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 选取相似
Keyboard.Press("S", "press", ["Alt"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Keyboard.Press("R", "down", [],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Rem 新建图层
Mouse.Move(1855, 1037, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 填充
Mouse.Move(21, 394, false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 图像
Mouse.Move(取色点坐标[0], 取色点坐标[1], false,{"iDelayAfter": 300, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
Rem 合并
Keyboard.Press("E", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Rem 取消框线保存
Keyboard.Press("D", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 200, "sSimulate": "simulate"})
Keyboard.Press("S", "press", ["Ctrl"],{"iDelayAfter": 300, "iDelayBefore": 1000, "sSimulate": "simulate"})
Rem 关闭
Mouse.Move(165, 72, false,{"iDelayAfter": 1000, "iDelayBefore": 200})
Mouse.Click("left", "click", [],{"iDelayAfter": 300, "iDelayBefore": 300})
End If
NextApp.Kill(iPID)
Dim sRet
Dim sName

2000张图,时间很长

(但是必须要用PS填充(0,0,0)白色背景,因为下载下来的图片,肉眼看不出来白色的差异,实际白色背景里通常还有其他的白色,并不完全是0,0,0,)

四、代码展示

添加白色背景为(0,0,0)后,其余部分都可以Python代码解决了。

(一)原图

用8张修过白背景的图片做范例

(2)切掉白边(容量变小300K,尺寸不统一,去掉白边)

(3)统一大小(1200*1200)

(4)白色背景变成透明

(5)黑色图(先变成绿色)

(6)点状描边(点状是需要裁剪掉的部分,黑色是裁剪范围)

(7)绿色变成白色(需要保留的部分)

(8)把黑色图的绿色变成黑色(作为图纸左下角的参考图)

代码展示

'''
剪纸(团花)图案的图像处理(切边、统一大小、背景透明、黑色参考图、点状描边图)
星火讯飞、阿夏
20250105
'''
print('----1、切掉上下左右的白色图层------')import os
from PIL import Imageimport os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReaderfrom PIL import Image, ImageDraw, ImageFont
import os,randompath = r'D:\红色剪纸'
folder_path = path + r'\01白色'
output_folder = path + r'\02切边图'
os.makedirs(output_folder, exist_ok=True)# 留一点白边
white_edge = 0
# miao描边像素
mb=1def find_non_white_pixel(image):width, height = image.sizeleft, right, top, bottom = width, 0, height, 0for y in range(height):for x in range(width):r, g, b = image.getpixel((x, y))[:3]  # Ensure only RGB values are consideredif r != 255 or g != 255 or b != 255:if x < left:left = xif x > right:right = xif y < top:top = yif y > bottom:bottom = yreturn left, right, top, bottomdef crop_image(image, left, right, top, bottom):return image.crop((left - white_edge, top - white_edge, right + white_edge, bottom + white_edge))for file_name in os.listdir(folder_path):if file_name.endswith(".jpg") or file_name.endswith(".png"):input_path = os.path.join(folder_path, file_name)image = Image.open(input_path).convert('RGB')  # Convert to RGB modeleft, right, top, bottom = find_non_white_pixel(image)cropped_image = crop_image(image, left, right, top, bottom)output_path = os.path.join(output_folder, file_name)cropped_image.save(output_path)print('----2、统一大小(1200*1200)------')import os
from PIL import Image# input_folder = path+r'\05切边图'
output_folder =path+r'\02切边图'
# output_folder = path+r'\04透明图'
newput_folder =path+r'\03统一图'
os.makedirs(newput_folder,exist_ok=True)# 提取最大宽度的那张图片的尺寸
def get_max_width_and_height(fold_path):max_width = 0max_height = 0for file_name in os.listdir(fold_path):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):file_path = os.path.join(fold_path, file_name)img = Image.open(file_path)width, height = img.sizeif width > max_width:max_width = widthmax_height = heightreturn max_width, max_heightfold_path = output_folder
max_width, max_height = get_max_width_and_height(fold_path)
print("最大宽度:", max_width)
print("最大高度:", max_height)# 最大宽度: 724
# 最大高度: 869# 自定义长宽
max_width=1200
max_height=1200# 统一所有图片大小
def resize_image(image_path, output_folder, new_image_name):img = Image.open(image_path)new_img = img.resize((max_width,max_height))new_img.save(os.path.join(output_folder, new_image_name))for file in os.listdir(output_folder):if file.endswith('.png'):input_image_path = os.path.join(output_folder, file)new_image_name = f"{file[:-4]}.png"resize_image(input_image_path, newput_folder, new_image_name)print('----3、背景透明图)------')from PIL import Image
import osdef process_image(file_path):img = Image.open(file_path)img = img.convert("RGBA")datas = img.getdata()new_data = []for item in datas:if item[0] == 255 and item[1] == 255 and item[2] == 255:new_data.append((255, 255, 255, 0))else:new_data.append(item)img.putdata(new_data)return imginput_folder = path+r'\03统一图'
output_folder = path+r'\04透明图'if not os.path.exists(output_folder):os.makedirs(output_folder)for file_name in os.listdir(input_folder):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):input_file_path = os.path.join(input_folder, file_name)output_file_path = os.path.join(output_folder, file_name)processed_image = process_image(input_file_path)processed_image.save(output_file_path)print('----4、黑色图(参考图)------')from PIL import Image
import os# 定义路径
input_folder =path+r'\04透明图'
output_folder = path+r'\05黑色图'
os.makedirs(output_folder, exist_ok=True)def process_image(file_path):img = Image.open(file_path).convert("RGBA")datas = img.getdata()new_data = []for item in datas:if item[3] != 0:  # 非透明像素点new_data.append((125, 125, 0, item[3]))  # 改为黑色 (0, 0, 0)else:new_data.append(item)img.putdata(new_data)return imgfor file_name in os.listdir(input_folder):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):input_file_path = os.path.join(input_folder, file_name)output_file_path = os.path.join(output_folder, file_name)processed_image = process_image(input_file_path)processed_image.save(output_file_path)print('----5、制作点背景、描边线条,空心图案的 组合图------')import os
from PIL import Image, ImageDraw, ImageOps# 定义路径input_folder = path + r'\05黑色图'
combined_folder = path + r'\06点状描边图'
combined_output_folder = path + r'\07合并图'
os.makedirs(output_folder, exist_ok=True)
os.makedirs(combined_folder, exist_ok=True)
os.makedirs(combined_output_folder, exist_ok=True)def create_base_image():base_width, base_height = 1200, 1200dot_diameter = 5  # 圆点直径    dot_radius = dot_diameter // 2  # 圆点半径margin = 2  # 空格间隙total_width = dot_diameter + margin * 2  # 总宽度num_dots = base_width // total_width*2  # 点子数乘以2,以便撑满画布base_img = Image.new('RGBA', (base_width, base_height), (255, 255, 255, 255))  # 白色背景draw = ImageDraw.Draw(base_img)for y in range(num_dots):for x in range(num_dots):left = x * total_width + margintop = y * total_width + marginright = left + dot_diameterbottom = top + dot_diameterdraw.ellipse((left, top, right, bottom), fill=(0, 0, 0, 255))  # 黑色圆点return base_imgdef process_image(file_path):img = Image.open(file_path).convert("RGBA")datas = img.getdata()width, height = img.size# 创建一个新的透明图像用于描边edge_img = Image.new('RGBA', (width, height), (0, 0, 0, 0))draw = ImageDraw.Draw(edge_img)# 遍历每个像素点for y in range(height):for x in range(width):r, g, b, a = datas[y * width + x]if a != 0:  # 非透明像素点# 检查周围像素是否透明for dy in [-1, 0, 1]:for dx in [-1, 0, 1]:nx, ny = x + dx, y + dyif 0 <= nx < width and 0 <= ny < height:nr, ng, nb, na = datas[ny * width + nx]if na == 0:  # 透明像素点draw.ellipse((x - mb, y - mb, x + mb, y + mb), fill=(0, 0, 0, 255))  # 红色描边breakelse:continuebreak# 将 (1, 1, 1) 的部分变成白色edge_data = edge_img.load()for y in range(height):for x in range(width):if edge_data[x, y][:3] == (125, 125, 0):edge_data[x, y] = (255, 255, 255, 255)# 合并原图和描边图result_img = Image.alpha_composite(img.convert("RGBA"), edge_img)return result_imgdef combine_images(base_img, processed_img):combined_img = Image.alpha_composite(base_img.convert("RGBA"), processed_img)return combined_imgbase_img = create_base_image()for file_name in os.listdir(input_folder):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):input_file_path = os.path.join(input_folder, file_name)output_file_path = os.path.join(output_folder, file_name)combined_file_path = os.path.join(combined_folder, file_name)processed_image = process_image(input_file_path)combined_img = combine_images(base_img, processed_image)combined_img.save(combined_file_path)# 读取07点状图中的图片并处理
for file_name in os.listdir(combined_folder):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):combined_file_path = os.path.join(combined_folder, file_name)output_file_path = os.path.join(combined_output_folder, file_name)combined_img = Image.open(combined_file_path)combined_data = combined_img.load()for y in range(combined_img.height):for x in range(combined_img.width):if combined_data[x, y][:3] == (125, 125, 0):combined_data[x, y] = (255, 255, 255, 255)combined_img.save(output_file_path)print('----6、黑色图(实际绿色)变成黑色参考图------')
from PIL import Image
import os# 定义路径
output_folder = path + r'\05黑色图'
reference_folder = path + r'\08参考图'
os.makedirs(output_folder, exist_ok=True)
os.makedirs(reference_folder, exist_ok=True)def convert_color(file_path):img = Image.open(file_path).convert("RGBA")datas = img.getdata()new_data = []for item in datas:if item[:3] == (125, 125, 0):new_data.append((0, 0, 0, item[3]))  # 将 (125, 125, 0) 改为 (0, 0, 0)else:new_data.append(item)img.putdata(new_data)return img# 读取黑色图中的内容并转换颜色后保存到参考图文件夹
for file_name in os.listdir(output_folder):if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):output_file_path = os.path.join(output_folder, file_name)reference_file_path = os.path.join(reference_folder, file_name)converted_image = convert_color(output_file_path)converted_image.save(reference_file_path)

目前发现有些图案的边缘比较光滑,可以使用

有些图案的边缘不光滑,细节边缘不模糊,或者图案周围有黑点,我不想再修图了,还是放弃吧

后续对图案进行分类(两次边对折(4等分、8等分图案)两次对角折4等分、8等分图案)、五边形折叠等,然后再添加固定折痕线,只保留一个角的图案,

下了几百张后换关键词

一个非常简单的剪纸对称图案造型,春节元素,中国传统剪纸,中点对称图案,弧线,简单传统纹样,,红色剪纸艺术,白色背景,幼儿剪纸图案,不要镂空,不要精细,不要细节,容易剪,


http://www.mrgr.cn/news/82671.html

相关文章:

  • 第1章:Go语言入门
  • STM32F4适配WINUSB1.0
  • 笔上云世界微服务版
  • oceanbase集群访问异常问题处理
  • 【HarmonyOS】:DevEco Studio安装与应用工程创建指南
  • SpringBoot3 集成配置讲解 - SpringBoot3 与 SpringBoot2 的核心差别和升级理由。
  • SkinnedMeshRenderer相关知识
  • 如何让大模型不再“已读乱回”——RAG技术助力生成更精确的答案
  • 三、GIT与Github推送(上传)和克隆(下载)
  • 奥迪TT MK1(初代奥迪TT、第一代奥迪TT)仪表盘故障/不精准/水温/剩余油量不准,如何修复、测试、复位?
  • windows11安装minikube
  • arm64函数源码和汇编解析(objdump)
  • 线上go内存泄漏分析实战
  • Java100道面试题
  • 网站常用功能模块-鉴权
  • 【Rust自学】10.6. 生命周期 Pt.2:生命周期的语法与例子
  • 后台管理系统用户退出登录方案实现
  • 4进货+后台事务
  • Kubernetes Gateway API-3-TLS配置
  • 如何在 Hive SQL 中处理复杂的数据类型?
  • 安卓触摸对焦
  • 如何操作github,gitee,gitcode三个git平台建立镜像仓库机制,这样便于维护项目只需要维护一个平台仓库地址的即可-优雅草央千澈
  • 如何使用 Ansys OptiSlang 同时运行多个参数化设计研究
  • 当今世界如何减少暴戾之气和矛盾纷争
  • 【Rust自学】10.5. 生命周期 Pt.1:生命周期的定义与意义、借用检查器与泛型生命周期
  • Linux 基础七 内存