Module generativepy.nparray

Functions

def apply_npcolormap(out, counts, npcolormap)

Apply a color map to an array of counts, filling an existing output array

Args

out
numpy array - the output array, height x width x channels (channels is 3 or 4).
counts
numpy array - the counts array, height x width, count range 0 to max_count.
npcolormap
numpy array - a numpy color map, must have at least maxcount+1 elements.
def load_nparray(infile)

Load a numpy array from file

Args

infile
str - file path including extension.

Returns

A numpy array. No checking is done on the array.

def make_nparray(outfile, paint, pixel_width, pixel_height, channels=3)

Create a PNG file using numpy.

Args

outfile
str - Name of output file.
paint
function - the paint function.
pixel_width
int - width in pixels.
pixel_height
int - height in pixels.
channels
int - 1 for greyscale, 3 for rgb, 4 for rgba.
def make_nparray_data(paint, pixel_width, pixel_height, channels=3, dtype=numpy.uint64)

Create a data array using numpy.

This is similar to make_nparray_frame() except that it produce a general purpose numpy array rather than a frame buffer. The difference is that the data from this function isn't constrained to the range 0-255.

The data can be outside that normal range, and it can also be a different type (eg signed integer or floating point). This allows the method to be used for storing intermediate data such as per pixel counts.

Args

paint
function - the paint function.
pixel_width
int - width in pixels.
pixel_height
int - height in pixels.
channels
int - 1 for greyscale, 3 for rgb, 4 for rgba.
dtype
numpy data type - the type of the array.

Returns

A numpy array

def make_nparray_frame(paint, pixel_width, pixel_height, channels=3, out=None)

Create a frame using numpy

Args

paint
function - the paint function.
pixel_width
int - width in pixels.
pixel_height
int - height in pixels.
channels
int - 1 for greyscale, 3 for rgb, 4 for rgba.
out
numpy array - optional array to hold result. Must be correct width, height and channels, but can be any int type.

Returns

A numpy array frame buffer

def make_nparray_frames(paint, pixel_width, pixel_height, count, channels=3)

Create a frame sequence using numpy.

This function returns a lazy iterator that can be used to access the sequence. Images will be created as they are requested.

Args

paint
function - the paint function.
pixel_width
int - width in pixels.
pixel_height
int - height in pixels.
count
int - number of frames ot create.
channels
int - 1 for greyscale, 3 for rgb, 4 for rgba.

Yields

Lazy iterator of frames.

def make_nparrays(outfile, paint, pixel_width, pixel_height, count, channels=3)

Create a set of PNG files using numpy.

Args

outfile
str - Name of output file.
paint
function - the paint function.
pixel_width
int - width in pixels.
pixel_height
int - height in pixels.
count
int - number of frames to create.
channels
int - 1 for greyscale, 3 for rgb, 4 for rgba.
def make_npcolormap(length, colors, bands=None, channels=3)

Create a colormap, a list of varying colors, as a numpy array.

Args

length
  • int, required size of list
colors
  • tuple of Color objects - the list of colours, must be at least 2 long.
bands
tuple of numbers - Relative size of each band. bands[i] gives the size of the band between color[i] and color[i+1]. len(bands) must be exactly 1 less than len(colors). If bands is None, equal bands will be used.
channels
int 3 for RGB, 4 for RGBA

Returns

An array of shape (length, channels) containing the RGB(A) values for each entry, as integers from 0-255

def overlay_nparrays(array1, array2)

Overlay array2 on top of array1. Any pixels in array2 that are fully white are treated as transparent.

Both frames nust be same size, and both must be 4 channel RGBA data.

Args

array1
numpy frame - first (lower) frame.
array2
numpy frame - second (upper) frame.

Returns

A numpy array frame buffer

def save_nparray(outfile, array)

Save a general array to file in mumpy format. The saved file is not an image file.

The file created can be read back in using load_nparray().

Args

outfile
str - numpy file path including extension.
array
numpy array - data to be saved.
def save_nparray_image(outfile, array)

Save an array to an image file.

Args

outfile
str - image file path including extension.
array
numpy array - data to be saved.