Skip to content

modosaic.services.image

modosaic.services.image

ImageService

Image conversion helpers shared by generators and validators.

bytes_to_pil staticmethod

bytes_to_pil(image_bytes)

Decode encoded image bytes into an RGB PIL image.

Parameters:

Name Type Description Default
image_bytes bytes

Encoded image bytes.

required

Returns:

Type Description
Image

RGB PIL image.

Source code in modosaic/services/image.py
13
14
15
16
17
18
19
20
21
22
23
@staticmethod
def bytes_to_pil(image_bytes: bytes) -> "Image.Image":
    """Decode encoded image bytes into an RGB PIL image.

    Args:
        image_bytes: Encoded image bytes.

    Returns:
        RGB PIL image.
    """
    return Image.open(io.BytesIO(image_bytes)).convert("RGB")

save_bytes_to_tmp_file staticmethod

save_bytes_to_tmp_file(image_bytes, suffix='.jpg')

Save encoded image bytes to a temporary image file.

Callers own cleanup for the returned path. Prefer temporary_image_file when the file is only needed within one scoped operation.

Parameters:

Name Type Description Default
image_bytes bytes

Encoded image bytes.

required
suffix str

Filename suffix for the temporary file.

'.jpg'

Returns:

Type Description
Path

Path to the created temporary file.

Source code in modosaic/services/image.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@staticmethod
def save_bytes_to_tmp_file(image_bytes: bytes, suffix: str = ".jpg") -> Path:
    """Save encoded image bytes to a temporary image file.

    Callers own cleanup for the returned path. Prefer
    `temporary_image_file` when the file is only needed within one scoped
    operation.

    Args:
        image_bytes: Encoded image bytes.
        suffix: Filename suffix for the temporary file.

    Returns:
        Path to the created temporary file.
    """
    with io.BytesIO(image_bytes) as img_io:
        with Image.open(img_io) as img:
            tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
            path = Path(tmp.name)
            img.save(path)
            return path

temporary_image_file staticmethod

temporary_image_file(image_bytes, suffix='.jpg')

Yield a temporary image file path and remove it when the scope exits.

Parameters:

Name Type Description Default
image_bytes bytes

Encoded image bytes.

required
suffix str

Filename suffix for the temporary file.

'.jpg'

Yields:

Type Description
Path

Path to the temporary image file.

Source code in modosaic/services/image.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@staticmethod
@contextmanager
def temporary_image_file(image_bytes: bytes, suffix: str = ".jpg") -> Iterator[Path]:
    """Yield a temporary image file path and remove it when the scope exits.

    Args:
        image_bytes: Encoded image bytes.
        suffix: Filename suffix for the temporary file.

    Yields:
        Path to the temporary image file.
    """
    path = ImageService.save_bytes_to_tmp_file(image_bytes, suffix=suffix)
    try:
        yield path
    finally:
        try:
            path.unlink()
        except FileNotFoundError:
            pass