【教学类-70-02】20240724立体拼图(9方块6图)-N套测试(蝴蝶)

d3869e48c0ff4495a74ed21529c6fb9b.png

 

 

 

背景需求

前期做了一个蝴蝶的六面图

【教学类-70-01】20240724立体拼图(9方块6图)-1套测试(蝴蝶)-CSDN博客文章浏览阅读279次,点赞11次,收藏2次。【教学类-70-01】20240724立体拼图(9方块6图)-1套测试(蝴蝶)https://blog.csdn.net/reasonsummer/article/details/140669551

这次是想测试多张图片制作9个嵌套骰子,实现立体拼图的效果。

333a84a1e0684241b84027f9a1a8bc25.png

通义万相下载的正方形彩色图片(1:1)

300ed337bdb74817a800ce16d836c89b.png

word模版(一个两页)

7e51fff92c354afa98df993db84bdfe1.png

4bedd9e438d14f819e27877b7f04e3ce.png

4cc7fb1164d24f458e5bca46be6f9f3b.png

252bc6d679734667bddead773fdf4848.png

以下是我写了8个小时代码(不用星火讯飞,套用代码改了很长时间)

1、正方形图片切割成9张

6df79694f1514396aa1b1389137c8eef.png

2、六张参考图做成嵌套列表

affdab11dafa4b63a512e659a1f716cd.png

 

3、把每张图片的第一个小图合并在一起,在6个小图组合内插入空格,组合成10个一套

78662d1c9d684eed800bf404bc77e98a.png

4、把六个原始图插入表2,做样板

dbd7cb4e8fde4f38a398913234b014b4.png

e4c1a1cab5cd4afcad377de23db58ff5.png

 

代码展示

'''
立体拼图六面积木-3套图案测试

星火讯飞、通义万项
2024年7月24日
'''

from docx import Document
from docx.shared import Cm
from PIL import Image, ImageDraw, ImageFont
from PyPDF2 import PdfFileMerger, PdfFileReader
import os,time,shutil

# 你的代码逻辑

print('------1、多套6张图片切割成6*9张---------------')
def split_image(image_path, output_folder):
    image = Image.open(image_path)
    width, height = image.size
    part_width = width // 3
    part_height = height // 3

    for i in range(3):
        for j in range(3):
            part = image.crop((i * part_width, j * part_height, (i + 1) * part_width, (j + 1) * part_height))
            part.save(os.path.join(output_folder, f"{os.path.splitext(os.path.basename(image_path))[0]}_{i}_{j}{os.path.splitext(os.path.basename(image_path))[1]}"))


path = r'C:\Users\jg2yXRZ\OneDrive\桌面\立体方块拼图'
input_folder = path + r"\01图片2"
image_folder = path + r"\02切割"
os.makedirs(image_folder, exist_ok=True)


for file in os.listdir(input_folder):
    if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
        image_path = os.path.join(input_folder, file)
        
        split_image(image_path, image_folder)  # 修改为传入image_path而不是image_folder

print('------2、多套6张图片作为参考图---------------')

small_image1= [f for f in os.listdir(input_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
print(small_image1)

small_image= [small_image1[i:i +6] for i in range(0, len(small_image1),6)]

# small_image = [small_image2]
print(small_image)
print(len(small_image))
# 制作成[[],[]]的样式


print('------3、一套6张图片54张图片,把每张图片的第一张图片放在一起,插入空格,,做成列表--------------')     
# print('--------制作图片 ----------')     
# # 图片选择
image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]
print(image_files)
print(len(image_files))
# 162
# 有几套
z=int((len(image_files))/54)

# 将图片拆成9个一组
group_files = [image_files[i:i +54] for i in range(0, len(image_files),54)]
print(group_files)
print(len(group_files))
# 3
X=[]
for x in range(len(group_files)):
    xx=[]
    for x1 in range(6):
        xx.append(group_files[x][x1*9:x1*9+9])
    X.append(xx)
print(X)
print(len(X))
# 3

grouped_files1=[]  
for g in range(9):         
    for k in range(z):    # 3
        gg=[] 
        for h in range(len(X[k])): # 6
            print(h)
            gg.append(X[k][h][g])
        grouped_files1.append(gg)

print( grouped_files1)
print(len(grouped_files1))   

# 在每个子列表的第4个位置插入一个空字符串
for group in grouped_files1:
    group.insert(4, '')
    group.insert(5, '')
    group.insert(7, '')
    group.insert(9, '')

print(grouped_files1)
print(len(grouped_files1))
# 27
DD=[]
for d in range(z):
    chooice = [grouped_files1[i * z + d ] for i in range(len(grouped_files1) // z)]  
    DD.append(chooice)
   
print(DD)
print(len(DD))

grouped_files4 = [item for sublist in DD for item in sublist]
print(grouped_files4)
print(len(grouped_files4))

grouped_files3 = [item for sublist in grouped_files4 for item in sublist]
print(grouped_files3)  # 输出: []


# [[60个],[]]
grouped_files = [grouped_files3[i:i +90] for i in range(0, len(grouped_files3),90)]

print(grouped_files)
print(len(grouped_files))
# 3

#  创建临时文件夹
new_folder = path+r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)

print('------4、小图片插入插入9*10的单元格里---------------')    
# # 处理每一组图片
for group_index, group in enumerate(grouped_files):
    # 创建新的Word文档
    doc = Document(path+r'\立体方块拼图模版1页.docx')
    # print(group)
    
    # 遍历每个单元格,并插入图片
    for cell_index, image_file in enumerate(group):
        # 计算图片长宽(单位:厘米)
        print(image_file)
        # 插入图片到单元格
        table = doc.tables[0]
        cell = table.cell(int(cell_index / 10), cell_index % 10)
        # 如果第一行有4个格子,两个数字都写4
        cell_paragraph = cell.paragraphs[0]
        cell_paragraph.clear()
        run = cell_paragraph.add_run()
        if image_file !='':
            run.add_picture(os.path.join(image_folder, image_file), width=Cm(3.2), height=Cm(3.2))
        else:
            pass
        
    # 保存Word文档
    doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
    time.sleep(5)

print('------5、参考图入1*6的单元格里---------------')   
for group_index, group in enumerate(small_image):
    # 创建新的Word文档
   
    doc = Document(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
    print(group)
    
    # 遍历每个单元格,并插入图片
    for cell_index, image_file in enumerate(group):
        # 计算图片长宽(单位:厘米)
        print(image_file)
        # 插入图片到单元格
        table = doc.tables[1]
        cell = table.cell(int(cell_index / 6), cell_index % 6)
        # 如果第一行有4个格子,两个数字都写4
        cell_paragraph = cell.paragraphs[0]
        cell_paragraph.clear()
        run = cell_paragraph.add_run()
      
        run.add_picture(os.path.join(input_folder, image_file), width=Cm(4.7), height=Cm(4.7))
        
        # 保存Word文档
    doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
    time.sleep(5)

print('------6、合并PDF---------------')   
# 将10个docx转为PDF
import os,time
from docx2pdf import convert
from PyPDF2 import PdfFileMerger

pdf_output_path = path+fr'\\立方体{int(len(grouped_files))}张小图{int(len(image_files)/6)}个方体.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(5)


# 合并零时文件里所有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(3)

# 保存合并后的PDF文件
merger.write(pdf_output_path)
merger.close()


# 删除输出文件夹

shutil.rmtree(new_folder)
shutil.rmtree(image_folder)
# shutil.rmtree(new)
time.sleep(2)

本次是3套图片

1d136f3b16bc458caf208183e6d31440.png59c92aeacae142bbad1a245bf89b5c21.png

 

 

 dbd7cb4e8fde4f38a398913234b014b4.png

309ab4f589a541148d1c6985858f0e79.png

e4c1a1cab5cd4afcad377de23db58ff5.png

背景需求

下次去学校打印出来,再展示9个立方体的制作方法吧。

 

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
极验3.0滑动拼图验证是一种常用的人机验证方式,可以有效防止机器恶意攻击,保障网站安全。在Java中,可以通过调用极验的API来实现滑动验证功能。下面是一个简单的使用示例: 1. 在极验官网申请账号,并创建一个验证项目,获得验证ID和密钥。 2. 下载极验的Java SDK,解压后将其中的geetest-lib.jar文件添加到项目的classpath中。 3. 在Java代码中调用极验API实现验证功能,示例代码如下: ```java import com.geetest.sdk.GTConfig; import com.geetest.sdk.GeetestLib; public class GeetestVerify { private static final String GEETEST_ID = "your_geetest_id"; // 极验验证ID private static final String GEETEST_KEY = "your_geetest_key"; // 极验验证密钥 public static boolean verify(String challenge, String validate, String seccode) { GeetestLib gtSdk = new GeetestLib(GEETEST_ID, GEETEST_KEY); GTConfig config = new GTConfig(); config.setCaptchaId(GEETEST_ID); config.setPrivateKey(GEETEST_KEY); gtSdk.setConfig(config); // 自定义参数,可选择添加 // Map<String, String> paramMap = new HashMap<>(); // paramMap.put("user_id", "your_user_id"); // paramMap.put("client_type", "web"); // paramMap.put("ip_address", "127.0.0.1"); // 调用验证接口 int result = gtSdk.enhencedValidateRequest(challenge, validate, seccode, null); // 验证结果,0表示成功,1表示失败 return result == 0; } } ``` 4. 在前端页面中嵌入极验验证组件,具体实现方式可参考极验官网提供的相关文档和示例代码。在用户完成验证后,将验证结果传递给后台Java程序进行验证,通过调用上述示例代码实现验证功能即可。 总的来说,Java实现极验滑动验证相对较为简单,只需要调用极验提供的Java SDK即可。需要注意的是,极验官网提供的Java SDK版本可能会更新,需要及时更新SDK文件以保证验证功能的正常运作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿夏reasonsummer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值