背景需求:
用通义万相制作秘密花园涂色书
关键词(中文):秘密花园涂色书,简单笔画,卡通,黑白轮廓,未着色,幼儿插图,线条画,没有背景,没有颜色,黑白漫画线条艺术:,线描,空背景,粗轮廓,清晰的线条,矢量线。简单,大,
这个关键词,并不能生成4个,大多是3张图。
用UIBOT下载,
有重复的图片需要删除
删除后的图片
通义的“花园”涂色书,质量还是不错的,我做的都是1:1
素材模版
花园图片是正方形,下面还有一个长方形,所以我想再放点插图(花卉、蝴蝶、大头娃娃,都是通义万象下载的图片),让幼儿练习涂色和裁剪,黏贴在画面花园图片上。
最少的一份图片是人物图片,76张(双数,这个数字必须能够除以4才可以),另外的图片也抽取前76张。
代码展示
'''
秘密花园涂色书(通义鱼类图片,花园2图+3个小图片)
作者:AI对话大师,阿夏
2024年6月22日'''import os
import time
import random
from docx import Document
from docx.shared import Pt, Inches, Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# from docx.enum.text import WD_VERTICAL_ALIGNMENT
# from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
from docx.oxml.ns import qn
from docxtpl import DocxTemplate
import pandas as pd
from docx2pdf import convertprint('----------第1步:提取所有的幼儿照片的路径------------')
# 文件信息
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\秘密花园涂色书'# 读取每个子文件夹里的图片
subfolders = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
# print(len(subfolders))
# 6个文件夹jpg_paths = []
for v in range(1, len(subfolders)):png_paths = []first_subfolder = subfolders[v]for root, dirs, files in os.walk(os.path.join(path, first_subfolder)):for file in files:if file.endswith('.png'):png_paths.append(os.path.join(root, file))jpg_paths.append(png_paths)
# print(jpg_paths)
# print(len(jpg_paths))# 4组# 74张图片
path_jpg = []
for w in range(4):b = jpg_paths[w][:76] # 提取几张图片path_jpg.append(b)print(path_jpg)
# print(len(path_jpg))# 4path3 = [[path_jpg[k][h] for k in range(len(path_jpg))] for h in range(38)]
print(path3)
print(len(path3))# 从 path 中抽取 2 个一组
groups = [path3[i:i+2] for i in range(0, len(path3), 2)]
print(groups)
print(len(groups))pathend=[]
for u in groups: ll = [item for sublist in u for item in sublist]pathend.append(ll)
print(pathend)
print(len(pathend))
# for i in pathend:
# print(i)# 导入图片
pic=['00','10','11','12','04','14','15','16']
long=['14','2.96','5.5','5.5','14','2.96','5.5','5.5']
wide=['14','5.5','5.5','5.5','14','5.5','5.5','5.5']
# 每4个图片一组进行处理# 新建文佳佳
ten_folder = path+r'\零时Word'
os.makedirs(ten_folder , exist_ok=True)for nn in range(int(len(pathend))): # 读取图片的全路径 的数量 31张doc = Document(path+r'\02涂色书模板1大3小.docx') table = doc.tables[0] # 4567(8)行for l in range(int(len(long))):# 单元格坐标figures=pathend[nn][l] # 图片的全路径的第一张 a=int(pic[l][0])b=int(pic[l][1])# 写入1张大图run=doc.tables[0].cell(a,b).paragraphs[0].add_run() # # 图片位置 第一个表格的0 3 插入照片run.add_picture(r'{}'.format(figures),width=Cm(float(long[l])),height=Cm(float(wide[l])))table.cell(a,b).paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #居中 doc.save(ten_folder+r'\{}.docx'.format('%02d'%nn)) from docx2pdf import convert# docx 文件另存为PDF文件inputFile = ten_folder+fr'\{nn:02d}.docx' # 要转换的文件:已存在outputFile = ten_folder+fr'\{nn:02d}.pdf' # 要生成的文件:不存在# 先创建 不存在的 文件f1 = open(outputFile, 'w')f1.close()# 再转换往PDF中写入内容convert(inputFile, outputFile)print('----------第4步:把都有PDF合并为一个打印用PDF------------')# 多个PDF合并(CSDN博主「红色小小螃蟹」,https://blog.csdn.net/yangcunbiao/article/details/125248205)
import os
from PyPDF2 import PdfFileMerger
target_path = ten_folder
pdf_lst = [f for f in os.listdir(target_path) if f.endswith('.pdf')]
pdf_lst = [os.path.join(target_path, filename) for filename in pdf_lst]
pdf_lst.sort()
file_merger = PdfFileMerger()
for pdf in pdf_lst:print(pdf)file_merger.append(pdf)
file_merger.write(path+fr"\01涂色书1大3小({len(pathend)*2}人共{int(len(pathend))}份).pdf")
file_merger.close()
# doc.Close()# print('----------第5步:删除临时文件夹------------')
import shutil
shutil.rmtree(ten_folder) #递归删除文件夹,即:删除非空文件夹