register_image_conversion()#

Register new image conversion functionality to be used by convert_image().

Examples#

 1import numpy as np
 2
 3# toy example that converts tuples to grayscale images
 4def precondition(obj):
 5    if not isinstance(obj, tuple) or len(obj) != 3:
 6        return False
 7    return all(isinstance(x, int) for x in obj)
 8
 9
10def convert_function(obj):
11    array = np.full((obj[1], obj[2]), obj[0], dtype=np.uint8)
12    return py5.NumpyImageArray(array, 'L')
13
14
15py5.register_image_conversion(precondition, convert_function)
16
17
18def setup():
19    img1 = py5.convert_image((20, 30, 40))
20    img2 = py5.convert_image((100, 20, 80))
21    py5.println(type(img1))
22    py5.println(type(img2))
23    py5.image(img1, 30, 50)
24    py5.image(img2, 10, 20)

Description#

Register new image conversion functionality to be used by convert_image(). This will allow users to extend py5’s capabilities and compatability within the Python ecosystem.

The precondition parameter must be function that accepts an object as a parameter and returns True if and only if the convert_function can successfully convert the object.

The convert_function parameter must be a function that accepts an object as a parameter and returns either a filename that can be read by load_image(), a py5.NumpyImageArray object, or a Py5Image object. View py5’s source code for detailed information about py5.NumpyImageArray objects.

Syntax#

register_image_conversion(precondition: Callable, convert_function: Callable) -> None

Parameters#

  • convert_function: Callable - function to convert object to relevant image data

  • precondition: Callable - predicate determining if an object can be converted

Updated on September 11, 2021 16:51:34pm UTC