背景需求:
前期用圆形花边,制作了内部圆形和方形的圆镜子。
现在我想用一个花边图案,做六个一样的图形,制作圆灯笼、方盒灯笼。
模板样式
边框线加粗2.25磅
选择外圈是圆形的花纹图案,边缘是正圆(不是有花边的圆)
选择外圈是方形的花纹图案,边缘是正方
第一款:圆形灯笼6图(外圆内圆)
代码展示:
'''
圆形灯笼6图(外圆内圆)
星火讯飞、通义万相 阿夏
2024年7月25日
'''
import os,time
from PIL import Image, ImageDraw
import shutil
from docx import Document
from docx.shared import Cm
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import ImageFont
import random
name='01圆形灯笼图片'
# name='02方形灯笼图片'
print('-----1、测试镜子5CM直径,覆盖一个4.5CM的黑线白填充圆形和一个4CM的正方形--------')
def draw_circle(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 设置圆的半径(假设每像素代表1CM)dpi=2400
radius = int(2.25 * 110) # 转换为像素
# print(radius)
# 画一个红色的圆形
draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), outline="black",fill='white', width=10)
# 保存图片
image.save(output_path)
def draw_square(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 添加正方形
square_size = int(4.5 * 110) # 转换为像素
draw.rectangle((center_x - square_size // 2, center_y - square_size // 2, center_x + square_size // 2, center_y + square_size // 2), outline="black", fill="white",width=10)
# 保存图片
image.save(output_path)
# 获取123文件夹下的所有图片文件
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\圆形灯笼'
folder_path =path+fr"\{name}"
zheng_path=path+r"\03正面"
# fan_path=path+r"\04反面"
os.makedirs(zheng_path,exist_ok=True)
# os.makedirs(fan_path,exist_ok=True)
image_files = [f for f in os.listdir(folder_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 遍历所有图片文件,处理并保存圆形和正方形分别保存在两个文件夹里
for image_file in image_files:
input_path = os.path.join(folder_path, image_file)
output_path_circle = os.path.join(zheng_path, image_file)
# output_path_square = os.path.join(zheng_path, image_file)
# output_path_square = os.path.join(fan_path, image_file)
draw_circle(input_path, output_path_circle)
# draw_square(input_path, output_path_square)
# print(zheng_path)
# print('----2、读取两个图,分别插入docx,制作PDF---------')
zheng_files = [f for f in os.listdir(zheng_path) if f.endswith('.jpg') or f.endswith('.png')]
# fan_files = [f for f in os.listdir(fan_path) if f.endswith('.jpg') or f.endswith('.png')]
# 正面图6张
image_files_all=[]
for i in range(len(zheng_files)):
for aa in range(6):
image_files_all.append(os.path.join(zheng_path, zheng_files[i]))
# image_files_all.append(os.path.join(fan_path, fan_files[i]))
# print(image_files_all)
# 将图片拆成6个一组
grouped_files = [image_files_all[i:i + 6] for i in range(0, len(image_files_all), 6)]
print(len(grouped_files))
# 53
# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
# 处理每一组图片
for group_index, group in enumerate(grouped_files):
print(group)
# 创建新的Word文档
doc = Document(path+r'\圆镜灯笼模版.docx')
# print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
print(image_file)
# 插入图片到单元格
table = doc.tables[0]
cell = table.cell(int(cell_index / 2), cell_index % 2)
# 如果第一行有4个格子,两个数字都写4
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(9.31), height=Cm(9.31))
# 保存Word文档
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
time.sleep(5)
# 将10个docx转为PDF
import os
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
pdf_output_path = path+fr'\\{name}外圆内圆_{len(image_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(zheng_path)
# shutil.rmtree(fan_path)
time.sleep(10)
第二款:圆形灯笼6图(外方内圆)
代码展示:
'''
圆形灯笼6图(外方内圆)
星火讯飞、通义万相 阿夏
2024年7月25日
'''
import os,time
from PIL import Image, ImageDraw
import shutil
from docx import Document
from docx.shared import Cm
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import ImageFont
import random
#
# name='01圆形灯笼图片'
name='02方形灯笼图片'
print('-----1、测试镜子5CM直径,覆盖一个4.5CM的黑线白填充圆形和一个4CM的正方形--------')
def draw_circle(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 设置圆的半径(假设每像素代表1CM)dpi=2400
radius = int(2.25 * 110) # 转换为像素
# print(radius)
# 画一个红色的圆形
draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), outline="black",fill='white', width=10)
# 保存图片
image.save(output_path)
def draw_square(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 添加正方形
square_size = int(4.5 * 110) # 转换为像素
draw.rectangle((center_x - square_size // 2, center_y - square_size // 2, center_x + square_size // 2, center_y + square_size // 2), outline="black", fill="white",width=10)
# 保存图片
image.save(output_path)
# 获取123文件夹下的所有图片文件
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\圆形灯笼'
folder_path =path+fr"\{name}"
zheng_path=path+r"\03正面"
# fan_path=path+r"\04反面"
os.makedirs(zheng_path,exist_ok=True)
# os.makedirs(fan_path,exist_ok=True)
image_files = [f for f in os.listdir(folder_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 遍历所有图片文件,处理并保存圆形和正方形分别保存在两个文件夹里
for image_file in image_files:
input_path = os.path.join(folder_path, image_file)
output_path_circle = os.path.join(zheng_path, image_file)
# output_path_square = os.path.join(zheng_path, image_file)
# output_path_square = os.path.join(fan_path, image_file)
draw_circle(input_path, output_path_circle)
# draw_square(input_path, output_path_square)
# print(zheng_path)
# print('----2、读取两个图,分别插入docx,制作PDF---------')
zheng_files = [f for f in os.listdir(zheng_path) if f.endswith('.jpg') or f.endswith('.png')]
# fan_files = [f for f in os.listdir(fan_path) if f.endswith('.jpg') or f.endswith('.png')]
# 正面图6张
image_files_all=[]
for i in range(len(zheng_files)):
for aa in range(6):
image_files_all.append(os.path.join(zheng_path, zheng_files[i]))
# image_files_all.append(os.path.join(fan_path, fan_files[i]))
# print(image_files_all)
# 将图片拆成6个一组
grouped_files = [image_files_all[i:i + 6] for i in range(0, len(image_files_all), 6)]
print(len(grouped_files))
# 53
# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
# 处理每一组图片
for group_index, group in enumerate(grouped_files):
print(group)
# 创建新的Word文档
doc = Document(path+r'\圆镜灯笼模版.docx')
# print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
print(image_file)
# 插入图片到单元格
table = doc.tables[0]
cell = table.cell(int(cell_index / 2), cell_index % 2)
# 如果第一行有4个格子,两个数字都写4
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(9.31), height=Cm(9.31))
# 保存Word文档
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
time.sleep(5)
# 将10个docx转为PDF
import os
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
pdf_output_path = path+fr'\\{name}外方内圆_{len(image_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(zheng_path)
# shutil.rmtree(fan_path)
time.sleep(10)
第三款:圆形灯笼6图(外方内方)
代码展示:
'''
圆形灯笼6图(外方内方)
星火讯飞、通义万相 阿夏
2024年7月25日
'''
import os,time
from PIL import Image, ImageDraw
import shutil
from docx import Document
from docx.shared import Cm
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import ImageFont
import random
# name='01圆形灯笼图片'
name='02方形灯笼图片'
print('-----1、测试镜子5CM直径,覆盖一个4.5CM的黑线白填充圆形和一个4CM的正方形--------')
def draw_circle(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 设置圆的半径(假设每像素代表1CM)dpi=2400
radius = int(2.25 * 110) # 转换为像素
# print(radius)
# 画一个红色的圆形
draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), outline="black",fill='white', width=10)
# 保存图片
image.save(output_path)
def draw_square(image_path, output_path):
# 打开图片
image = Image.open(image_path)
width, height = image.size
# 计算中心点
center_x = width // 2
center_y = height // 2
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(image)
# 添加正方形
square_size = int(4.5 * 110) # 转换为像素
draw.rectangle((center_x - square_size // 2, center_y - square_size // 2, center_x + square_size // 2, center_y + square_size // 2), outline="black", fill="white",width=10)
# 保存图片
image.save(output_path)
# 获取123文件夹下的所有图片文件
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\圆形灯笼'
folder_path =path+fr"\{name}"
zheng_path=path+r"\03正面"
# fan_path=path+r"\04反面"
os.makedirs(zheng_path,exist_ok=True)
# os.makedirs(fan_path,exist_ok=True)
image_files = [f for f in os.listdir(folder_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# 遍历所有图片文件,处理并保存圆形和正方形分别保存在两个文件夹里
for image_file in image_files:
input_path = os.path.join(folder_path, image_file)
# output_path_circle = os.path.join(zheng_path, image_file)
output_path_square = os.path.join(zheng_path, image_file)
# output_path_square = os.path.join(fan_path, image_file)
# draw_circle(input_path, output_path_circle)
draw_square(input_path, output_path_square)
# print(zheng_path)
# print('----2、读取两个图,分别插入docx,制作PDF---------')
zheng_files = [f for f in os.listdir(zheng_path) if f.endswith('.jpg') or f.endswith('.png')]
# fan_files = [f for f in os.listdir(fan_path) if f.endswith('.jpg') or f.endswith('.png')]
# 正面图6张
image_files_all=[]
for i in range(len(zheng_files)):
for aa in range(6):
image_files_all.append(os.path.join(zheng_path, zheng_files[i]))
# image_files_all.append(os.path.join(fan_path, fan_files[i]))
# print(image_files_all)
# 将图片拆成6个一组
grouped_files = [image_files_all[i:i + 6] for i in range(0, len(image_files_all), 6)]
print(len(grouped_files))
# 53
# 创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
# 处理每一组图片
for group_index, group in enumerate(grouped_files):
print(group)
# 创建新的Word文档
doc = Document(path+r'\圆镜灯笼模版.docx')
# print(group)
# 遍历每个单元格,并插入图片
for cell_index, image_file in enumerate(group):
# 计算图片长宽(单位:厘米)
print(image_file)
# 插入图片到单元格
table = doc.tables[0]
cell = table.cell(int(cell_index / 2), cell_index % 2)
# 如果第一行有4个格子,两个数字都写4
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(9.31), height=Cm(9.31))
# 保存Word文档
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
time.sleep(5)
# 将10个docx转为PDF
import os
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
pdf_output_path = path+fr'\\{name}外方内方_{len(image_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(zheng_path)
# shutil.rmtree(fan_path)
time.sleep(10)
总之做成六片,空间镂空,做一个六面体的灯笼