Convert a PIL.Image to a base-64 data URI suitable for the OpenAI/vLLM 'image_url' block. fmt = 'PNG' (lossless) or 'JPEG' (smaller, 0-100 quality).
(img: Image.Image, fmt: str = "PNG")
| 49 | from transformers import AltCLIPProcessor, AltCLIPModel |
| 50 | |
| 51 | def pil_to_data_uri(img: Image.Image, fmt: str = "PNG") -> str: |
| 52 | """ |
| 53 | Convert a PIL.Image to a base-64 data URI suitable for |
| 54 | the OpenAI/vLLM 'image_url' block. |
| 55 | fmt = 'PNG' (lossless) or 'JPEG' (smaller, 0-100 quality). |
| 56 | """ |
| 57 | buf = io.BytesIO() |
| 58 | if fmt.upper() == "JPEG": |
| 59 | img.save(buf, format="JPEG", quality=90) |
| 60 | mime = "image/jpeg" |
| 61 | else: |
| 62 | img.save(buf, format="PNG") |
| 63 | mime = "image/png" |
| 64 | b64 = base64.b64encode(buf.getvalue()).decode() |
| 65 | return f"data:{mime};base64,{b64}" |
| 66 | |
| 67 | def md_to_blocks( |
| 68 | md: str, |
no test coverage detected