背景需求
前期做了一个蝴蝶的六面图
这次是想测试多张图片制作9个嵌套骰子,实现立体拼图的效果。
通义万相下载的正方形彩色图片(1:1)
word模版(一个两页)
以下是我写了8个小时代码(不用星火讯飞,套用代码改了很长时间)
1、正方形图片切割成9张
2、六张参考图做成嵌套列表
3、把每张图片的第一个小图合并在一起,在6个小图组合内插入空格,组合成10个一套
4、把六个原始图插入表2,做样板
代码展示
'''
立体拼图六面积木-3套图案测试
星火讯飞、通义万项
2024年7月24日
'''
from docx import Document
from docx.shared import Cm
from PIL import Image, ImageDraw, ImageFont
from PyPDF2 import PdfFileMerger, PdfFileReader
import os,time,shutil
# 你的代码逻辑
print('------1、多套6张图片切割成6*9张---------------')
def split_image(image_path, output_folder):
image = Image.open(image_path)
width, height = image.size
part_width = width // 3
part_height = height // 3
for i in range(3):
for j in range(3):
part = image.crop((i * part_width, j * part_height, (i + 1) * part_width, (j + 1) * part_height))
part.save(os.path.join(output_folder, f"{os.path.splitext(os.path.basename(image_path))[0]}_{i}_{j}{os.path.splitext(os.path.basename(image_path))[1]}"))
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\立体方块拼图'
input_folder = path + r"\01图片2"
image_folder = path + r"\02切割"
os.makedirs(image_folder, exist_ok=True)
for file in os.listdir(input_folder):
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
image_path = os.path.join(input_folder, file)
split_image(image_path, image_folder) # 修改为传入image_path而不是image_folder
print('------2、多套6张图片作为参考图---------------')
small_image1= [f for f in os.listdir(input_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
print(small_image1)
small_image= [small_image1[i:i +6] for i in range(0, len(small_image1),6)]
# small_image = [small_image2]
print(small_image)
print(len(small_image))
# 制作成[[],[]]的样式
print('------3、一套6张图片54张图片,把每张图片的第一张图片放在一起,插入空格,,做成列表--------------')
# print('--------制作图片 ----------')
# # 图片选择
image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]
print(image_files)
print(len(image_files))
# 162
# 有几套
z=int((len(image_files))/54)
# 将图片拆成9个一组
group_files = [image_files[i:i +54] for i in range(0, len(image_files),54)]
print(group_files)
print(len(group_files))
# 3
X=[]
for x in range(len(group_files)):
xx=[]
for x1 in range(6):
xx.append(group_files[x][x1*9:x1*9+9])
X.append(xx)
print(X)
print(len(X))
# 3
grouped_files1=[]
for g in range(9):
for k in range(z): # 3
gg=[]
for h in range(len(X[k])): # 6
print(h)
gg.append(X[k][h][g])
grouped_files1.append(gg)
print( grouped_files1)
print(len(grouped_files1))
# 在每个子列表的第4个位置插入一个空字符串
for group in grouped_files1:
group.insert(4, '')
group.insert(5, '')
group.insert(7, '')
group.insert(9, '')
print(grouped_files1)
print(len(grouped_files1))
# 27
DD=[]
for d in range(z):
chooice = [grouped_files1[i * z + d ] for i in range(len(grouped_files1) // z)]
DD.append(chooice)
print(DD)
print(len(DD))
grouped_files4 = [item for sublist in DD for item in sublist]
print(grouped_files4)
print(len(grouped_files4))
grouped_files3 = [item for sublist in grouped_files4 for item in sublist]
print(grouped_files3) # 输出: []
# [[60个],[]]
grouped_files = [grouped_files3[i:i +90] for i in range(0, len(grouped_files3),90)]
print(grouped_files)
print(len(grouped_files))
# 3
# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
print('------4、小图片插入插入9*10的单元格里---------------')
# # 处理每一组图片
for group_index, group in enumerate(grouped_files):
# 创建新的Word文档
doc = Document(path+r'\立体方块拼图模版1页.docx')
# print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
print(image_file)
# 插入图片到单元格
table = doc.tables[0]
cell = table.cell(int(cell_index / 10), cell_index % 10)
# 如果第一行有4个格子,两个数字都写4
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
if image_file !='':
run.add_picture(os.path.join(image_folder, image_file), width=Cm(3.2), height=Cm(3.2))
else:
pass
# 保存Word文档
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
time.sleep(5)
print('------5、参考图入1*6的单元格里---------------')
for group_index, group in enumerate(small_image):
# 创建新的Word文档
doc = Document(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
print(image_file)
# 插入图片到单元格
table = doc.tables[1]
cell = table.cell(int(cell_index / 6), cell_index % 6)
# 如果第一行有4个格子,两个数字都写4
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(os.path.join(input_folder, image_file), width=Cm(4.7), height=Cm(4.7))
# 保存Word文档
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
time.sleep(5)
print('------6、合并PDF---------------')
# 将10个docx转为PDF
import os,time
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
pdf_output_path = path+fr'\\立方体{int(len(grouped_files))}张小图{int(len(image_files)/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'))
time.sleep(5)
# 合并零时文件里所有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(3)
# 保存合并后的PDF文件
merger.write(pdf_output_path)
merger.close()
# 删除输出文件夹
shutil.rmtree(new_folder)
shutil.rmtree(image_folder)
# shutil.rmtree(new)
time.sleep(2)
本次是3套图片
背景需求
下次去学校打印出来,再展示9个立方体的制作方法吧。
标签:02,image,grouped,70,print,path,folder,os,20240724 From: https://blog.csdn.net/reasonsummer/article/details/140671528