base64 Args: image_input: PIL.Image Returns: base64 Raises: ValueError:
(image_input: Union[str, Image.Image])
| 283 | |
| 284 | |
| 285 | def encode_image(image_input: Union[str, Image.Image]) -> str: |
| 286 | """ |
| 287 | base64 |
| 288 | |
| 289 | Args: |
| 290 | image_input: PIL.Image |
| 291 | |
| 292 | Returns: |
| 293 | base64 |
| 294 | |
| 295 | Raises: |
| 296 | ValueError: |
| 297 | """ |
| 298 | if isinstance(image_input, str): |
| 299 | # |
| 300 | with open(image_input, "rb") as image_file: |
| 301 | return base64.b64encode(image_file.read()).decode("utf-8") |
| 302 | elif isinstance(image_input, Image.Image): |
| 303 | # PIL.Image |
| 304 | buffer = io.BytesIO() |
| 305 | image_input.save(buffer, format='PNG') |
| 306 | return base64.b64encode(buffer.getvalue()).decode("utf-8") |
| 307 | else: |
| 308 | raise ValueError("image_input must be a file path string or PIL.Image object") |
| 309 | |
| 310 | |
| 311 | def tools_to_text_description(tools: List[Dict[str, Any]]) -> str: |
nothing calls this directly
no outgoing calls
no test coverage detected