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

news2024/9/24 21:27:01

背景需求:

【教学类-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,math


path = 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 = 20
    py = 20     #  偏移值
   
    w=6
    circle_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=10
       
        c = 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)
            pass
        else:
            # 在每个圆的圆心位置向下画一条黑色3磅的线100磅长w
            draw.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 = 20
    py = 20     #  偏移值
   
    w=6
    circle_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,合并PDF

import 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 Cm

n=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列,两个都是1
            cell_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转为PDF
    import os
    from docx2pdf import convert
    from PyPDF2 import PdfFileMerger
    # from PyPDF4 import PdfMerger

    # output_folder = output_folder

    pdf_output_path = path2+fr'\{n}毛毛虫ABB({len(all_permutations)}个).pdf'
    # pdf_output_path = path+fr'\黑白三角1-10宫格随机每款{f*ys}图共{ys}张一黑一白黑点白边黑白.pdf'
    n+=1

    # 将所有DOCX文件转换为PDF
    for 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 time
    shutil.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图。

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

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

相关文章

【JavaEE精炼宝库】 初识网络原理——网络通信基础 | 协议

文章目录 一、网络发展史1.1 独立模式:1.2 网络互连:1.3 局域网(LAN):1.4 广域网(WAN): 二、网络通信基础2.1 IP地址:2.2 端口号: 三、协议3.1 协议的概念&am…

RabbitMQ:基础篇

1.RabbitMQ是高性能的异步通讯组件 何为异步通讯 打电话就是同步通讯,微信聊天可以理解为异步通讯,不是实时的进行通讯:时效性差。 同步调用的缺点: 拓展性差(需求不尽提) 性能下降 级联失败 …

实时高清无延迟:EasyDSS/EasyCVR无人机直播技术重塑赛事新体验

近日有网友发视频称,在内蒙古呼伦贝尔一景区的马术表演现场,一架无人机擅自在场地上空飞行拍摄。现场工作人员多次制止飞行无果,一名表演者骑马用弓箭将无人机射落。 在数字科技日新月异的今天,无人机直播推流技术以其独特的视角…

在GPU上运行PyTorch

文章目录 1、查看GPU的CUDA版本2、下载CUDA版本3、安装cuDNN4、配置CUDA环境变量5、安装配置Anaconda6、使用Anaconda7、pycharm导入虚拟环境8、安装带GPU的PyTorch⭐9、总结 🍃作者介绍:双非本科大三网络工程专业在读,阿里云专家博主&#x…

字节跳动十年经验老鸟,耗时大半年整理的软件测试面试真题【附答案】

软件测试工程师,和开发工程师相比起来,虽然前期可能不会太深,但是涉及的面还是比较广的。前期面试实习生或者一年左右的岗位,问的也主要是一些基础性的问题比较多。涉及的知识主要有MySQL数据库的使用、Linux操作系统的使用、软件…

回溯算法的去重问题

概述 在利用回溯算法去求子集、排列、组合等问题时,所给数组中如果包含重复元素,需要进行去重操作。常用的去重方法是使用used数组或集合。 对于上述子集、排列、组合等问题的求解方法是将数组转化为树的形式,利用递归和回溯的方法进行求解…

10.1 JSP语言入门

JSP语言入门 目录一、 基础概念1. 什么是JSP?2. 工作原理3. 基本语法 二、 表达式语言(EL)1. 简介2. 语法 三、 JSTL(JSP Standard Tag Library)1. 简介2. 核心标签库3. 常用标签 四、 高级话题1. 会话管理2. 自定义标…

卷积神经网络(一)-LeNet-5

前言 LeNet开启了卷积神经网络的第一枪,这一网络模型由Yann LeCun等人在1998年提出,被视为卷积神经网络的开山之作。 论文地址: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf 如果打不开就看csdn: https://download.…

LangChain-v0.2 Build an Agent 构建代理

语言模型本身不能采取行动,它们只是输出文本。LangChain的一个重要用例是创建代理。代理是使用LLM作为推理引擎来确定要采取哪些行动,以及传递哪些输入的系统。执行操作后,可以将结果反馈到LLM中,以确定是否需要更多操作&#xff…

陪玩系统小程序模式APP小程序H5系统搭建开发

随着移动互联网的营及和游戏行业的蓬轨发展,陪玩服务应远而生并迅速唱起,陪玩系统小程序作为连接游戏玩家与陪玩师的桥梁,其模式系统的搭建与开发是得尤为重要,本文将洋细凰述陪玩系统小程宗模式系统的搭建开发流程,包…

基础动态规划题目基础动态规划题目

目录 题目1: P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles 代码示例: 题目2: Common Subsequence 代码示例 题目3 :最长上升子序列 最长不下降子序列 最长上升子序列oj答案 题目1: P1216 [USACO1.5]…

Linux热键,shell含义及权限介绍

君子忧道不忧贫。 —— 孔丘 Linux操作系统的权限 1、几个常用的热键介绍1、1、[Tab]键1、2、[ctrl]-c1、3、[ctrl]-d1、4、[ctrl]-r 2、shell命令以及运行原理3、权限3、1、什么是权限3、2、权限的本质3、3、Linux中的用户3、4、Linux中文件的权限3、4、1、快速掌握修改权限的…

华为HCIP Datacom H12-821 卷41

1.多选题 以下关于BGP Atomic_Aggregate和Aggregator的描述,正确的是哪些项? A、Aggregator属性属于可选过渡属性 B、Atomic_Aggregate属于公认任意属性 C、收到携带Atomic_Aggregate属性的路由表示这条路由不能再度明细化 D、 Agregator表示某条路由可能出现…

古玻璃制品的成分分析与鉴别详解【国一,附完整代码】

声明:2024年数模国赛即将来临,为助力国赛和钉钉杯,我将重温22年小样本国赛C题和23年大样本国赛C题,给出详细思路和完整代码,供广大数模爱好者阅览,如需比赛指导,请联系文章底部卡片咨询。 未经允…

UNiapp微信小程序Ucharts

效果图如下 以上为加载接口所得数据的玫瑰图与折线图 具体步骤如下 1,将插件导入Hbuiler 所需要的项目中(插件地址:秋云 ucharts echarts 高性能跨全端图表组件 - DCloud 插件市场) 2,导入成功是这样的 3&#xff0c…

Windows终端远程登陆Linux服务器(SSH+VScode)

W i n d o w s 终端远程登陆 L i n u x 服务器( S S H V S c o d e ) \huge{Windows终端远程登陆Linux服务器(SSHVScode)} Windows终端远程登陆Linux服务器(SSHVScode) 文章目录 写在前面通过SSH远程连接L…

rust + python+ libtorch

1: 环境,ubuntu 1.1 rust : rust-1.79.0 (在官方下载linux版本后,解压文件夹,内部有个install的sh文件,可安装) 安装成功测试:cargo --version 1.2 python3.10 (直接使用apt install pytho…

Redis-基础概念

目录 概念 Redis是什么 Redis 和 MySQL 的区别? Redis单线程有什么极端场景的瓶颈 Redis为什么快? 为什么Redis是单线程? Redis是单线程还是多线程 Redis为什么选择单线程做核心处理 Redis6.0之后引入了多线程,你知道为什么吗? 瓶颈是内存和I…

SAP PP学习笔记28 - 生产订单的收货及品质管理

上一章讲了生产订单的很多概念,比如确认(报工)以及报工的各种形式,反冲,自动入库等。 SAP PP学习笔记27 - Confirmation(报工/确认)(CO11,CO11N,CO15,CO12),…

IOS系统有什么好用的藏语翻译软件推荐吗?

藏语翻译通是我使用过的比较好用实用的藏语翻译软件。 藏语翻译通是一款专为藏语和汉语互译设计的智能翻译软件,它利用最新的人工智能技术,为用户提供高效、准确的翻译服务。软件界面简洁直观,易于操作,无论是藏语学习者、研究者…