Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

import os inputpath = input("请输入需要拆分的合并后图片的路径:") # 输入合并后图片的路径 outputdir =…

  1. User

    import os

    input_path = input("请输入需要拆分的合并后图片的路径:") # 输入合并后图片的路径 output_dir = input("请输入拆分后图片序列的保存路径:") # 输出拆分后图片序列的文件夹路径 grid_size = input("请输入拆分网格的行数和列数,用'x'连接:") # 用户输入拆分网格的行数和列数,比如 2x2、3x3 row_num, column_num = map(int, grid_size.strip().split('x'))

    index = 0

    创建输出文件夹

    if not os.path.exists(output_dir): os.makedirs(output_dir)

    遍历输入路径下所有图片文件,执行图片拆分操作

    for file in os.listdir(input_path): if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"): img_path = os.path.join(input_path, file) # 获取输入图片的宽和高 iw, ih, _ = os.popen('ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 {}'.format(img_path)).read().split('x') iw, ih = int(iw.strip()), int(ih.strip())

    # 使用ffmpeg命令行工具进行图片拆分
    for y in range(row_num):
      for x in range(column_num):
        # 构建输出文件路径
        index += 1
        output_path = os.path.join(output_dir, f"{str(index).zfill(3)}.jpeg")
        # 拆分图片
        crop_w = iw / column_num
        crop_h = ih / row_num
        os.system('ffmpeg -i {} -vf "crop={}:{}:{}:{}" {}'.format(img_path, crop_w, crop_h, crop_w * x, crop_h * y, output_path))
        # 输出拆分后的图片序列路径
        print("图片 {} 拆分后的图片序列路径为:{}".format(file, output_path))  错误信息:C:\Users\96394\Videos\test>python split.py
    

    请输入需要拆分的合并后图片的路径:./16/merged 请输入拆分后图片序列的保存路径:./16/split 请输入拆分网格的行数和列数,用'x'连接:3x3 Traceback (most recent call last): File "C:\Users\96394\Videos\test\split.py", line 82, in iw, ih, _ = os.popen('ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 {}'.format(img_path)).read().split('x') ValueError: not enough values to unpack (expected 3, got 2)

  2. Assistant

    这个错误是由于使用ffprobe命令获取视频宽度、高度时没有输出帧率(fps)信息,导致返回的结果只有两个值而不是三个,造成无法进行拆分。可以尝试修改一下命令,加上输出帧率的信息:

    ffprobe -v error -select_streams v:0 -show_entries stream=width,height,r_frame_rate -of csv=s=x:p=0 {} | tr '/' ' '
    

    这里使用了tr命令将帧率中的/替换为空格,便于后续处理。同时修改拆分命令的格式,将视频帧率也加入到命令中:

    os.system('ffmpeg -i {} -vf "crop={}:{}:{}:{}" -r {} {}'.format(img_path, crop_w, crop_h, crop_w * x, crop_h * y, fps, output_path))
    

    这样就可以获取到三个值,分别为视频宽度、高度和帧率,成功进行拆分操作。完整代码如下:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.