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

【教学类-67-02】20240716毛毛虫ABB排序

背景需求:

【教学类-67-01】20240715毛毛虫AB排序-CSDN博客文章浏览阅读584次,点赞16次,收藏6次。【教学类-67-01】20240715毛毛虫AB排序https://blog.csdn.net/reasonsummer/article/details/140443310

在AB排序基础上,继续制作ABB。

重点说明;

word模版

代码展示:

'''
名称:毛毛虫ABB排序
作者:星火讯飞、阿夏
实践:2024年7月16日
'''print('----1、制作毛毛虫图纸-------')
from PIL import Image, ImageDraw
from itertools import permutations
import os, random,mathpath = r'C:\Users\jg2yXRZ\OneDrive\桌面\毛毛虫'
path2 = path+r'\02毛毛虫ABB'# 创建一个空白画布
canvas_width = 900
canvas_height = 200
canvas = Image.new('RGB', (canvas_width, canvas_height), 'white')
draw = ImageDraw.Draw(canvas)colours = ["red", "orange", "yellow", "green", "cyan", "blue", "purple", "pink"]
# 8色# 使用itertools.permutations生成所有可能的排列
all_permutations = list(permutations(colours, 2))
print(all_permutations)# 创建一个新的列表,用于存储修改后的组合
new_combinations = []# 遍历原始组合列表
for combo in all_permutations:# 获取第一个和第二个元素first_element = combo[0]second_element = combo[1]# 创建新的元组,包含第一个元素、第二个元素(作为第三个元素)和第二个元素new_combo = (first_element, second_element, second_element)# 将新元组添加到新组合列表中new_combinations.append(new_combo)# 打印新组合列表
print(new_combinations)# 计算组合数
print(f"一共有 {len(new_combinations)} 种不重复的方法")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 or i==2:color = str(all_permutations[xx][1])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# 保存并显示图像w = path2 + r'\02毛毛虫ABB'os.makedirs(w, exist_ok=True)canvas.save(w + 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 == 1 or i==3 or i==4:color = str(all_permutations[xx][1])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'\02毛毛虫ABB圆圈'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'\02毛毛虫ABB',path2 + r'\02毛毛虫ABB圆圈']:# 读取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}毛毛虫ABB({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(w)# 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'\02毛毛虫ABB({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文件已合并并删除原始文件。")

因为ABB也是抽取两个颜色,所以不重复数量还是56图。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 探索十大最佳产品设计软件:软件排行榜揭晓
  • Lora模型训练的参数-学习笔记
  • 【学习笔记】无人机(UAV)在3GPP系统中的增强支持(九)-无人机服务区分离
  • 防火墙-NAT策略和智能选路
  • 新手教学系列——简单的服务配置项集中管理
  • python取色器
  • Pycharm 导入 conda 环境
  • 开发指南047-前端模块版本
  • NineData全面支持PostgreSQL可视化表结构设计
  • 无人机监测的必要性及方法
  • ES证书过期替换方案
  • Python中的数据结构:五彩斑斓的糖果盒
  • 【深度学习入门篇 ⑦】PyTorch池化层
  • python自动化之用flask校验接口token(把token作为参数)
  • Web 安全之 VAPT (漏洞评估与渗透测试)详解
  • 08.Android之View事件问题
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • Fundebug计费标准解释:事件数是如何定义的?
  • Go 语言编译器的 //go: 详解
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • linux学习笔记
  • magento 货币换算
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • nfs客户端进程变D,延伸linux的lock
  • Odoo domain写法及运用
  • Spring Boot快速入门(一):Hello Spring Boot
  • Spring-boot 启动时碰到的错误
  • vue.js框架原理浅析
  • Webpack 4x 之路 ( 四 )
  • 给github项目添加CI badge
  • 记一次删除Git记录中的大文件的过程
  • 看域名解析域名安全对SEO的影响
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 排序算法学习笔记
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 前嗅ForeSpider采集配置界面介绍
  • 软件开发学习的5大技巧,你知道吗?
  • 设计模式 开闭原则
  • 正则表达式小结
  • ionic入门之数据绑定显示-1
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • # 服务治理中间件详解:Spring Cloud与Dubbo
  • (3)医疗图像处理:MRI磁共振成像-快速采集--(杨正汉)
  • (Oracle)SQL优化基础(三):看懂执行计划顺序
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (第61天)多租户架构(CDB/PDB)
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (实战篇)如何缓存数据
  • (四)汇编语言——简单程序
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .md即markdown文件的基本常用编写语法