【教学类-73-01】20240804镂空瓶子01

背景需求:

瓶子里的春天呀! - 小红书 (xiaohongshu.com)https://www.xiaohongshu.com/explore/63ef87f8000000000703acae?app_platform=android&ignoreEngage=true&app_version=8.47.0&share_from_user_hidden=true&xsec_source=app_share&type=normal&xsec_token=CBpundBBTrUJLdU3L5pYm0gLJYMp3HokY9qPe994qdlwI=&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1722745595&share_id=4be83769e22643f78607bc3976f0b4ab

首先用通义万相下载了一堆空瓶子

部分图片黑色多,白色少,

所有图片都用ps修图

尽量让中间的瓶内空白部分多一点

素材准备:

代码展示:

python 复制代码
'''
黑白瓶子
星火讯飞、阿夏
2024年8月4日
'''


import os
from PIL import Image, ImageDraw

print('----1、房屋切割成左右对称两片------------')

path=r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白瓶子'
old_path=path+r'\01黑白瓶子'


# 获取文件夹下的所有图片文件
image_file = [os.path.join(old_path, f) for f in os.listdir(old_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
print(image_file)

# 合并图案

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



image_files=[]
for a in range(len(image_file)):    
    image_files.append(image_file[a])
    image_files.append('')
print(image_files)
print(len(image_files))


# 将图片拆成8个一组
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)

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(9.75), height=Cm(9.35))
        
    # 保存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)

把瓶子中间镂空,背后衬纸,然后用彩色纸片从下塞入,模拟瓶中的彩色果汁

相关推荐
FreakStudio32 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal2 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali2 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ2 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.3 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~3 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算
爆更小小刘3 小时前
Python基础语法(3)下
开发语言·python
哪 吒3 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od
憨憨小白3 小时前
Python 的集合类型
开发语言·python·青少年编程·少儿编程
白杆杆红伞伞4 小时前
01_快速入门
python·pandas