【教学类-67-01】20240715毛毛虫AB排序

news2024/9/24 1:24:26

背景需求

幼儿园数学区 颜色排序 - 小红书毛毛虫颜色排序 直接打印#幼儿园数学icon-default.png?t=N7T8https://www.xiaohongshu.com/explore/63362546000000001d026455?app_platform=android&ignoreEngage=true&app_version=8.44.1&share_from_user_hidden=true&xsec_source=app_share&type=normal&xsec_token=CBwSWSoO8gVdQQZieUrR_Ro80Mo0aeVWwWgKbioOtCjoc=&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1720962096&share_id=8b4472aadc144a73a9effa41a8a070ee

这是一套AB样式的毛毛虫涂色卡。

我想用代码做出一下内容

1、八种颜色随机2个颜色,一共有不重复的56种

2、制作毛毛虫图案

(1)身体有起伏、眼睛有大小、嘴巴随着脸移动

(2) 触角、蹆、嘴巴是固定位置

3、制作黏贴用的圆圈

4、毛毛虫图纸和圆圈图片合并在一起

全部代码

'''
名称:毛毛虫AB排序
作者:星火讯飞、阿夏
实践:2024年7月15日
'''

print('----1、制作毛毛虫图纸-------')
from PIL import Image, ImageDraw
from itertools import permutations
import os, random,math

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\毛毛虫'
path2 = path+r'\01毛毛虫AB'

# 创建一个空白画布
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)

# 计算组合数
print(f"一共有 {len(all_permutations)} 种不重复的方法")

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 == 2 or i == 4:
            color = str(all_permutations[xx][0])
        elif i == 1 or i == 3:
            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'\01毛毛虫AB'
    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 == 2 or i==4:
            color = str(all_permutations[xx][1])
        elif i == 1 or i == 3 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'\01毛毛虫AB圆圈'
    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'\01毛毛虫AB',path2 + r'\01毛毛虫AB圆圈']:
    # 读取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}毛毛虫AB({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'\01毛毛虫AB({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文件已合并并删除原始文件。")

素材准备

生成后的效果

发布到小红书的作为学具商品哦

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

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

相关文章

Jenkins用户权限管理指定操作任务

安装插件 在 Jenkins 的管理插件中进行插件安装。 搜索插件库:Role-based Authorization Strategy 修改授权策略 在全局安全配置中,把授权策略改为Role-Based Strategy 添加角色规则 在安全中选择Manage and Assign Roles 在Global roles中添加一个…

嵌入式人工智能(2-树莓派4B开发板硬件环境搭建)

1.硬件开发环境(T型板) 树莓派4B开发板需要搭配面包板,T型板将40个GPIO口引出,再将T型板插到面包板上面。这个地方需要注意插接的方向,由于插树莓派引脚的排线没有防呆设计,因此,请注意方向&am…

Centos7 安装私有 Gitlab

在 CentOS 7上,下面的命令也会在系统防火墙中打开 HTTP、HTTPS 和 SSH 访问。这是一个可选步骤,如果您打算仅从本地网络访问极狐GitLab,则可以跳过它。 sudo yum install -y curl policycoreutils-python openssh-server perl sudo systemct…

免单优选:重塑电商销售新纪元

免单优选作为一种颠覆性的电商销售策略,其核心在于以价格优势为驱动,融合渐进激励与社交网络效应,深度挖掘并激发消费者的购买潜力,引领销售增长的新潮流。 一、合规为基,重塑信任 在免单优选模式中,我们坚…

el-popover或el-popconfirm中button不展示问题

vue3在使用Element-plus 2.X时&#xff0c;出现el-popover或el-popconfirm中button不展示问题。 正常效果&#xff1a; 第一种错误原因&#xff1a;el-button没有添加 slotreference <template slot-scope"scope"><el-popconfirm title"您确定删除吗…

CVE-2024-24549 Apache Tomcat - Denial of Service

https://lists.apache.org/thread/4c50rmomhbbsdgfjsgwlb51xdwfjdcvg Apache Tomcat输入验证错误漏洞&#xff0c;HTTP/2请求的输入验证不正确&#xff0c;会导致拒绝服务&#xff0c;可以借助该漏洞攻击服务器。 https://mvnrepository.com/artifact/org.apache.tomcat.embed/…

【Android】活动之间的穿梭

引入 在活动的初学建立了一个简单的活动&#xff0c;但只有一个活动不是过于简单&#xff0c;在你使用手机的时候按下一个按钮可能会跳转到下一个界面&#xff0c;此时就是活动之间的穿梭&#xff1a;使用Intent在活动之间穿梭 Intent&#xff1a;是android程序中各组件之间进…

【LeetCode力扣】006. Z 字形变换(Python)

最快解法 参考了运行时间最短的代码&#xff0c;其使用的思路就是按列排序后连接。 class Solution:def convert(self, s: str, numRows: int) -> str:if numRows < 2 : # numRows1时候&#xff0c;对应输出为原字符串return sn len(s)lst [ for _ in range(numRows…

andon系统在电力设备工管理中起到那些作用与价值

安灯系统&#xff0c;作为精益制造执行中的一个核心工具&#xff0c;在电力设备工厂车间管理中发挥着不可替代的作用&#xff0c;它能够实现生产透明管理&#xff0c;为工厂高效运作提供强大的支撑。本文将从安灯系统的功能、应用场景和价值三个方面&#xff0c;深入探讨其在电…

如何保证数据库和redis的数据一致性

1、简介 在客户端请求数据时&#xff0c;如果能在缓存中命中数据&#xff0c;那就查询缓存&#xff0c;不用在去查询数据库&#xff0c;从而减轻数据库的压力&#xff0c;提高服务器的性能。 2、问题如何保证两者的一致性 先更新数据库在删除缓存 难点&#xff1a;如何保证…

微信小程序,订阅消息

微信小程序&#xff0c;订阅消息&#xff0c;完整流程 1.选择需要的模版 2.前端调用订阅消息 注&#xff1a;tmplIds&#xff1a;模板ID模版id,这里也可以选多个 wx.requestSubscribeMessage({tmplIds: [7UezzOrfJg_NIYdE1p*******],success (res) { console.log(res);wx.g…

论文阅读:FAST SPECTRAL CLUSTERING WITH SELF-WEIGHTED FEATURES

1 Abstract 作为主流聚类方法之一&#xff0c;谱聚类因其在非线性数据集上的良好性能而越来越受到关注。然而&#xff0c;传统的谱聚类模型计算复杂度高。同时&#xff0c;大多数这些模型在实践中未能区分噪声和有用特征&#xff0c;导致聚类性能受限。在本文中&#xff0c;我…

集成excel工具:自定义导入回调监听器、自定义类型转换器、web中的读、捕获文件格式转换错误ExcelDataConvertException

文章目录 I 封装导入导出1.1 定义工具类1.2 自定义读回调监听器: 回调业务层处理导入数据1.3 定义文件导入上下文1.4 定义回调协议II 自定义转换器2.1 自定义枚举转换器2.2 日期转换器2.3 时间、日期、月份之间的互转2.4 LongConverterIII web中的读3.1 使用默认回调监听器3.2…

React、Vue的password输入框组件,如何关闭自动填充?

有时候我们的表单使用了一个password组件&#xff0c;这时候每次打开新建&#xff0c;都会自动获取浏览器缓存的密码&#xff0c;但是它的上一个input输入框并不是用户名&#xff0c;这时候我们希望我们的表单&#xff0c;每次点开的时候密码是空的&#xff0c;让用户自动输入&…

windows下通过nginx解压包启动nginx

退出&#xff1a; taskkill /f /t /im nginx.exe 人工智能学习网站 https://chat.xutongbao.top

【ProtoBuf】初识 ProtoBuf

一、序列化概念 1、序列化和反序列化 序列化&#xff1a;把对象转换为字节序列的过程称为对象的序列化。 反序列化&#xff1a;把字节序列恢复为对象的过程称为对象的反序列化。 什么情况下需要序列化&#xff1f; 存储数据&#xff1a;当我们想把的内存中的对象状态保存到⼀…

论文翻译:Explainability for Large Language Models: A Survey

https://arxiv.org/pdf/2309.01029 目录 可解释性在大型语言模型中&#xff1a;一项调查摘要1 引言2 LLMs的训练范式2.1 传统微调范式2.2 提示范式 3 传统微调范式的解释3.1 局部解释3.1.1 基于特征归因的解释3.1.2 基于注意力的解释3.1.3 基于示例的解释 3.2 全局解释3.2.1 基…

a newer or same version is present nvidia解决方案

安装时候出现a newer or same version is present nvidia 或者Night Visual Editor 失败&#xff0c;把显卡驱动卸载掉&#xff0c;打开service.mtc 服务控制面板&#xff0c;把nvidia开头的服务全停掉&#xff0c;重新启动cuda安装程序选择自定义安装 vse visual studio相关的…

构建未来智能边缘:4G定制化ARM边缘计算网关解决方案

在当今数字化转型的时代背景下&#xff0c;边缘计算正成为连接物理世界与数字世界的关键桥梁&#xff0c;为企业提供实时数据处理和决策能力。为了满足市场对高性能、灵活且可定制的边缘计算解决方案的需求&#xff0c;一款专为各类品牌量身定制的ARMxy边缘计算网关 产品亮点&…

【C++】题解:P1259 黑白棋子的移动_递归+模拟_算法竞赛_洛谷

文章目录 P1259 黑白棋子的移动 题解题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 提示解题思路AC CodeEnd P1259 黑白棋子的移动 题解 Link&#xff1a;Luogu - P1259 题目描述 有 2 n 2n 2n 个棋子排成一行&#xff0c;开始为位置白子全部在左边&#xff0c;黑…