Created
March 28, 2024 19:48
-
-
Save spillai/406093e83743508cb6b529a485674bbf to your computer and use it in GitHub Desktop.
Helper image-processing utility for vlm API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from base64 import b64encode | |
from io import BytesIO | |
from typing import Literal, Union | |
import requests | |
from PIL import Image | |
def pprint(data): | |
print(json.dumps(data, indent=2)) | |
def encode_image(image: Image.Image, format: Literal["PNG", "JPEG"] = "PNG") -> Union[str, bytes]: | |
"""Convert an image to a base64 string.""" | |
buffered = BytesIO() | |
image_format = image.format or format | |
image.save(buffered, format=image_format) | |
img_str = b64encode(buffered.getvalue()).decode() | |
return f"data:image/{image_format.lower()};base64,{img_str}" | |
def download_image(url: str) -> Image.Image: | |
"""Download an image from a URL.""" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", | |
} | |
bytes = BytesIO(requests.get(url, headers=headers).content) | |
bytes.seek(0) | |
return Image.open(bytes).convert("RGB") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment