Ask Your Question
0

Simple example of writing color imagery using VideoWriter in python

asked 2016-07-29 11:53:11 -0500

idg101 gravatar image

updated 2016-07-29 14:04:45 -0500

I want to be able to write out a color raster for each video from in python. However, when I pass a 3d array to video writer, it generates a bad output. What am I doing wrong?

Sampled code below....

 h,w = raster.shape
cmap = hot()
writer = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('P','I','M','1'), 25, (h,w), True)    
for i in range(len(images)):
    x = np.transpose((dsp.normalize(images[i]**(0.5))*255).astype('uint8'))
    x  = (cmap(x)*255).astype('uint8')
    for k in range(25):
        writer.write(x)  
del writer

EDIT 1: Here is a simplier, stand alone version of the problem. This hangs VLC.

import cv2
import numpy as np
x = np.random.randint(0,255,(100,100,3)).astype('uint8')
writer = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('P','I','M','1'), 25, (100,100), True)
for k in range(100):
  writer.write(x)
del writer
edit retag flag offensive close merge delete

Comments

"it generates a bad output" -- meaning what, exactly ?

ohh, btw, it should be (w,h), not (h,w)

berak gravatar imageberak ( 2016-07-29 12:12:02 -0500 )edit

The avi file has size to it on disk, but VLC player wont play it. VLC doesnt give me an error, it simply hangs.

idg101 gravatar imageidg101 ( 2016-07-29 12:34:26 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-07-29 14:06:32 -0500

idg101 gravatar image

updated 2016-07-29 14:27:49 -0500

Solution (i am not waiting 2 days to answer my own question).

import cv2
import numpy as np
x = np.random.randint(0,255,(100,100,3)).astype('uint8')
writer = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 25, (100,100), True)
for k in range(100):
  writer.write(x)
writer.release()

EDIT 1:

I just changed video form to MJPG and it worked. I still have no idea why PIM1 doesnt work. It works on my other computer just fine.

edit flag offensive delete link more

Comments

extra karma for explaining the difference to your original problem !

berak gravatar imageberak ( 2016-07-29 14:23:51 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-07-29 11:53:11 -0500

Seen: 1,013 times

Last updated: Jul 29 '16