【教学类-67-04】20240718毛毛虫ABC排序

背景需求

【教学类-67-01】20240715毛毛虫AB排序-CSDN博客文章浏览阅读747次,点赞17次,收藏8次。【教学类-67-01】20240715毛毛虫AB排序https://blog.csdn.net/reasonsummer/article/details/140443310【教学类-67-02】20240716毛毛虫ABB排序-CSDN博客文章浏览阅读308次。【教学类-67-02】20240716毛毛虫ABB排序https://blog.csdn.net/reasonsummer/article/details/140473546【教学类-67-03】20240717毛毛虫AAB排序-CSDN博客文章浏览阅读299次,点赞12次,收藏5次。【教学类-67-03】20240717毛毛虫AAB排序https://blog.csdn.net/reasonsummer/article/details/140486001

前期制作了AB、ABB、AAB的毛毛虫,

下面制作ABC

重点说明;

word模版

代码展示:

'''
名称:毛毛虫ABC排序
作者:星火讯飞、阿夏
实践:2024年7月18日
'''print('----1、制作毛毛虫图纸-------')
from PIL import Image, ImageDraw
from itertools import permutations
import os, random,math
import shutilpath = r'C:\Users\jg2yXRZ\OneDrive\桌面\毛毛虫'
path2 = path+r'\04毛毛虫ABC'# 创建一个空白画布
canvas_width = 900
canvas_height = 200
canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw = ImageDraw.Draw(canvas)# colours = ["red", "orange", "yellow", "green",  "blue","purple"]
colours = ["red", "orange", "yellow", "green", "cyan", "blue","purple", "pink"]# 使用itertools.permutations生成所有可能的排列
all_permutations = list(permutations(colours, 3))
print(f"一共有 {len(all_permutations)} 种不重复的方法")for xx in range(len(all_permutations)):b = 20py = 20     #  偏移值w=6circle_spacing = (canvas_width - 3 * b) / 9# 创建一个新的空白画布canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')draw = ImageDraw.Draw(canvas)# 绘制10个圆和线条for i in range(9):radius=10c = random.randint(10, 35)x = int(i * (circle_spacing) + b)  # 修改圆心位置,使圆的一半重叠,并加上左右上的间距,减去5像素以产生重叠效果# 第一个圆没有脚if i==0:            draw.line([(x +b +py+3, b+c+3), (50,10)], fill='black', width=w-3)  # 宽度乘以2,因为每磅等于2像素draw.ellipse((50-5,10-5,50+5,10+5), fill='white', outline='black', width=6)draw.line([(x + b+py+3, b+c+3), (100,10)], fill='black', width=w-3)  # 宽度乘以2,因为每磅等于2像素draw.ellipse((100-5,10-5,100+5,10+5), fill='white', outline='black', width=6)passelse:# 在每个圆的圆心位置向下画一条黑色3磅的线100磅长wdraw.line([(x + b+py, int(canvas_height/2)), (x + b+py, int(canvas_height/2) + py*4)], fill='black', width=w)  # 宽度乘以2,因为每磅等于2像素# 在每个圆的圆心位置向下画一条黑色3磅的线100磅长draw.line([(x + b+py*2, int(canvas_height/2)), (x + b+py*2, int(canvas_height/2) + py*4)], fill='black', width=w)  # 宽度乘以2,因为每磅等于2像素# 两个脚draw.ellipse((x + b+py-5-radius, int(canvas_height/2) + py*4-radius, x + b+py+radius-5, int(canvas_height/2) + py*4+radius), fill='white', outline='black', width=6)draw.ellipse((x + b+py*2-5-radius, int(canvas_height/2) + py*4-radius, x + b+py*2+radius-5, int(canvas_height/2) + py*4+radius), fill='white', outline='black', width=6)if i == 0 or i==3:color = str(all_permutations[xx][0])elif i==1:color = str(all_permutations[xx][1])elif i==2:color = str(all_permutations[xx][2])else:color = 'white'# 在每个圆draw.ellipse((x, b + c, x + circle_spacing + b, circle_spacing + 2 * b + c), fill=color, outline='black', width=2)
# r2=random.randint(3, radius-3)#   添加第一个眼睛if i==0:     #白眼珠    draw.ellipse((x+py*2-radius*2-10, b+c+py*2-radius*2, x+py*2-10 +radius*2,  b+c+py*2+radius*2), fill='white', outline='black', width=2)draw.ellipse((x+py*4-radius*2-10, b+c+py*2-radius*2, x+py*4-10 +radius*2,  b+c+py*2+radius*2), fill='white', outline='black', width=2)#黑眼珠 draw.ellipse((x+py*2-r2-10, b+c+py*2-r2, x+py*2-10 +r2,  b+c+py*2+r2), fill='black', outline='black', width=2)draw.ellipse((x+py*4-r2-10, b+c+py*2-r2, x+py*4-10 +r2,  b+c+py*2+r2), fill='black', outline='black', width=2)# 嘴巴白for bb in ['+1+1','+1-1','-1+1','-1-1']:center_x, center_y, radius, start_angle, end_angle, width, color = x+py*2.5+int(bb[:2]), b+c+py*3.5+int(bb[2:4]), 30, 0, 180, 6, 'white'points = [(center_x + radius * math.cos(math.radians(angle)), center_y + radius * math.sin(math.radians(angle))) for angle in range(start_angle, end_angle + 1)]draw.line(points, fill=color, width=width)# 嘴巴黑center_x, center_y, radius, start_angle, end_angle, width, color = x+py*2.5, b+c+py*3.5, 30, 0, 180, 6, 'black'points = [(center_x + radius * math.cos(math.radians(angle)), center_y + radius * math.sin(math.radians(angle))) for angle in range(start_angle, end_angle + 1)]draw.line(points, fill=color, width=width)else:pass# 保存并显示图像w1 = path2 + r'\04毛毛虫ABC'os.makedirs(w1, exist_ok=True)canvas.save(w1 + fr'\{xx:03d}.png')# 生成分开的圆圈
for xx in range(len(all_permutations)):b = 20py = 20     #  偏移值w=6circle_spacing = (canvas_width - 3 * b) / 9# 创建一个新的空白画布canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')draw = ImageDraw.Draw(canvas)# 绘制10个圆和线条for i in range(6):radius=10    x = int(i * (circle_spacing+50) + b)  # 修改圆心位置,使圆的一半重叠,并加上左右上的间距,减去5像素以产生重叠效果       # 添加圆形if i ==0  or i == 3 :color = str(all_permutations[xx][1])elif i == 1 or i == 4 :color = str(all_permutations[xx][2])   elif i == 2 or i == 5 :color = str(all_permutations[xx][0])       # 在每个圆draw.ellipse((x, b , x + circle_spacing + b, circle_spacing + 2 * b ), fill=color, outline='black', width=2)# 保存并显示图像w2 = path2 + r'\04毛毛虫ABC圆圈'os.makedirs(w2, exist_ok=True)canvas.save(w2 + fr'\{xx:03d}.png')print('-----2、读取毛毛虫图纸合并成PDF,读取圆圈图纸合并成pdf-------')
# 第3步,读取图片写入docx,合并PDFimport os,time
from docx import Document
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from PyPDF2 import PdfMerger
from docx.shared import Cmn=1
for c in [path2 + r'\04毛毛虫ABC',path2 + r'\04毛毛虫ABC圆圈']:# 读取123文件夹中的所有图片地址new_folder = path2+r'\零时文件夹'os.makedirs(new_folder, exist_ok=True)image_files = [os.path.join(c, file) for file in os.listdir(c) if file.endswith('.png')]# 每8个图片一组进行处理grouped_files = [image_files[i:i+6] for i in range(0, len(image_files), 6)]print(grouped_files)# 处理每一组图片for group_index, group in enumerate(grouped_files):# 创建新的Word文档doc = Document(path+r'\毛毛虫模版.docx')print(group)# 遍历每个单元格,并插入图片for cell_index, image_file in enumerate(group):# 计算图片长宽(单位:厘米)# 插入图片到单元格table = doc.tables[0]cell = table.cell(int(cell_index / 1), cell_index % 1)# 只有1列,两个都是1cell_paragraph = cell.paragraphs[0]cell_paragraph.clear()run = cell_paragraph.add_run()run.add_picture(image_file, width=Cm(19.6), height=Cm(4.35))# 保存Word文档doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))# 所有docx合并成PDF# 将10个docx转为PDFimport osfrom docx2pdf import convertfrom PyPDF2 import PdfFileMerger# from PyPDF4 import PdfMerger# output_folder = output_folderpdf_output_path = path2+fr'\{n}毛毛虫ABC({len(all_permutations)}个).pdf'# pdf_output_path = path+fr'\黑白三角1-10宫格随机每款{f*ys}图共{ys}张一黑一白黑点白边黑白.pdf'n+=1# 将所有DOCX文件转换为PDFfor docx_file in os.listdir(new_folder):if docx_file.endswith('.docx'):docx_path = os.path.join(new_folder, docx_file)convert(docx_path, docx_path.replace('.docx', '.pdf'))# 合并零时文件里所有PDF文件merger = PdfFileMerger()for pdf_file in os.listdir(new_folder):if pdf_file.endswith('.pdf'):pdf_path = os.path.join(new_folder, pdf_file)merger.append(pdf_path)time.sleep(2)# 保存合并后的PDF文件merger.write(pdf_output_path)merger.close()import shutil# 删除输出文件夹import timeshutil.rmtree(new_folder)# shutil.rmtree(w1)# shutil.rmtree(w2)time.sleep(2)print('-----3、毛毛虫图纸和圆圈图纸合并-------')import os
import PyPDF2# 指定包含PDF文件的文件夹路径
# folder_path = '123'# 获取文件夹中所有的PDF文件
pdf_files = [f for f in os.listdir(path2) if f.endswith('.pdf')]# 创建一个新的PDF文件用于合并
merger = PyPDF2.PdfFileMerger()# 遍历文件夹中的每个PDF文件
for pdf_file in pdf_files:# 读取PDF文件with open(os.path.join(path2, pdf_file), 'rb') as file:merger.append(file)# 将合并后的PDF文件保存到新文件中
output_file = path+fr'\04毛毛虫ABC({len(colours)}色{len(all_permutations)}图,图纸和圆圈).pdf'
with open(os.path.join(path2,output_file), 'wb') as output:merger.write(output)# 删除原始PDF文件
for pdf_file in pdf_files:os.remove(os.path.join(path2, pdf_file))print("PDF文件已合并并删除原始文件。")shutil.rmtree(w1)
shutil.rmtree(w2)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3247816.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

Atcoder ABC351 A-E 题解

A: 打卡题 题目描述 一中队和二中队正在进行一场棒球比赛&#xff0c;一中队是第一棒。 目前&#xff0c;比赛已进行到第九局上半&#xff0c;第九局下半即将开始。 一中队在 第i局 (1 < i < 9) 上半场得到了 Ai 分&#xff0c;二中队在 第j局 (1 < j < 8) 下…

数据结构之跳表SkipList、ConcurrentSkipListMap

概述 SkipList&#xff0c;跳表&#xff0c;跳跃表&#xff0c;在LevelDB和Lucene中都广为使用。跳表被广泛地运用到各种缓存实现当中&#xff0c;跳跃表使用概率均衡技术而不是使用强制性均衡&#xff0c;因此对于插入和删除结点比传统上的平衡树算法更为简洁高效。 Skip lis…

2-37 基于matlab的IMU姿态解算

基于matlab的IMU姿态解算,姿态类型为四元数&#xff1b;角速度和线加速度的类型为三维向量。IMU全称是惯性导航系统&#xff0c;主要元件有陀螺仪、加速度计和磁力计。其中陀螺仪可以得到各个轴的加速度&#xff0c;而加速度计能得到x&#xff0c;y&#xff0c;z方向的加速度&a…

云计算数据中心(三)

目录 四、自动化管理&#xff08;一&#xff09;自动化管理的特征&#xff08;二&#xff09;自动化管理实现阶段&#xff08;三&#xff09;Facebook自动化管理 五、容灾备份&#xff08;一&#xff09;容灾系统的等级标准&#xff08;二&#xff09;容灾备份的关键技术&#…

NXP i.MX8系列平台开发讲解 - 3.19 Linux TTY子系统(二)

专栏文章目录传送门&#xff1a;返回专栏目录 Hi, 我是你们的老朋友&#xff0c;主要专注于嵌入式软件开发&#xff0c;有兴趣不要忘记点击关注【码思途远】 目录 1. Linux 串口驱动 1.1 Uart 驱动注册流程 1.2 uart 操作函数 1.3 line discipline 2. Linux tty应用层使用…

Windows安装部署MySQL8.0

1.版本及下载 1.版本介绍&#xff1a; Alpha 版&#xff1a;开发版&#xff0c;公司内部使用 Beta 版&#xff1a;完成开发后&#xff0c;用户体验版 RC 版&#xff1a;生产环境发布之前的一个小版本或称候选版 GA 版&#xff1a;正式发布版本&#xff08;咱们要用的&…

代码随想录算法训练营Day26 | 491.递增子序列 | 46.全排列 | 47.全排列 II | 332.重新安排行程 | 51.N皇后 | 37.解数独

今日任务 491.递增子序列 题目链接&#xff1a; https://leetcode.cn/problems/non-decreasing-subsequences/description/题目描述&#xff1a; Code class Solution { public:vector<vector<int>> findSubsequences(vector<int>& nums) {vector&l…

SSE(Server Sent Event)实战(2)- Spring MVC 实现

一、服务端实现 使用 RestController 注解创建一个控制器类&#xff08;Controller&#xff09; 创建一个方法来创建一个客户端连接&#xff0c;它返回一个 SseEmitter&#xff0c;处理 GET 请求并产生&#xff08;produces&#xff09;文本/事件流 (text/event-stream) 创建…

leetcode145. 二叉树的后序遍历,递归法+迭代法,全过程图解+步步解析,一点点教会你迭代法后序遍历

leetcode145. 二叉树的后序遍历&#xff0c;递归法迭代法 给你一棵二叉树的根节点 root &#xff0c;返回其节点值的 后序遍历 。 示例 1&#xff1a; 输入&#xff1a;root [1,null,2,3] 输出&#xff1a;[3,2,1] 示例 2&#xff1a; 输入&#xff1a;root [] 输出&#…

vue、js截取视频任意一帧图片

html有本地上传替换部分&#xff0c;可以不看 原理&#xff1a;通过video标签对视频进行加载&#xff0c;随后使用canvas对截取的视频帧生成需要的图片 <template> <el-row :gutter"18" class"preview-video"><h4>视频预览<span&…

LabVIEW电路产品功能自动检测系统

开发基于LabVIEW的电路产品功能自动检测系统。该系统通过整合先进的硬件和软件技术&#xff0c;实现了电路产品的自动化测试&#xff0c;显著提高了测试效率和准确性&#xff0c;对于提升电子产品的可靠性和工作效率具有重要意义。 项目背景 在电子制造业中&#xff0c;电路产…

PyCharm查看文件或代码变更记录

背景&#xff1a; Mac笔记本上有一个截图的定时任务在运行&#xff0c;本地Python使用的是PyCharm IDE&#xff0c;负责的同事休假&#xff0c;然后定时任务运行的结果不符合预期&#xff0c;一下子不知道问题出现在哪里。 定位思路&#xff1a; 1、先确认网络、账号等基本的…

git使用以及理解

git练习网站 Learn Git Branching git操作大全Oh Shit, Git!?! git commit git branch name git merge bugFix 合并俩个分支 git rebase main git checkout headgit switch head 会导致HEAD分离 &#xff0c;就是指head->HEAD->c1 相对引用 ------------------- …

测试面试宝典(十四)—— 你觉得软件测试的核心竞争力是什么?

回答一&#xff1a; 软件测试的核心竞争力在于其能够保障软件产品的质量和可靠性。 首先&#xff0c;测试人员需要具备敏锐的观察力和细致入微的分析能力&#xff0c;能够在复杂的系统中发现潜在的缺陷和问题。例如&#xff0c;在测试一款电商平台时&#xff0c;不仅要关注订…

Apache AGE的MATCH子句

MATCH子句允许您在数据库中指定查询将搜索的模式。这是检索数据以在查询中使用的主要方法。 通常在MATCH子句之后会跟随一个WHERE子句&#xff0c;以添加用户定义的限制条件到匹配的模式中&#xff0c;以操纵返回的数据集。谓词是模式描述的一部分&#xff0c;不应被视为仅在匹…

【TDA4板端部署】基于 Pytorch 训练并部署 ONNX 模型在 TDA4

1 将torch模型转onnx模型 Ti转换工具只支持以下格式&#xff1a; Caffe - 0.17 (caffe-jacinto in gitHub) Tensorflow - 1.12 ONNX - 1.3.0 (opset 9 and 11) TFLite - Tensorflow 2.0-Alpha 基于 Tensorflow、Pytorch、Caffe 等训练框架&#xff0c;训练模型&#xff1a;选择…

多多OJ评测系统 前端项目环境初始化 安装Vue脚手架 引入Arco Design组件

目录 确定环境 命令行输入 装一下脚手架 监测一下是否安装成功 创建一个项目 选择一系列的配置后 我们打开webStorm 配置脚手架后我们先运行 我们这边能获取到网址 其实我们脚手架已经帮我们做到了 接下来要引入相关的组件 选择用npm进行安装 我们建议的是完整引入…

姓名配对测试源码

源码简介 姓名配对测试源码&#xff0c;输入两人姓名即可测试缘分&#xff0c;可查看朋友到底喜欢谁的趣味源码。 自己手动在数据库里修改数据&#xff0c;数据库里有就会优先查询数据库的信息&#xff0c; 没设置的话第一次查询缘分都是非常好的 95-99&#xff0c;第二次查…

Spring Web MVC(常用的注解@RequestMapping,@RequestParam,@RequestBody等)

一、Spring MVC spring的启动类 启动类是看这个 SpringBootApplication 注解&#xff0c;而不是 类的名字 这个注解在哪&#xff0c;哪个类就是启动类 1.MVC思想 举例 二、Spring MVC mvc 是一种思想&#xff0c;而spring mvc是对mvc思想的一种实现。全称是 spring web mvc…

pytorch学习(四)绘制loss和correct曲线

这一次学习的时候静态绘制loss和correct曲线&#xff0c;也就是在模型训练完成后&#xff0c;对统计的数据进行绘制。 以minist数据训练为例子 import torch from torch import nn from torch.utils.data import DataLoader from torchvision import datasets from torchvisi…