Writing images

Similar to the wrapper for decoding images, Vuba also provides a convenience wrapper for enconding footage in addition: vuba.Writer. Just like vuba.Video, this wrapper has the same API for all footage formats.

Initiation

Similar to vuba.Video, vuba.Writer has an equivalent API regardless of footage format. The following code demonstrates a couple of ways one can initiate this wrapper:

To encode to a movie:

In [1]: import vuba

# Initiate with minimal parameters
In [2]: writer = vuba.Writer('./output.avi', fps=15, resolution=(1000, 1000))
# Custom codec (default is MJPG)
In [3]: writer = vuba.Writer('./output.avi', fps=15, resolution=(1000,1000), codec='MPNG')
# Encode video in grayscale format
In [4]: writer = vuba.Writer('./output.avi', fps=15, resolution=(1000,1000), grayscale=True)

To encode to individual images:

# Write to individual images
In [5]: writer = vuba.Writer([f"./{i}.png" for i in range(100)], resolution=(1000, 1000))

Note that you can also supply an instance of vuba.Video if you wish to encode footage in the same format as the source video:

# Write to an AVI of the same format as the input video
In [6]: video = vuba.Video('../examples/example_data/raw_video/test.avi')

In [7]: writer = vuba.Writer('./output.avi', video)
# Write to individual images from multiple images
In [8]: video = vuba.Video([f"../examples/example_data/raw_images/{i}.png" for i in range(100)])

In [9]: writer = vuba.Writer([f"./{i}.png" for i in range(len(video))], video)

Also note that these are interchangeable, i.e. you can supply a ‘movie’ instance of vuba.Video to an ‘individual images’ instance of vuba.Writer.

Writing frames

All writing operations are handled by Writer.write. This method encapsulates encoders that enable writing to individual images and movies. Moreover, it will also resize and convert supplied frames to the resolution and format declared at initiation, if needed. Attempting to write frames in the wrong colour space or resolution has been one of the most common failure points in our own applications, and the error messages supplied by OpenCV are not of much use when debugging these scenarios. Thus, we have provided built-in operations and warnings in Writer.write to help in this particular case.

Now, when it actually comes to writing images to a given output, the syntax is the same regardless of the input or output footage format:

# Read the first frame
In [10]: frame = video.read(index=0)

# And write it
In [11]: writer.write(frame)

And that’s it!

See also

For additional example scripts that cover usage of this module in more depth, see the following: