Skip to content

Commit f9d7b91

Browse files
committed
convenient array to base64 function
1 parent 596fcd2 commit f9d7b91

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/agentlab/llm/llm_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ def image_to_jpg_base64_url(image: np.ndarray | Image.Image):
381381
return f"data:image/jpeg;base64,{image_base64}"
382382

383383

384+
def image_to_png_base64_url(image: np.ndarray | Image.Image):
385+
if isinstance(image, np.ndarray):
386+
image = Image.fromarray(image)
387+
if image.mode in ("RGBA", "LA"):
388+
image = image.convert("RGB")
389+
buffered = io.BytesIO()
390+
image.save(buffered, "PNG")
391+
image_base64 = base64.b64encode(buffered.getvalue()).decode()
392+
return f"data:image/png;base64,{image_base64}"
393+
394+
384395
class BaseMessage(dict):
385396
def __init__(self, role: str, content: Union[str, list[dict]], **kwargs):
386397
allowed_attrs = {"log_probs"}

0 commit comments

Comments
 (0)