generativepy has moved to the pythoninformer.com website, and this page might not be up to date. Please visit the new location.
A frame is a bitmap image stored im 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:
Frames are stored as NumPy arrays with a dtype
of np.uint8
, that is each element is an unsigned byte (valoe 0 to 255).
Frame arrays have a rank of 3:
[height, width, 1]
, where height
is the height of the image in pixels, width
is the width of the image in pixels.[height, width, 3]
.[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)
Here, 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 memeory at the same time.
This works because make_image_frames
is a Python generator.
Copyright (c) Axlesoft Ltd 2021