generativepy Frames


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 dtype of 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 height is the height of the image in pixels, width is 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.

Frame sequences

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 memory at the same time.

This works because make_image_frames is a Python generator.

If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.

Prev

Popular tags

2d arrays abstract data type alignment and animation arc array arrays behavioural pattern bezier curve built-in function callable object circle classes close closure cmyk colour comparison operator comprehension context context manager conversion creational pattern data types design pattern device space dictionary drawing duck typing efficiency else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif gradient greyscale higher order function hsl html image image processing imagesurface immutable object index inner function input installing iter iterable iterator itertools l system lambda function len line linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path polygon positional parameter print pure function pycairo radial gradient range recipes rectangle recursion reduce rgb rotation scaling sector segment sequence singleton slice slicing sound spirograph sprite square str strategy stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency tuple turtle unpacking user space vectorisation webserver website while loop zip