"""Imágenes de verdad para la suite: la compresión no acepta bytes inventados."""

from io import BytesIO

from PIL import Image


def png_bytes(width: int = 64, height: int = 48, color=(30, 120, 200)) -> bytes:
    buffer = BytesIO()
    Image.new('RGB', (width, height), color).save(buffer, format='PNG')
    return buffer.getvalue()


def jpeg_bytes(
    width: int, height: int, exif_orientation: int | None = None, mode: str = 'RGB'
) -> bytes:
    """Un JPEG, opcionalmente con la orientación EXIF que graban los teléfonos."""
    image = Image.new('RGB', (width, height), (200, 80, 40)).convert(mode)
    buffer = BytesIO()
    if exif_orientation is None:
        image.save(buffer, format='JPEG')
    else:
        exif = Image.Exif()
        exif[0x0112] = exif_orientation
        image.save(buffer, format='JPEG', exif=exif.tobytes())
    return buffer.getvalue()
