【问题标题】:Converting a sequence of ppm images to video with python使用python将一系列ppm图像转换为视频
【发布时间】:2020-05-22 18:22:31
【问题描述】:

我正在尝试使用 IPython (python 2.7) 从 ppm 图像制作视频。

我写了这段代码:

import cv2
import glob

img1 = cv2.imread('C:/Users/Joseph/image0.ppm')
height, width, layers = img1.shape

video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height))

filenames = glob.glob('C:/Users/Joseph/*.ppm')
for filename in filenames:
    print(filename)
    img = cv2.imread(filename)
    video1.write(img)

cv2.destroyAllWindows()
video1.release()

视频已创建但为空size=0B,无法打开。

没有错误信息。

我怀疑问题出在位置的书写上,因为print(filename) 产生:

C:/Users/Joseph\image0.ppm

C:/Users/Joseph\image1.ppm

C:/Users/Joseph\image2.ppm

C:/Users/Joseph\image2.ppm

而不是我的预期:C:/Users/Joseph/image0.ppm

你能帮帮我吗?

编辑:文件类型为type: GIMP 2.10.14 (.ppm)。问题是否与这种类型的 ppm 有关?

编辑 2: 问题似乎与.ppm 没有直接关系。

确实,我试过了(考虑到Rotem的回答):

import cv2
import glob

i = cv2.imread('C:/Users/Joseph/image0.ppm')
cv2.imwrite('C:/Users/Joseph/image.jpg',i)


img1 = cv2.imread('C:/Users/Joseph/image.jpg')
height, width, layers = img1.shape

# Set FOURCC code to '24BG' - '24BG' is used for creating uncompressed raw video
video1 = cv2.VideoWriter('video1.avi', cv2.VideoWriter_fourcc('D','I','B',' '), 1, (width, height))

filenames = glob.glob('C:/Users/Joseph/*.ppm')

try:
    for filename in filenames:
        print(filename)
        img = cv2.imread(filename)
        cv2.imwrite('C:/Users/Joseph/a.jpg',img)
        img=cv2.imread('C:/Users/Joseph/a.jpg')
        # Display input image for debugging
        cv2.imshow('img', img)
        cv2.waitKey(1000)
        video1.write(img)
except:
     print('An error occurred.')

cv2.destroyAllWindows()
video1.release()

而且它也不起作用。而且我没有显示任何图像。

所以这似乎是我的 cv2 视频错误。 jpg 制作的很好。

编辑:解决方案。

本着 rotem 的回答精神,我尝试了: cv2.VideoWriter_fourcc('M','J','P','G') 并且成功了!

【问题讨论】:

    标签: python-2.7 opencv video gimp ppm


    【解决方案1】:

    获取空视频文件的原因有多种,但路径看起来正确。

    在Windows系统中C:/Users/Joseph\image0.ppmC:/Users/Joseph/image0.ppm是一样的。

    • 手动删除video1.avi文件,只是为了确保文件没有被锁定。

    我认为问题涉及video codec,但我不能确定。

    在命令video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height)) 中,第二个参数是FOURCC 代码,用于选择视频编码器的视频编解码器。
    将值设置为 -1 时,会打开一个对话框,让您选择编解码器。
    在旧版本的 OpenCV 中,它并不总是有效。

    尝试将 FOURCC 设置为 'DIB ',应用“基本 Windows 位图格式”。
    使用它来创建原始(未压缩)的 AVI 视频文件。

    代码如下:

    import cv2
    import glob
    
    img1 = cv2.imread('C:/Users/Joseph/image0.ppm')
    height, width, layers = img1.shape
    
    # Set FOURCC code to '24BG' - '24BG' is used for creating uncompressed raw video
    video1 = cv2.VideoWriter('video1.avi', cv2.VideoWriter_fourcc('D','I','B',' '), 1, (width, height))
    
    filenames = glob.glob('*.ppm')
    
    try:
        for filename in filenames:
            print(filename)
            img = cv2.imread(filename)
    
            # Display input image for debugging
            cv2.imshow('img', img)
            cv2.waitKey(1000)
    
            video1.write(img)
    except:
         print('An error occurred.')
    
    cv2.destroyAllWindows()
    video1.release()
    
    • 我添加了cv2.imshow('img', img) 来帮助您调试问题,以防不是编解码器问题。
    • 确保您没有遇到任何异常。

    如果我的回答解决了您的问题,请拒绝。

    【讨论】:

    • 谢谢!但它仍然不起作用。奇怪的是,我也在另一台电脑上试过,它也不起作用。但我可以用 GIMP 很好地查看图像。我还编辑了关于 GIMP 的编辑,因为我只是想到了这一点。
    • 我忘了说也没有错误信息
    • 尝试:以 PNG 格式保存一些图像(在 GIMP 中使用另存为)。只是为了确保问题不是 PPM 格式,请尝试从 PNG 创建视频。
    • 您也可以尝试使用 Python 3 和更新版本的 OpenCV。相同的代码兼容 Python 3
    猜你喜欢
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2021-05-12
    • 2013-11-30
    • 2014-12-16
    相关资源
    最近更新 更多