Skip to content

Encoding

bytes_to_opencv_image(payload, array_type=np.uint8)

Decode a bytes object to an OpenCV image.

Parameters:

Name Type Description Default
payload bytes

The bytes object to decode.

required
array_type number

The type of the array.

uint8

Returns:

Type Description
ndarray

The OpenCV image.

Source code in inference_sdk/http/utils/encoding.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def bytes_to_opencv_image(
    payload: bytes, array_type: np.number = np.uint8
) -> np.ndarray:
    """Decode a bytes object to an OpenCV image.

    Args:
        payload: The bytes object to decode.
        array_type: The type of the array.

    Returns:
        The OpenCV image.
    """
    bytes_array = np.frombuffer(payload, dtype=array_type)
    decoding_result = cv2.imdecode(bytes_array, cv2.IMREAD_UNCHANGED)
    if decoding_result is None:
        raise EncodingError("Could not encode bytes to OpenCV image.")
    return decoding_result

bytes_to_pillow_image(payload)

Decode a bytes object to a PIL image.

Parameters:

Name Type Description Default
payload bytes

The bytes object to decode.

required

Returns:

Type Description
Image

The PIL image.

Source code in inference_sdk/http/utils/encoding.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def bytes_to_pillow_image(payload: bytes) -> Image.Image:
    """Decode a bytes object to a PIL image.

    Args:
        payload: The bytes object to decode.

    Returns:
        The PIL image.
    """
    buffer = BytesIO(payload)
    try:
        return Image.open(buffer)
    except UnidentifiedImageError as error:
        raise EncodingError("Could not encode bytes to PIL image.") from error

encode_base_64(payload)

Encode a bytes object to a base64 string.

Parameters:

Name Type Description Default
payload bytes

The bytes object to encode.

required

Returns:

Type Description
str

The base64 string.

Source code in inference_sdk/http/utils/encoding.py
42
43
44
45
46
47
48
49
50
51
def encode_base_64(payload: bytes) -> str:
    """Encode a bytes object to a base64 string.

    Args:
        payload: The bytes object to encode.

    Returns:
        The base64 string.
    """
    return base64.b64encode(payload).decode("utf-8")

numpy_array_to_base64_jpeg(image)

Encode a numpy array to a base64 JPEG string.

Parameters:

Name Type Description Default
image ndarray

The numpy array to encode.

required

Returns:

Type Description
Union[str]

The base64 JPEG string.

Source code in inference_sdk/http/utils/encoding.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def numpy_array_to_base64_jpeg(
    image: np.ndarray,
) -> Union[str]:
    """Encode a numpy array to a base64 JPEG string.

    Args:
        image: The numpy array to encode.

    Returns:
        The base64 JPEG string.
    """
    _, img_encoded = cv2.imencode(".jpg", image)
    image_bytes = np.array(img_encoded).tobytes()
    return encode_base_64(payload=image_bytes)

pillow_image_to_base64_jpeg(image)

Encode a PIL image to a base64 JPEG string.

Parameters:

Name Type Description Default
image Image

The PIL image to encode.

required

Returns:

Type Description
str

The base64 JPEG string.

Source code in inference_sdk/http/utils/encoding.py
28
29
30
31
32
33
34
35
36
37
38
39
def pillow_image_to_base64_jpeg(image: Image.Image) -> str:
    """Encode a PIL image to a base64 JPEG string.

    Args:
        image: The PIL image to encode.

    Returns:
        The base64 JPEG string.
    """
    with BytesIO() as buffer:
        image.save(buffer, format="JPEG")
        return encode_base_64(payload=buffer.getvalue())