generativepy.utils module


Martin McBride, 2020-11-25
Tags gif
Categories generativepy generative art

The utils module contains several utility functions that are generally useful within generativepy.

correct_pycairo_byte_order

Mainly for internal use with Pycairo data.

generativepy.utils.correct_pycairo_byte_order(array, channels)
Parameter Type Description
array array NumPy array of image data
channels int Number of channels in the data

Pycairo stores image data using a single 32-bit word per pixel (storing 8 bits each of R, G, B and optional Alpha).

When this is converted to a NumPy array, each 32-bit word is copie dinto memory, then NumPy treats it as 4 bytes. The order of the 4 bytes depends on the endian-ness of the processor. If the processor is little-endian, the ordering will be incorrect, leading to incorrect colours.

The function checks the processor endian-ness, and reorders the data if needed. It returns the adjusted array.

temp_file

Creates a filename in the system temp files area.

generativepy.utils.temp_file(*names)
Parameter Type Description
names string(s) One or more path parts.

Takes a filename or partial path string, and return a full pathname that will create a file in the temp area.

For a file:

temp_file('myimage.png')

Will return a path (on a linux system) of:

'/tmp/myimage.png'

and on a Windows system:

'c:\temp\myimage.png'

For a subfolder and file:

temp_file('myfolder', 'myimage.png')

Will return a path (on a linux system) of:

'/tmp/myfolder/myimage.png'

and on a Windows system:

'c:\temp\myfolder\myimage.png'

The folder used as system folder may be different depending on how your system is configured. This function only return the path string, it will not create the folders or files.

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 chain circle classes close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data types design pattern device space dictionary drawing duck typing efficiency ellipse 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 linear gradient 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 pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation scaling sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle tuple turtle unpacking user space vectorisation webserver website while loop zip