ContinuumIO / anaconda-issues

Anaconda issue tracking
646 stars 220 forks source link

opencv - not writing output with cv2.VideoWriter.write #223

Open mattivi opened 9 years ago

mattivi commented 9 years ago

OpenCV is not writing to output video files with cv2.VideoWriter.write. I only get an empty video file (8 KB) and no error messages. Where is the problem? Thanks a lot! I am running 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46) [GCC 4.2.1 (Apple Inc. build 5577)] opencv 2.4.8 np17py27_2

Here is a sample test code that write an image to a video output file:

import cv2
import numpy as np
import Image

#read image
fname_img_02 = "test.png"
img_02 = Image.open(fname_img_02)
#convert to cv2 image
img_out_cv2 = np.array(img_02)
#convert from RGB of PIL to BGR of OpenCV
img_out_cv2 = img_out_cv2[:, :, ::-1].copy()
frameW = img_02.size[0]
frameH = img_02.size[1]

fourcc =  cv2.cv.CV_FOURCC(*'XVID')
video = cv2.VideoWriter("output.avi", fourcc, 2, (frameW,frameH), 1)
nFrames = 20

for i in range(nFrames):
    video.write(img_out_cv2)

video.release()
inostia commented 3 years ago

Another tip: make sure the directory you're writing to exists. VideoWriter will not create it, but it will not fail.

thematrixdev commented 2 years ago

I am facing this issue in 2022. Correct framerate, resolution, etc. And the capture is released.

thematrixdev commented 2 years ago

If you trying to output H264 this may help https://github.com/opencv/opencv-python/issues/207

XiaoBaiCanDo commented 2 years ago

I came to the same problem and I successed only after replacing '/' with '//'.

hamzakhalil798 commented 1 year ago

@Hashemian01 worked for me thankyou!