作品展示
背景说明
前期对A4横版做了三种迷宫图布局(1大张横版、 2小张竖版、4张小横版)
【教学类-09-06】20240401细线迷宫图01+箭头图片(A4横版一页-1份横版)-CSDN博客文章浏览阅读195次,点赞6次,收藏3次。【教学类-09-06】20240401细线迷宫图01+箭头图片(A4横版一页-1份横版)https://blog.csdn.net/reasonsummer/article/details/137207749【教学类-09-07】20240401细线迷宫图02+箭头图片(A4横版一页-2份竖版)-CSDN博客文章浏览阅读108次。【教学类-09-07】20240401细线迷宫图02+箭头图片(A4横版一页-2份竖版)
https://blog.csdn.net/reasonsummer/article/details/137207520
也尝试直接在迷宫图上加上箭头
【教学类-09-07】20240401细线迷宫图02+箭头图片(A4横版一页-2份竖版)-CSDN博客文章浏览阅读221次。【教学类-09-07】20240401细线迷宫图02+箭头图片(A4横版一页-2份竖版)
https://blog.csdn.net/reasonsummer/article/details/137207520?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22137207520%22%2C%22source%22%3A%22reasonsummer%22%7D现在我想做15*15CM手工纸大小的迷宫图(正方形迷宫)。
1、以15*15格为基础,制作0-15迷宫,16-30迷宫。
但是生成0格-7格会出现报错,怀疑格子数太少,迷宫无法生成?
从8*8开始,可以生成了迷宫图了。
代码测试:
第一次:8-30格
# 15CM正方形手工纸上 以15*15格子为基础,测试0-14,16-30的格子
num=1
# int(input('几张(30份)\n'))
print('-----------1、 生成细线迷宫-----------')
import sys
import matplotlib.pyplot as plt
from random import randint
import os
# 保存多少张图?
for x in range(8,31):
for i in range(num):
WIDTH = x
HEIGHT = WIDTH
sys.setrecursionlimit(WIDTH * HEIGHT)
def initVisitedList():
visited = []
for y in range(HEIGHT):
line = []
for x in range(WIDTH):
line.append(False)
visited.append(line)
return visited
def drawLine(x1, y1, x2, y2):
plt.plot([x1, x2], [y1, y2], color="black")
def removeLine(x1, y1, x2, y2):
plt.plot([x1, x2], [y1, y2], color="white")
def get_edges(x, y):
result = []
result.append((x, y, x, y+1))
result.append((x+1, y, x+1, y+1))
result.append((x, y, x+1, y))
result.append((x, y+1, x+1, y+1))
return result
def drawCell(x, y):
edges = get_edges(x, y)
for item in edges:
drawLine(item[0], item[1], item[2], item[3])
def getCommonEdge(cell1_x, cell1_y, cell2_x, cell2_y):
edges1 = get_edges(cell1_x, cell1_y)
edges2 = set(get_edges(cell2_x, cell2_y))
for edge in edges1:
if edge in edges2:
return edge
return None
def initEdgeList():
edges = set()
for x in range(WIDTH):
for y in range(HEIGHT):
cellEdges = get_edges(x, y)
for edge in cellEdges:
edges.add(edge)
return edges
def isValidPosition(x, y):
if x < 0 or x >= WIDTH:
return False
elif y < 0 or y >= HEIGHT:
return False
else:
return True
def shuffle(dX, dY):
for t in range(4):
i = randint(0, 3)
j = randint(0, 3)
dX[i], dX[j] = dX[j], dX[i]
dY[i], dY[j] = dY[j], dY[i]
def DFS(X, Y, edgeList, visited):
dX = [0, 0, -1, 1]
dY = [-1, 1, 0, 0]
shuffle(dX, dY)
for i in range(len(dX)):
nextX = X + dX[i]
nextY = Y + dY[i]
if isValidPosition(nextX, nextY):
if not visited[nextY][nextX]:
visited[nextY][nextX] = True
commonEdge = getCommonEdge(X, Y, nextX, nextY)
if commonEdge in edgeList:
edgeList.remove(commonEdge)
DFS(nextX, nextY, edgeList, visited)
edgeList = initEdgeList()
visited = initVisitedList()
DFS(0, 0, edgeList, visited)
edgeList.remove((0, 0, 0, 1))
edgeList.remove((WIDTH, HEIGHT-1, WIDTH, HEIGHT))
figure = plt.figure(figsize=(15,15)) # 创建一个指定大小的图像窗口
ax = plt.Axes(figure, [0., 0., 1., 1.], frame_on=False) # 创建一个坐标轴,覆盖整个图像窗口
figure.add_axes(ax)
ax.axis('off') # 关闭坐标轴显示
for edge in edgeList:
drawLine(edge[0], edge[1], edge[2], edge[3])
# 新建图片文件夹,保存所有生成的迷宫图
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
os.makedirs(folder_path, exist_ok=True)
plt.savefig(folder_path+r'\{}.png'.format('%02d'%x), dpi=400) # 保存迷宫图像为maze.png,设置dpi参数调整图像质量
plt.close() # 超过20张图片会提示占内存,所以关闭图像窗口释放内存
8*8:难度最低,间距最宽
11*11:难度低,间距较宽
15*15:难度中,间距约等于1CM,与15*15CM手工纸相同
23*23:难度高,间距较窄
30*30:难度高,间距极窄
30格子已经要走一点时间了,幼儿操作就限定在30以内吧。
第二次:30-60格(测试更大的格子数)
看最细宽度迷宫等于格子
标准:通道宽度窄到,完全不能划线了,线条细密看不清。
60*60:难度高,间距极窄
50*50:难度高,间距极窄
所有正方形迷宫图的上下左右边距(留白部分)都是一样宽的,即使格子数量增加,但边距都是一样宽的。
30-60格子,还是有空间画线的,但是走迷宫的时间很长了。已经不适合幼儿操作了。
测试3: 61-90
感觉无论数字多大,迷宫总是会有可以行走的空隙(成人操作)
测试4:直接输入200、300、400、500做测试
并没有三位数的图片生成
测试5:91-102
生成到93就终止了。
结论:
15*15CM手工纸上的迷宫图的格子数量范围
(最小8格,最大93格)
视频展示:
一共生成20分钟,才把8格-93格的图片全部生成完成,一开始每张图片10秒出现,后面25秒出现(格子越来越多了,生成就慢了)
20240401 手工纸15CM迷宫图(生成8-93格)
第二步:添加左上角的格子数目
因为生成太慢了,所以在图片上添加“格子数”的代码就没有与生8-93格迷宫图的代码连在一起,而是直接对“01迷宫图”里的08.png-93.png进行最上角格子数的添加(说明这张迷宫图是几个格子的)
代码展示:原图覆盖
# print('-----------2、PNG上左上角加格子数目----------')
import os
from PIL import Image, ImageDraw, ImageFont
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
# 获取文件夹内的所有文件
# folder_path = "path/to/123"
files = os.listdir(folder_path)
for file in files:
if file.endswith(".png"):
# 构建图片的完整路径
image_path = os.path.join(folder_path, file)
# 打开图片
image = Image.open(image_path)
# 创建一个可以在图片上绘制的对象
draw = ImageDraw.Draw(image)
# 定义字体和字号
font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 200)
# 在图片左上角写入文件名
# 300, 300正好在左上角的格子内部
# 50, 50正好在左上角的格子外部
draw.text((50, 50), file[:-4], fill="black", font=font)
# 保存修改后的图片
image.save(image_path)
# 关闭图片
image.close()
运行后,图片左上角有和png文件名字一样的数字序号。
第3步:把这些修改过的正方形迷宫图添加上起点、终点箭头
因为8格迷宫图道路宽敞,而93格迷宫图的道路狭窄,因此箭头的位置会不同,为了能够统一添加箭头,箭头位置以图93的起始位置为准
print('-----------2、PNG上加箭头,以图93的最细的起点重点位置为箭头的坐标点----------')
from PIL import Image
import os
# 箭头图片路径
arrow_image_path = path + r'\00箭头.png'
# 调整后的箭头图片大小
new_arrow_size = (200, 100) # 替换为你想要的箭头图片尺寸
# #1张横版的左下箭头 (300,7730) 4张横版的右上箭头 (11150, 450)
p1=[50,5750]
p2=[5650,250]
for r in range(len(p1)):
# 遍历图片文件夹
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
# 打开原始图片
image_path = os.path.join(folder_path, filename)
image = Image.open(image_path)
# 打开箭头图片并调整大小
arrow_image = Image.open(arrow_image_path)
arrow_image = arrow_image.resize(new_arrow_size)
# 如果箭头图片模式不是RGBA,则转换为RGBA模式以保留透明度信息
if arrow_image.mode != 'RGBA':
arrow_image = arrow_image.convert('RGBA')
# 在指定位置添加箭头图片
position = (int(p1[r]), int(p2[r])) # 替换为你想要添加箭头图片的位置坐标
image.paste(arrow_image, position, mask=arrow_image)
# 保存修改后的图片
new_image_path = os.path.join(folder_path, filename)
image.save(new_image_path)
# 关闭图片对象
image.close()
arrow_image.close()
08的道路宽,箭头看上去非常小
93的道路细小,箭头看上去非常小
第5套:把所有的正方形图片(包含格子数、左下角的箭头、右上角的箭头)写入docx,转成PDF,合并PDF
print('-----------3、 导入word,合并png----------')
import os,time
from docx import Document
from docx.shared import Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx2pdf import convert
from PyPDF2 import PdfMerger
# 设置路径和文件夹名称
# folder_path ='迷宫图所有图片文件夹路径'
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
template_path = path+r"\04迷宫图细线(15CM1张横图).docx"# 模板文件路径
output_path =path+r'\零时Word'# 生成docx和PDF的文件夹,最后要删除
# 创建输出文件夹
if not os.path.exists(output_path):
os.makedirs(output_path)
n = 1
# 遍历图片文件夹
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
# 打开模板文档
doc = Document(template_path)
# 获取第一个表格
table = doc.tables[0]
# 在表格中插入图片
table.cell(0, 0).paragraphs[0].add_run().add_picture(os.path.join(folder_path, filename), width=Cm(15), height=Cm(14.94))
# 保存为Word文档
doc.save(os.path.join(output_path, '{:02d}.docx'.format(n)))
time.sleep(3)
# 转换为PDF
convert(os.path.join(output_path, '{:02d}.docx'.format(n)), os.path.join(output_path, '{:02d}.pdf'.format(n)))
n += 1
# 合并PDF
pdf_lst = [os.path.join(output_path, filename) for filename in os.listdir(output_path) if filename.endswith('.pdf')]
pdf_lst.sort()
file_merger = PdfMerger()
for pdf in pdf_lst:
file_merger.append(pdf)
file_merger.write(path+fr'\01(打印合集)迷宫图(3-93格测试)(15CM整页1份横图)({num}份).pdf')
time.sleep(5)
# file_merger.write(os.path.join(output_path, '合并后的PDF.pdf'))
file_merger.close()
# 删除临时文件夹
import shutil
shutil.rmtree(output_path)
shutil.rmtree(folder_path)
所有代码
'''
批量制作细线条的迷宫图(15CM正方形手工纸横板一面一份横版)测试格子数量,最小3格,最多93格子,左上角贴上格子数量,左下角右上角加小箭头片
作者:
1、落难Coder https://blog.csdn.net/u014297502/article/details/124839912
2、AI对话大师、
3、阿夏
作者:2024年4月4日
'''
# 15CM正方形手工纸上 以15*15格子为基础,测试0-14,16-30的格子
num=1
# int(input('几张(30份)\n'))
print('-----------1、 生成细线迷宫-----------')
import sys
import matplotlib.pyplot as plt
from random import randint
import os
# 保存多少张图?
for x in range(8,94):
for i in range(num):
WIDTH = x
HEIGHT = WIDTH
sys.setrecursionlimit(WIDTH * HEIGHT)
def initVisitedList():
visited = []
for y in range(HEIGHT):
line = []
for x in range(WIDTH):
line.append(False)
visited.append(line)
return visited
def drawLine(x1, y1, x2, y2):
plt.plot([x1, x2], [y1, y2], color="black")
def removeLine(x1, y1, x2, y2):
plt.plot([x1, x2], [y1, y2], color="white")
def get_edges(x, y):
result = []
result.append((x, y, x, y+1))
result.append((x+1, y, x+1, y+1))
result.append((x, y, x+1, y))
result.append((x, y+1, x+1, y+1))
return result
def drawCell(x, y):
edges = get_edges(x, y)
for item in edges:
drawLine(item[0], item[1], item[2], item[3])
def getCommonEdge(cell1_x, cell1_y, cell2_x, cell2_y):
edges1 = get_edges(cell1_x, cell1_y)
edges2 = set(get_edges(cell2_x, cell2_y))
for edge in edges1:
if edge in edges2:
return edge
return None
def initEdgeList():
edges = set()
for x in range(WIDTH):
for y in range(HEIGHT):
cellEdges = get_edges(x, y)
for edge in cellEdges:
edges.add(edge)
return edges
def isValidPosition(x, y):
if x < 0 or x >= WIDTH:
return False
elif y < 0 or y >= HEIGHT:
return False
else:
return True
def shuffle(dX, dY):
for t in range(4):
i = randint(0, 3)
j = randint(0, 3)
dX[i], dX[j] = dX[j], dX[i]
dY[i], dY[j] = dY[j], dY[i]
def DFS(X, Y, edgeList, visited):
dX = [0, 0, -1, 1]
dY = [-1, 1, 0, 0]
shuffle(dX, dY)
for i in range(len(dX)):
nextX = X + dX[i]
nextY = Y + dY[i]
if isValidPosition(nextX, nextY):
if not visited[nextY][nextX]:
visited[nextY][nextX] = True
commonEdge = getCommonEdge(X, Y, nextX, nextY)
if commonEdge in edgeList:
edgeList.remove(commonEdge)
DFS(nextX, nextY, edgeList, visited)
edgeList = initEdgeList()
visited = initVisitedList()
DFS(0, 0, edgeList, visited)
edgeList.remove((0, 0, 0, 1))
edgeList.remove((WIDTH, HEIGHT-1, WIDTH, HEIGHT))
figure = plt.figure(figsize=(15,15)) # 创建一个指定大小的图像窗口
ax = plt.Axes(figure, [0., 0., 1., 1.], frame_on=False) # 创建一个坐标轴,覆盖整个图像窗口
figure.add_axes(ax)
ax.axis('off') # 关闭坐标轴显示
for edge in edgeList:
drawLine(edge[0], edge[1], edge[2], edge[3])
# 新建图片文件夹,保存所有生成的迷宫图
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
os.makedirs(folder_path, exist_ok=True)
plt.savefig(folder_path+r'\{}.png'.format('%02d'%x), dpi=400) # 保存迷宫图像为maze.png,设置dpi参数调整图像质量
plt.close() # 超过20张图片会提示占内存,所以关闭图像窗口释放内存
# plt.show()
# print('-----------2、PNG上左上角加格子数目----------')
import os
from PIL import Image, ImageDraw, ImageFont
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
# 获取文件夹内的所有文件
# folder_path = "path/to/123"
files = os.listdir(folder_path)
for file in files:
if file.endswith(".png"):
# 构建图片的完整路径
image_path = os.path.join(folder_path, file)
# 打开图片
image = Image.open(image_path)
# 创建一个可以在图片上绘制的对象
draw = ImageDraw.Draw(image)
# 定义字体和字号
font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 200)
# 在图片左上角写入文件名
# 300, 300正好在左上角的格子内部
# 50, 50正好在左上角的格子外部
draw.text((50, 50), file[:-4], fill="black", font=font)
# 保存修改后的图片
image.save(image_path)
# 关闭图片
image.close()
print('-----------2、PNG上加箭头,以图93的最细的起点重点位置为箭头的坐标点----------')
from PIL import Image
import os
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
# 箭头图片路径
arrow_image_path = path + r'\00箭头.png'
# 调整后的箭头图片大小
new_arrow_size = (200, 100) # 替换为你想要的箭头图片尺寸
# #1张横版的左下箭头 (300,7730) 4张横版的右上箭头 (11150, 450)
p1=[50,5750]
p2=[5650,250]
for r in range(len(p1)):
# 遍历图片文件夹
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
# 打开原始图片
image_path = os.path.join(folder_path, filename)
image = Image.open(image_path)
# 打开箭头图片并调整大小
arrow_image = Image.open(arrow_image_path)
arrow_image = arrow_image.resize(new_arrow_size)
# 如果箭头图片模式不是RGBA,则转换为RGBA模式以保留透明度信息
if arrow_image.mode != 'RGBA':
arrow_image = arrow_image.convert('RGBA')
# 在指定位置添加箭头图片
position = (int(p1[r]), int(p2[r])) # 替换为你想要添加箭头图片的位置坐标
image.paste(arrow_image, position, mask=arrow_image)
# 保存修改后的图片
new_image_path = os.path.join(folder_path, filename)
image.save(new_image_path)
# 关闭图片对象
image.close()
arrow_image.close()
print('-----------3、 导入word,合并png----------')
import os,time
from docx import Document
from docx.shared import Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx2pdf import convert
from PyPDF2 import PdfMerger
# 设置路径和文件夹名称
# folder_path ='迷宫图所有图片文件夹路径'
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\细线迷宫图图片'
folder_path = path+r'\01迷宫图'
template_path = path+r"\04迷宫图细线(15CM1张横图).docx"# 模板文件路径
output_path =path+r'\零时Word'# 生成docx和PDF的文件夹,最后要删除
# 创建输出文件夹
if not os.path.exists(output_path):
os.makedirs(output_path)
n = 1
# 遍历图片文件夹
for filename in os.listdir(folder_path):
if filename.endswith('.png'):
# 打开模板文档
doc = Document(template_path)
# 获取第一个表格
table = doc.tables[0]
# 在表格中插入图片
table.cell(0, 0).paragraphs[0].add_run().add_picture(os.path.join(folder_path, filename), width=Cm(15), height=Cm(14.94))
# 保存为Word文档
doc.save(os.path.join(output_path, '{:02d}.docx'.format(n)))
time.sleep(3)
# 转换为PDF
convert(os.path.join(output_path, '{:02d}.docx'.format(n)), os.path.join(output_path, '{:02d}.pdf'.format(n)))
n += 1
# 合并PDF
pdf_lst = [os.path.join(output_path, filename) for filename in os.listdir(output_path) if filename.endswith('.pdf')]
pdf_lst.sort()
file_merger = PdfMerger()
for pdf in pdf_lst:
file_merger.append(pdf)
file_merger.write(path+fr'\01(打印合集)迷宫图(3-93格测试)(15CM整页1份横图)(93份).pdf')
time.sleep(5)
# file_merger.write(os.path.join(output_path, '合并后的PDF.pdf'))
file_merger.close()
# 删除临时文件夹
import shutil
shutil.rmtree(output_path)
shutil.rmtree(folder_path)