背景需求
通义万相下载“对称建筑,简笔画”
我想把“对称建筑”做成对称的样式,类似《对称脸》
【教学类-36-12-01】20240302儿童对称正脸图(一)4图右脸+4图左脸(中班《幼儿园里朋友多》)(midjounery-v 5.1 Python图片切割)-CSDN博客文章浏览阅读766次,点赞30次,收藏30次。【教学类-36-12-01】20240302儿童对称正脸图(一)4图右脸+4图左脸(中班《幼儿园里朋友多》)(midjounery-v 5.1 Python图片切割)https://blog.csdn.net/reasonsummer/article/details/136417790
首先用程序,把图片切割成左边图案+右边空白(中线是虚线)、右边图案+左边空白(中线是虚线)
模板展示
代码展示
'''
建筑对称剪纸
星火讯飞、阿夏
2024年8月3日
'''
import os
from PIL import Image, ImageDraw
print('----1、房屋切割成左右对称两片------------')
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\对称房子剪纸'
old_path=path+r'\01房屋'
left_path=path+r'\02左房'
os.makedirs(left_path,exist_ok=True)
right_path=path+r'\03右房'
os.makedirs(right_path,exist_ok=True)
# 获取文件夹下的所有图片文件
image_files = [f for f in os.listdir(old_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 左边房子
for image_file in image_files:
# 打开图片
image_path = os.path.join(old_path, image_file)
image = Image.open(image_path)
# 获取图片的宽度和高度
width, height = image.size
# 创建一个新的空白图片,宽度为原图的一半,高度与原图相同
new_image = Image.new('RGB', (width , height), (255, 255, 255))
# 将原图的左半部分复制到新图片的左侧
left_half = image.crop((0, 0, width // 2, height))
new_image.paste(left_half, (0, 0))
# 在新图片的右侧绘制一条从上到下的虚线
# 将新图片的右侧部分设置为白色
right_half = new_image.crop((width // 2, 0, width, height))
white_image = Image.new('RGB', (width // 2, height), (255, 255, 255))
new_image.paste(white_image, (width // 2, 0))
draw = ImageDraw.Draw(new_image)
for y in range(0, height, 20): # 每10像素画一次虚线,空隙10像素
draw.line([(width // 2, y), (width // 2, y + 10)], fill=(0, 0, 0), width=10)
# 保存处理后的图片
new_image_path = os.path.join(left_path, image_file)
new_image.save(new_image_path)
# 右边房子
image_files = [f for f in os.listdir(old_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
for image_file in image_files:
# 打开图片
image_path = os.path.join(old_path, image_file)
image = Image.open(image_path)
# 获取图片的宽度和高度
width, height = image.size
# 创建一个新的空白图片,宽度为原图的一半,高度与原图相同
new_image = Image.new('RGB', (width , height), (255, 255, 255))
# 将原图的右半部分复制到新图片的右侧
right_half = image.crop((width // 2, 0, width, height))
new_image.paste(right_half, (width // 2, 0))
# 将新图片的左侧部分设置为白色
left_half = new_image.crop((0, 0, width // 2, height))
white_image = Image.new('RGB', (width // 2, height), (255, 255, 255))
new_image.paste(white_image, (0, 0))
# 在新图片的左侧绘制一条从上到下的虚线
draw = ImageDraw.Draw(new_image)
for y in range(0, height, 20): # 每10像素画一次虚线,空隙10像素
draw.line([(width // 2 - 10, y), (width // 2 - 10, y + 10)], fill=(0, 0, 0), width=10)
# 保存处理后的图片
new_image_path = os.path.join(right_path, image_file)
new_image.save(new_image_path)
# 合并图案
import os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import Image, ImageDraw, ImageFont
import os,random
print('----2、左图+右图------------')
left_files = [os.path.join(left_path, f) for f in os.listdir(left_path) if f.endswith('.jpg') or f.endswith('.png')]
right_files = [os.path.join(right_path, f) for f in os.listdir(right_path) if f.endswith('.jpg') or f.endswith('.png')]
image_files=[]
for a in range(len(left_files)):
image_files.append(left_files[a])
image_files.append(right_files[a])
print(image_files)
print(len(image_files))
# 将图片拆成8个一组
grouped_files = [image_files[i:i + 4] for i in range(0, len(image_files), 4)]
print(len(grouped_files))
# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
print('----3、插入docx,制作pdf------------')
# 处理每一组图片
for group_index, group in enumerate(grouped_files):
# 创建新的Word文档
doc = Document(path+r'\房子模板.docx')
# print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
# 如果是空格,就跳过
if not image_file:
continue
# 插入图片到单元格
table = doc.tables[0]
cell = table.cell(int(cell_index / 2), cell_index % 2)
# 如果第一行有2个格子,两个数字都写2
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(14.14), 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 PdfFileMerger
pdf_output_path = path+fr'\\01房屋对称{int(len(grouped_files))}张共{int(len(image_files)/2)}图.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(2)
# 合并零时文件里所有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)
time.sleep(2)