python-2.7opencvvideogimpppm

Converting a sequence of ppm images to video with python


I'm trying to make a video out of ppm images using IPython (python 2.7).

I wrote this code :

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()

The video is created but is empty size=0B and it cannot be opened.

There is no error message.

I suspect the problem is the writing of the location, since print(filename) yields :

C:/Users/Joseph\image0.ppm

C:/Users/Joseph\image1.ppm

C:/Users/Joseph\image2.ppm

C:/Users/Joseph\image2.ppm

instead of what I had expected : C:/Users/Joseph/image0.ppm

Could you help me please?

EDIT: The type of file is type: GIMP 2.10.14 (.ppm). Could the issue be connected to this type of ppm ?

EDIT 2: It seems that the issue is not connected directly to .ppm.

Indeed, I tried (taking into account the answer of 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()

And it doesn't work either. And I don't get any image displayed.

so it seems that it's an error in my cv2 for video. The jpg is well created.

EDIT : The solution.

In the spirit of the answer of rotem, I tried : cv2.VideoWriter_fourcc('M','J','P','G') and it worked !


Solution

  • There are multiple reasons for getting an empty video file, but the path looks correct.

    In Windows system C:/Users/Joseph\image0.ppm and C:/Users/Joseph/image0.ppm are the same.

    I think the issue involved video codec, but I can't be sure about it.

    in the command video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height)), the second parameter is a FOURCC code that selects the video codec for the video encoder.
    When setting the value to -1, a dialog box is opened letting you select the codec.
    In older versions of OpenCV it's not always working.

    Try setting the FOURCC to 'DIB ', applies "Basic Windows bitmap format".
    Use it for creating raw (uncompressed) AVI video file.

    Here is the code:

    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()
    

    Please let me no if my answer solves your problem.