generativepy.movie module


Martin McBride, 2020-07-27
Tags movie video
Categories generativepy generative art

The drawing module introduced earlier works by creating a Pycairo surface, calling a draw function to draw on it, then saving the result as a PNG file.

The movie module works in a slightly different way. It creates a Pycairo surface, calls the same type of draw function to draw on it, but it then converts the result to an in-memory image. More specifically, it saves the image as a NumPy array.

The in memory image can then be processed in other ways before finally getting saved as a PNG file.

The reason for this is that the drawing module usually operates on single images, but the movie module generally operates on a collection of images, each representing one frame of a video. Even a video that is only a few minutes long can have thousands of frames.

By storing images in memory, we can set up an efficient processing pipeline to process the frames. We can then process the entire video by treating it as a lazy iterable that returns frames. The frames pass through the pipeline, one by one.

make_frames

make_frames(draw, width, height, count, channels=3)
Parameter Type Description
draw Function A drawing function object, see make_image.
width int The width of each image that will be created, in pixels.
height int The height of each image that will be created, in pixels.
count int The total number of images that will be created.
channels int The number of colour channels. 3 for RGB, 4 for RGBA.

This function is similar to make_images, but instead of creating a set of PNG files, it returns a lazy iterable of NumPy image arrays.

make_frame

make_frame(draw, width, height, channels=3)
Parameter Type Description
draw Function A drawing function object, see make_image.
width int The width of each image that will be created, in pixels.
height int The height of each image that will be created, in pixels.
channels int The number of colour channels. 3 for RGB, 4 for RGBA.

This function is similar to make_frames, but it returns a single frame rather than an iterable.

save_frames

save_frames(outfile, frames)
Parameter Type Description
outfile String The path and base filename for the output PNG files. See below.
frames iterable An iterbale that returns frames.

Iterates through the sequence of frame NumPy arrays returned by the frames iterable, and save each one as a PNG file.

The outfile parameter should be a base filepath. For example:

C:/temp/image

In this case the files will be created in the folder C:/temp, and their names will be based on the base name "image":

image00000000.png
image00000001.png
image00000002.png
etc

Once the images have been created, you can use an external program to convert them into an animated GIF or movie.

save_frame

save_frame(outfile, frame)
Parameter Type Description
outfile String The path and filename for the output PNG file. It should end in '.png'.
frame NumPy array An image array in memory.

Saves the frame as a PNG file.

duplicate_frame

duplicate_frame(frame, count)
Parameter Type Description
frame NumPy array An image array in memory.
count int Number of repeats.

Returns an iterable that consists of the frame, repeated count times.

This can be used for static frames, for example a title frame that is displayed unchanged for several seconds.

Copyright (c) Axlesoft Ltd 2020