register_image_conversion()

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np

# toy example that converts tuples to grayscale images
def precondition(obj):
    if not isinstance(obj, tuple) or len(obj) != 3:
        return False
    return all(isinstance(x, int) for x in obj)


def convert_function(obj):
    array = np.full((obj[1], obj[2]), obj[0], dtype=np.uint8)
    return py5.NumpyImageArray(array, 'L')


py5.register_image_conversion(precondition, convert_function)


def setup():
    img1 = py5.convert_image((20, 30, 40))
    img2 = py5.convert_image((100, 20, 80))
    py5.println(type(img1))
    py5.println(type(img2))
    py5.image(img1, 30, 50)
    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