【教学类-70-02】20240726六片灯笼(外圆内圆、外方内圆、外方内方)

背景需求:

前期用圆形花边,制作了内部圆形和方形的圆镜子。

【教学类-70-01】20240722镜子花边(适配5CM圆镜)-CSDN博客文章浏览阅读768次。【教学类-70-01】20240722镜子花边(适配5CM圆镜)https://blog.csdn.net/reasonsummer/article/details/140601711

现在我想用一个花边图案,做六个一样的图形,制作圆灯笼、方盒灯笼。

模板样式

边框线加粗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)

总之做成六片,空间镂空,做一个六面体的灯笼

 

8月底去学校再把图纸打印出来

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值