import io | |
from PIL import Image | |
from urllib.parse import urlparse | |
import requests | |
def is_online_file(url: str) -> bool: | |
return urlparse(url).scheme in ["http", "https"] | |
def steam_online_file(url: str) -> bytes: | |
return io.BytesIO(requests.get(url, stream=True).content) | |
def get_RGB_image(image_or_path: str | Image.Image) -> bytes: | |
if isinstance(image_or_path, str): | |
if is_online_file(image_or_path): # Online | |
content = steam_online_file(image_or_path) | |
image_or_path = Image.open(content) | |
else: # Local | |
image_or_path = Image.open(image_or_path) | |
return image_or_path.convert("RGB") |