mistral_common.image
download_image(url, timeout)
Download an image from a URL and return it as a PIL Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the image to download. |
required |
timeout
|
float | None
|
Seconds to wait for the server response. If None, uses the
|
required |
Returns:
| Type | Description |
|---|---|
Image
|
The downloaded image as a PIL Image object. |
Source code in src/mistral_common/image.py
maybe_load_image_from_str_or_bytes(x)
Load an image from a string or bytes.
If the input is already a PIL Image, return it as is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Image | str | bytes
|
The input to load the image from. Can be a PIL Image, a string, or bytes. If it's a string, it's assumed to be a base64 encoded string of bytes; if it's bytes, raw encoded image data (e.g., PNG). |
required |
Returns:
| Type | Description |
|---|---|
Image
|
The loaded image as a PIL Image object. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the input cannot be decoded into an image. |
Source code in src/mistral_common/image.py
serialize_image_to_byte_str(im, info)
Serialize an image to a base64 encoded string of bytes.
The output honors two context keys from info, when present:
max_image_b64_len truncates the base64 string for display, and
add_format_prefix prepends a data:...;base64, prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
im
|
Image
|
The image to serialize. Its format is used, defaulting to PNG when unset. |
required |
info
|
SerializationInfo
|
The pydantic serialization info carrying optional context. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The serialized image as a base64 encoded string of bytes. |