Martin McBride, 2020-11-26
Tags frame numpy
Categories generativepy generative art
A frame is a bitmap image stored in memory as a NumPy array.
Frames hold Grey, RGB, or RGBA data. The data is stored as one byte per colour per pixel, where a byte value of 0 is black and 255 is full intensity:
- Grey data contains one channel (1 byte per pixel).
- RGB data contains three channels (3 bytes per pixel). The data is stored in the order R, G, B for each pixel.
- RGBA data contains four channels (4 bytes per pixel). The data is stored in the order R, G, B, A for each pixel.
NumPy format of a frame
Frames are stored as NumPy arrays with a
np.uint8, that is each element is an unsigned byte (value 0 to 255).
Frame arrays have a rank of 3:
- Grey data is stored in an array with shape
[height, width, 1], where
heightis the height of the image in pixels,
widthis the width of the image in pixels.
- RGB data is stored in an array with shape
[height, width, 3].
- RGBA data is stored in an array with shape
[height, width, 4].
Notice that the array is stored in rows then columns, as is standard for NumPy arrays. So to find the value of the pixel at position
(x, y) you would need to look at element
[y, x] - the coordinates are swapped.
Pixel position (0, 0) represents the top left of the image.
For example, if
im is an RGB frame, we can find the value of the pixel at
(100, 200) like this:
val = im[200, 100]
This might give a value such as
[255, 128, 0], which represents the RGB value of that pixel.
Some functions (such as
make_image_frames in the drawing module) create a lazy sequence of frames.
The lazy sequence is an interator that returns frames one by one, typically the frames in a video or animation. Here is an example:
frames = make_image_frames(draw_func, 600, 400, 10) for frame in frames: process(frame)
make_image_frames uses the
draw_func function (some function you have defined elsewhere) to create 10 frames, each 600 by 400 pixels.
However, the call to
make_image_frames doesn't actually create any frames at all, it just returns a lazy iterator.
In the for loop, we process each frame individually (again, the
process function can be whatever you like, it doesn't matter here).
On each pass through the loop, the iterator will call
draw_func to create the next frame to be processed. This means that if your movie contains thosuands of frames, you don't need to create then all in memory at the same time.
This works because
make_image_frames is a Python generator.