Spaces:
Sleeping
Sleeping
| import base64 | |
| import io | |
| import requests | |
| from PIL import Image | |
| def encode_image_base64(pil_image: Image.Image) -> str: | |
| """Encode a PIL Image to a base64 data URI for DashScope API input.""" | |
| buffer = io.BytesIO() | |
| fmt = pil_image.format or "JPEG" | |
| if fmt not in ("JPEG", "PNG"): | |
| fmt = "JPEG" | |
| pil_image.save(buffer, format=fmt) | |
| b64 = base64.b64encode(buffer.getvalue()).decode("utf-8") | |
| mime = "image/jpeg" if fmt == "JPEG" else "image/png" | |
| return f"data:{mime};base64,{b64}" | |
| def download_image(url: str) -> Image.Image: | |
| """Download image from a URL and return as PIL Image.""" | |
| response = requests.get(url, timeout=30) | |
| response.raise_for_status() | |
| return Image.open(io.BytesIO(response.content)) | |