generativepy.utils module

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

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