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

背景需求

6月小班个别化学习观摩时,看到一个小班益智区有这个玩具

这个让我想到我设计的“两嵌套骰子”

于是我想要这个代码制作9个嵌套骰子,实现立体拼图的效果。

91:1

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

word模版(一个两页)

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

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

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

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

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

代码展示

'''
立体拼图六面积木-一套6张蝴蝶测试
星火讯飞、通义万项
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图片1"
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]
print(small_image)
print(len(small_image))
# 制作成[[],[]]的样式


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

# 将图片拆成9个一组
group_files = [image_files[i:i +9] for i in range(0, len(image_files),9)]


print(group_files)
print(len(group_files))
# 6

# # 分别提取9个里面的第1张,共6张
grouped_files1=[]
for h in range(9):
    gg=[]
    for g in range(len(group_files)):    
        gg.append(group_files[g][h]) 
    grouped_files1.append(gg)    


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

print(len(grouped_files1))
# 9

grouped_files2 = [item for sublist in grouped_files1 for item in sublist]
print(len(grouped_files2))
# 90

grouped_files = [grouped_files2]
print(grouped_files)
print(len(grouped_files))

#  创建临时文件夹
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.3), height=Cm(3.3))
        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
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'))


# 合并零时文件里所有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(image_folder )
# shutil.rmtree(new)
time.sleep(2)

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

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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、付费专栏及课程。

余额充值