-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
63 lines (51 loc) · 1.71 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import base64
import io
from PIL import Image
import numpy as np
def encode_image(image: np.ndarray) -> str:
"""
Encode a numpy array image to a base64 string.
Args:
image (np.ndarray): The image as a numpy array.
Returns:
str: The base64 encoded string of the image.
"""
if image.dtype != np.uint8:
if image.max() <= 1.0:
image = (image * 255).astype(np.uint8)
else:
image = image.astype(np.uint8)
if len(image.shape) == 2:
image = np.stack((image,) * 3, axis=-1)
elif image.shape[2] == 4:
image = image[:, :, :3]
img = Image.fromarray(image)
buffered = io.BytesIO()
img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
def decode_image(encoded_string: str) -> np.ndarray:
"""
Decode a base64 string to a numpy array image.
Args:
encoded_string (str): The base64 encoded string of the image,
or a full data URI.
Returns:
np.ndarray: The decoded image as a numpy array.
"""
if encoded_string.startswith('data:image'):
encoded_string = encoded_string.split(',', 1)[1]
try:
decoded = base64.b64decode(encoded_string)
img = Image.open(io.BytesIO(decoded))
return np.array(img)
except Exception as e:
raise ValueError(f"Error decoding image: {str(e)}")
def handle_error(e: Exception) -> dict:
"""
Handle exceptions and return a formatted error message.
Args:
e (Exception): The exception that was raised.
Returns:
dict: A dictionary containing the error message.
"""
return {"error": str(e)}