Encode image to base64 string. Args: image_path: Path to image file. Returns: Base64 encoded image string. Raises: ValueError: If the file cannot be read or doesn't exist.
(self, image_path: str)
| 536 | return output |
| 537 | |
| 538 | def _encode_image(self, image_path: str) -> str: |
| 539 | """Encode image to base64 string. |
| 540 | |
| 541 | Args: |
| 542 | image_path: Path to image file. |
| 543 | |
| 544 | Returns: |
| 545 | Base64 encoded image string. |
| 546 | |
| 547 | Raises: |
| 548 | ValueError: If the file cannot be read or doesn't exist. |
| 549 | """ |
| 550 | try: |
| 551 | with open(image_path, "rb") as image_file: |
| 552 | return base64.b64encode(image_file.read()).decode("utf-8") |
| 553 | except FileNotFoundError: |
| 554 | raise ValueError(f"Image file not found: {image_path}") |
| 555 | except PermissionError: |
| 556 | raise ValueError(f"Permission denied when reading image file: {image_path}") |
| 557 | except Exception as e: |
| 558 | raise ValueError(f"Error encoding image {image_path}: {str(e)}") |
| 559 | |
| 560 | def _prepare_image_content( |
| 561 | self, image_source: Union[str, Dict[str, Any]], detail: str = "auto" |
no outgoing calls
no test coverage detected