generativepy Frames

By 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:

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.

See also

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

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest