【教学类-58-01】黑白三角拼图01(2*2宫格)256种

news2024/10/6 18:28:21

背景需求:

中班益智区素材:三角形变魔术 - 小红书自制益智区教玩具:三角形变魔术,共24题 玩法一:根据图示在空白格子里摆放。 玩法二:根据图示在空白格子里用黑笔涂。##自制玩具益智区 #幼儿园益智区 #中班益智区 #区域素材 #幼儿园益智区可打印素材icon-default.png?t=N7T8https://www.xiaohongshu.com/discovery/item/642edea90000000012032c3d?app_platform=android&ignoreEngage=true&app_version=8.36.0&share_from_user_hidden=true&type=normal&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1716531629

小红书上看到一个三角黑块拼图,我去年带的班级里也有这样的积木块。

我想用编程来做一下9块积木块共有多少种不同的排列方式。

样式是3*3的,感觉数量会很多,先做一个2*2的拼图。

代码展示:

1、800*800画布上制作2*2单元格

2、读取四个单元格的每个单元格的的四个坐标,都随机抽取3个

3、梳理出不重复的样式坐标

4、根据三点坐标制作黑色直角三角形

'''
黑白三角(2*2), 4个单元格每个有四个坐标,四个坐标随机抽取3个,进行组合,共有256种不重复排序
AI对话大师,阿夏
2024年5月24日
'''

print('---1、制作2*2格子-------')
from PIL import Image, ImageDraw
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白三角'
# 创建800x800的画布
canvas = Image.new('RGB', (800, 800), (255, 255, 255))
draw = ImageDraw.Draw(canvas)

# 定义表格的行数和列数
rows = 2
cols = 2

# 计算单元格的宽度和高度
cell_width = 800 // cols
cell_height = 800 // rows

# 绘制表格的竖直线
for i in range(1, cols):
    x = i * cell_width
    draw.line([(x, 0), (x, 800)], fill=(0, 0, 0), width=2)

# 绘制表格的水平线
for i in range(1, rows):
    y = i * cell_height
    draw.line([(0, y), (800, y)], fill=(0, 0, 0), width=2)
mb='2格模板.png'
# 保存画布
canvas.save(path+fr'\{mb}')


print('---2、计算三个坐标点的黑色三角形不重复图案有几个-------')

# 创建一个空列表用于存储单元格的坐标
cell_coordinates = []

# 计算每个单元格的四个顶点坐标
for row in range(rows):
    for col in range(cols):
        top_left = (col * cell_width, row * cell_height)
        top_right = ((col + 1) * cell_width, row * cell_height)
        bottom_left = (col * cell_width, (row + 1) * cell_height)
        bottom_right = ((col + 1) * cell_width, (row + 1) * cell_height)
        
        # 将四个顶点坐标添加到列表中
        cell_coordinates.append([top_left, top_right, bottom_left, bottom_right])
# print(cell_coordinates)
# [[(0, 0), (400, 0), (0, 400), (400, 400)], [(400, 0), (800, 0), (400, 400), (800, 400)], [(0, 400), (400, 400), (0, 800), (400, 800)], [(400, 400), (800, 400), (400, 800), (800, 800)]]

import itertools,os

# 生成所有组合方式
combinations = list(itertools.product(*[itertools.combinations(sublist, 3) for sublist in cell_coordinates]))
# print(combinations)
print(len(combinations))
# 256


print('---3、制作三个坐标点的黑色三角形(4个)-------')
from PIL import Image, ImageDraw

new=path+r'\二宫格组合图片'
os.makedirs(new,exist_ok=True)

m=1
# 定义要绘制的坐标点组合
for point_combination in combinations:
        # 读取图像文件
    image = Image.open(path+fr'\{mb}')

    # 创建绘图对象
    draw = ImageDraw.Draw(image)

    # 遍历每个坐标点组合
    for combination in point_combination:
        # 绘制填充为黑色的多边形
        draw.polygon(combination, fill="black")

    # 保存结果图像
    image.save(new+fr"\{m:03d}.png")
    m+=1


print('---4合并打印-------')


# 第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

# 读取123文件夹中的所有图片地址
image_folder = new
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
image_files = [os.path.join(image_folder, file) for file in os.listdir(image_folder) 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'\模板6格.docx')
    print(group)
    
    # 遍历每个单元格,并插入图片
    for cell_index, image_file in enumerate(group):
        # 计算图片长宽(单位:厘米)
     
        
        # 插入图片到单元格
        table = doc.tables[0]
        cell = table.cell(int(cell_index / 2), cell_index % 2)
        # 6列两个都是6
        cell_paragraph = cell.paragraphs[0]
        cell_paragraph.clear()
        run = cell_paragraph.add_run()
        run.add_picture(image_file, width=Cm(9.4), height=Cm(9.4))
        
    # 保存Word文档
    doc.save(os.path.join(new_folder, f'{group_index + 1}.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 = path+fr'\黑白三角256关(6张一页).pdf'

# 将所有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
# 删除输出文件夹

shutil.rmtree(new_folder)

2宫格的黑白三角拼图有256种不重复的排列方式

但是吧图片插入6格模版里,发现图片都黏连在一起了。

第二次修改

加了一个上下左右边距

代码展示:

生成的基础模版(有上下左右白色边距)

'''
黑白三角(2*2), 4个单元格每个有四个坐标,四个坐标随机抽取3个,进行组合,共有256种不重复排序,带边距
AI对话大师,阿夏
2024年5月24日
'''

from PIL import Image, ImageDraw

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白三角'

# 创建800x800的画布
canvas = Image.new('RGB', (800, 800), (255, 255, 255))
draw = ImageDraw.Draw(canvas)

# 定义表格的行数和列数
rows = 2
cols = 2
margin = 50

# 计算单元格的宽度和高度
cell_width = (800 - 2 * margin) // cols
cell_height = (800 - 2 * margin) // rows

# 绘制表格的竖直线
for i in range(cols + 1):
    x = margin + i * cell_width
    draw.line([(x, margin), (x, 800 - margin)], fill=(0, 0, 0), width=2)

# 绘制表格的水平线
for i in range(rows + 1):
    y = margin + i * cell_height
    draw.line([(margin, y), (800 - margin, y)], fill=(0, 0, 0), width=2)

# 保存画布
mb = '2格模板.png'
canvas.save(path + fr'\{mb}')


print('---2、计算三个坐标点的黑色三角形不重复图案有几个-------')

# 创建一个空列表用于存储单元格的坐标
cell_coordinates = []

# 计算每个单元格的四个顶点坐标
for row in range(rows):
    for col in range(cols):
        top_left = (margin + col * cell_width, margin + row * cell_height)
        top_right = (margin + (col + 1) * cell_width, margin + row * cell_height)
        bottom_left = (margin + col * cell_width, margin + (row + 1) * cell_height)
        bottom_right = (margin + (col + 1) * cell_width, margin + (row + 1) * cell_height)

        
        # 将四个顶点坐标添加到列表中
        cell_coordinates.append([top_left, top_right, bottom_left, bottom_right])
# print(cell_coordinates)
# [[(0, 0), (400, 0), (0, 400), (400, 400)], [(400, 0), (800, 0), (400, 400), (800, 400)], [(0, 400), (400, 400), (0, 800), (400, 800)], [(400, 400), (800, 400), (400, 800), (800, 800)]]

import itertools,os

# 生成所有组合方式
combinations = list(itertools.product(*[itertools.combinations(sublist, 3) for sublist in cell_coordinates]))
# print(combinations)
print(len(combinations))
# 256


print('---3、制作三个坐标点的黑色三角形(4个)-------')
from PIL import Image, ImageDraw

new=path+r'\二宫格组合图片'
os.makedirs(new,exist_ok=True)

m=1
# 定义要绘制的坐标点组合
for point_combination in combinations:
        # 读取图像文件
    image = Image.open(path+fr'\{mb}')

    # 创建绘图对象
    draw = ImageDraw.Draw(image)

    # 遍历每个坐标点组合
    for combination in point_combination:
        # 绘制填充为黑色的多边形
        draw.polygon(combination, fill="black")

    # 保存结果图像
    image.save(new+fr"\{m:03d}.png")
    m+=1


print('---4合并打印-------')


# 第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

# 读取123文件夹中的所有图片地址
image_folder = new
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
image_files = [os.path.join(image_folder, file) for file in os.listdir(image_folder) 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'\模板6格.docx')
    print(group)
    
    # 遍历每个单元格,并插入图片
    for cell_index, image_file in enumerate(group):
        # 计算图片长宽(单位:厘米)
     
        
        # 插入图片到单元格
        table = doc.tables[0]
        cell = table.cell(int(cell_index / 2), cell_index % 2)
        # 6列两个都是6
        cell_paragraph = cell.paragraphs[0]
        cell_paragraph.clear()
        run = cell_paragraph.add_run()
        run.add_picture(image_file, width=Cm(9.4), height=Cm(9.4))
        
    # 保存Word文档
    doc.save(os.path.join(new_folder, f'{group_index + 1}.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 = path+fr'\黑白三角256关(6张一页).pdf'

# 将所有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
# 删除输出文件夹

shutil.rmtree(new_folder)

有白色边距

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

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

相关文章

Color预设颜色测试

"AliceBlue", "获取 ARGB 值为 的系统 #FFF0F8FF定义颜色。", "AntiqueWhite", "获取 ARGB 值为 的系统 #FFFAEBD7定义颜色。", "Aqua", "获取 ARGB 值为 的系统 #FF00FFFF定义颜色。", "Aquamarine"…

.NET File Upload

VS2022 .NET8 &#x1f4be;基础上传示例 view {ViewData["Title"] "File Upload"; }<h1>ViewData["Title"]</h1><form method"post" enctype"multipart/form-data" action"/Home/UploadFile"…

MacOS/Linux系统多Java环境切换

通常我们在进行Java项目开发时&#xff0c;会安装不同版本的JDK&#xff0c;那么这个时候又需要根据项目来使用不同的Java版本&#xff0c;那么怎么来切换昵 第一步&#xff1a; 首先找出系统中安装的所有版本的路径 /usr/libexec/java_home -V这里可以看出安装了三个java 版…

基础widgets

1.widgets_文本和字体 在flutter当中几乎所有的对象都是widget,他跟原生开发的控线不一样,flutter开发当中,widget的概念更广泛一点, 不仅可以表示ui元素,也可以表示一些功能性的组件,例如手势检测等 基础组件 文本和字体 对于html当中对应就是lab或者label或者span这样的行内元…

Python变量、注释与数据类型

大家好&#xff0c;Python 是一种强大而灵活的编程语言&#xff0c;被广泛用于各种领域&#xff0c;包括软件开发、数据分析、科学计算等。在 Python 中&#xff0c;变量、注释和数据类型是构建代码的基础&#xff0c;对于理解和掌握这些概念是至关重要的。本文将深入探讨 Pyth…

数据库系统概论(超详解!!!)第九节 嵌入式SQL

SQL语言提供了两种不同的使用方式 &#xff1a;交互式&#xff0c; 嵌入式。 SQL语言是非过程性语言 。事务处理应用需要高级语言。 这两种方式细节上有差别&#xff0c;在程序设计的环境下&#xff0c;SQL语句要做某些必要的扩充。 1.嵌入式SQL的处理过程 嵌入式SQL是将SQL…

SOA半导体光放大器及其应用

---翻译自Michael Connelly于2015年发表的文章 1.简介 在过去的二十五年里&#xff0c;光纤通信网络的部署和容量迅速增长。这种增长得益于新光电技术的发展&#xff0c;这些技术可用于利用光纤的巨大带宽。如今&#xff0c;运行的系统比特率已超过 100 Gb/s。光技术是全球信…

linux-x86_64-musl 里面的musl是什么意思?

在一些开源库里面可以看到&#xff0c;linux-x86_64-musl类似于这样的字符串&#xff0c;这个musl是什么意思呢&#xff1f; 在字符串 "linux-x86_64-musl" 中&#xff0c;musl 指的是 musl libc&#xff0c;这是一个轻量级的 C 标准库实现。 让我们来拆解一下这个字…

使用maven-helper插件解决jar包冲突

发现问题 maven-helper分析问题 如上所述&#xff0c;问题就是依赖版本冲突了&#xff0c;出现版本冲突的原因是因为由于Maven具有依赖传递性&#xff0c;所以当你引入一个依赖类的同时&#xff0c;其身后的依赖类也一起如过江之鲫纷至沓来了。 举个例子&#xff1a;   A依赖…

软件详细规划与设计概览(软件概要文档、详细设计文档)

1引言 1.1编写目的 1.2项目背景 1.3参考资料 2系统总体设计 2.1整体架构 2.2整体功能架构 2.3整体技术架构 2.4运行环境设计 2.5设计目标 3系统功能模块设计 3.1个人办公 4性能设计 4.1响应时间 4.2并发用户数 5接口设计 5.1接口设计原则 5.2接口实现方式 6运行设计 6.1运行模块…

扫描链接打开小程序配置-谁看谁迷糊

各位你们怎么理解这个规则&#xff1f;如果再多一条数据&#xff0c;和上面一样&#xff0c;只是测试范围为线上版本&#xff0c;又怎么解读&#xff1f; 反正以我对中文的掌握程度&#xff0c;我认为上面的规则是针对体验版的&#xff0c;符合规则的都跳转到体验版。新增的线上…

How to record real IP of user on nginx?

应用(Docker)使用WAF接入internet&#xff0c;nginx log 查不到用户的真实IP地址&#xff0c;于是修改nginx 设置&#xff0c;以下都是在linux下操作&#xff1a; 由于没有WAF权限&#xff0c;所以在 docker上启动了两个container&#xff0c;一个模拟WAF(r-proxy)&#xff0c…

mysql实战——mysql5.7升级到mysql8.0

1、上传mysql8.0压缩包到/usr/local目录下 tar -zxvf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz mv mysql-8.0.25-linux-glibc2.12-x86_64 mysql8 #更改文件夹所属 chown -R mysql.mysql /usr/local/mysql8/ 2、更改配置文件my.cnf vi /etc/my.cnf # 最后几个for8.0的参数要…

GEO数据挖掘-PCA、差异分析

From 生物技能树 GEO数据挖掘第二节 文章目录 探针注释自主注释流程(了解)PCA图、top1000基因热图探针注释查看示例代码 top 1000 sd 热图离散基因热图&#xff0c;top1000表达基因&#xff0c;只是看一下&#xff0c;不用放文章里 差异分析火山图差异基因热图转换id富集分析-K…

无人机集群路径规划:遗传算法求解无人机集群路径规划,提供MATLAB代码

一、单个无人机路径规划模型介绍 无人机三维路径规划是指在三维空间中为无人机规划一条合理的飞行路径&#xff0c;使其能够安全、高效地完成任务。路径规划是无人机自主飞行的关键技术之一&#xff0c;它可以通过算法和模型来确定无人机的航迹&#xff0c;以避开障碍物、优化…

基于springboot的毕业设计系统的开发源码

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于springboot的毕业设计系统的开发。项目源码以及部署相关请联系风歌&#xff0c;文末附上联系信息 。 项目简介&#xff1a; 毕业设计系统能够实现…

微软:最新ChatGPT-4o模型,可在 Azure OpenAI上使用

北京时间5月14日凌晨&#xff0c;OpenAI 一场不到 30 分钟的发布会&#xff0c;正式发布了 GPT-4o&#xff0c;视频语音交互丝滑到吓人&#xff0c;还即将免费可用&#xff01; GPT-4o&#xff0c;其中的「o」代表「omni」&#xff08;即全面、全能的意思&#xff09;&#xff…

和程序员de 相处之道

1、不要"一遇到问题就去找程序员" 通常&#xff0c;技术问题通过阅读使用说明就可以解决。比如你刚买了一个新的播放器&#xff0c;想要把它连接到你的电视&#xff0c;你只需要找到使用手册里关于如何连接接口的那一页即可。 错误信息通常会被很清晰地列出来。通过…

需不需要选数据结构和算法的课程?

网络工程是互联网方向&#xff1f; 不过如果你想走机器学习和大数据方向的话&#xff0c;数据结构以及算法应该说是必修了吧。刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「数据结构的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“88…

XShell-连接-Centos 7

XShell 连接Centos 7 一.准备 安装XShell XShell下载地址&#xff1a; 在虚拟机上安装Centos 7&#xff0c;具体操作自行学习 二.Centos 7的准备 1.网络适配器修改为NAT 2.获取IP 输入命令&#xff1a; ip addr我的Centos 7对外IP为192.168.174.129 三.XShell连接Cento…