video write doesn't work from buffer
Hi, I'm trying to write a video from image buffer, and I have troubles with that. When I save the buffer to an image, and then read it with opencv and write to video, it works, but if I try to skip the saving process ( which makes more sense in this case), the video output is empty, even though when I compare the image I processed from buffer with the image read from disk with opencv, they are the same. What am I missing here?
writer = cv2.VideoWriter("output2.avi", cv2.VideoWriter_fourcc(*'MJPG'), 24, (WINDOW_WIDTH, WINDOW_HEIGHT))
for data in imgs:
data_array = np.frombuffer(data, np.uint8)
data_array.shape = WINDOW_WIDTH, WINDOW_HEIGHT, 4
new_data_array = cv2.cvtColor(data_array, cv2.COLOR_RGBA2BGR)
# for testing
img = Image.frombuffer("RGBA", (WINDOW_WIDTH, WINDOW_HEIGHT), data)
img.save("img.png", "png")
im = cv2.imread("img.png", 1)
print(im.all() == new_data_array.all()) # prints all true
# writer.write(new_data_array) # doesn't work
writer.write(im) # works
writer.release()
data_array.shape = WINDOW_WIDTH, WINDOW_HEIGHT, 4
dont try with 4 channel images, it cannot write more than 3.
please also check image dimensions
I convert 4 channel RGBA to 3 channel BGR using cv2.cvtColor. The final result, new_data_array is exactly the same as the image read from disk, as shown by print(im.all() == new_data_array.all()). It prints True. Yet, when I write im (the image read from disk) it works, but when I write the new_data_array, it doesn't. I don't understand how it's even possible. Their types are the same.
Actually, the problem was in transposing. In cv images they use (height, width, levels), instead of (width, height, levels). When I switched the two to make it
data_array.shape = WINDOW_HEIGHT, WINDOW_WIDTH, 4
, if finally worked. (I used a wrong equality form, I should've used(im == new_data_array).all()
instead of the one I used. It would've shown that the arrays were in fact not equal