Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

724 B
Raw Blame
from __future__ import annotations

import base64
import io

from PIL import Image
from PIL.Image import Image as PILImageType


def encode_image_base64(image: PILImageType, format: str = "PNG") -> str:
    buffer = io.BytesIO()
    image.save(buffer, format=format)
    return base64.b64encode(buffer.getvalue()).decode("ascii")


def decode_image_base64(encoded: str) -> PILImageType:
    data = base64.b64decode(encoded)
    image = Image.open(io.BytesIO(data))
    # Keep transparency (e.g. a "transparent" background request) instead of flattening it away.
    if image.mode in ("RGBA", "LA") or (image.mode == "P" and "transparency" in image.info):
        return image.convert("RGBA")
    return image.convert("RGB")