背景需求:
【教学类-68-01】20240720裙子涂色(女孩篇)-CSDN博客文章浏览阅读250次。【教学类-68-01】20240720裙子涂色(女孩篇)https://blog.csdn.net/reasonsummer/article/details/140578153
前期制作了女孩涂色延边剪用的裙子简笔画,现在想制作男孩感兴趣的图案,考虑到男孩手指精细动作不如女孩灵活,考虑设计”铠甲勇士扑克牌“,男孩只需要直线剪就可以了。
通义万相下载“铠甲勇士”简笔画
删除一些半身像
先用在图片左上角和右下角添加数字或字母,如果图像与数字字母有重叠,删除这个图片。
单独测试1、10、Joker
经过前面两次删除,99%勇士图都不会与左上角、右下角数字重叠,只找到一张盾牌与字母重叠的图片。
素材准备:
删除后的图片数量:144张,正好24张纸
代码展示:
'''
01铠甲勇士简笔画图(做成扑克牌) 6张
星火讯飞、通义万相、阿夏
2024年7月21日
'''
import os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReaderfrom PIL import Image, ImageDraw, ImageFont
import os,randomprint('----1、铠甲勇士图片左上右下加扑克牌数字------------')
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20240717铠甲勇士'
origin_folder = path + r'\01图片'
image_folder = path + r'\02改图'
os.makedirs(image_folder, exist_ok=True)image_files = [f for f in os.listdir(origin_folder) if f.endswith('.jpg') or f.endswith('.png')]for image_file in image_files:image = Image.open(os.path.join(origin_folder, image_file))draw = ImageDraw.Draw(image)numbers1 = list(range(2, 11))numbers = [str(num) for num in numbers1]print(numbers)b = ['A', 'J', 'Q', 'K', 'Joker']number = numbers + bprint(number)t = random.sample(number, 1)[0]print(t)zb = [[35,20],[910,870]] # 普通坐标zb1 = [[35,20],[910,420]] # joker坐标zb2 = [[35,20],[850,870]] # 10的坐标for k in range(len(zb)):font = ImageFont.truetype('arial.ttf', 120)if t =='Joker':vertical_text = ''.join([char for char in str(t)])width, height = font.getsize(vertical_text)for i, char in enumerate(vertical_text):draw.text((zb1[k][0], zb1[k][1] + i * height), char, font=font, fill='black')else:if t =='10':draw.text((zb2[k][0], zb2[k][1]), str(t), font=font, fill='black')else:draw.text((zb[k][0], zb[k][1]), str(t), font=font, fill='black')image.save(os.path.join(image_folder, image_file))print('----2、读取修图后的扑克牌图片,6------------')image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]# 将图片拆成6个一组
grouped_files = [image_files[i:i + 6] for i in range(0, len(image_files), 6)]
print(len(grouped_files))# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)# 处理每一组图片
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 / 3), cell_index % 3)# 如果第一行有4个格子,两个数字都写4cell_paragraph = cell.paragraphs[0]cell_paragraph.clear()run = cell_paragraph.add_run()run.add_picture(os.path.join(image_folder, image_file), width=Cm(9.42), height=Cm(9.75))# 保存Word文档doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))# 将10个docx转为PDF
import os
from docx2pdf import convert
from PyPDF2 import PdfFileMergerpdf_output_path = path+fr'\\铠甲勇士{int(len(grouped_files))}张共{len(image_files)}图.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()# 删除输出文件夹shutil.rmtree(new_folder)
shutil.rmtree(image_folder )
# shutil.rmtree(new)
time.sleep(2)