Skip to content

image

mlte/value/types/image.py

A Value instance for image media.

Image

Bases: Value

Image implements the Value interface for image media.

Source code in mlte/value/types/image.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Image(Value):
    """
    Image implements the Value interface for image media.
    """

    def __init__(
        self,
        metadata: EvidenceMetadata,
        image: Union[str, Path, bytes],
    ):
        """
        Initialize an Image instance.
        :param metadata: The generating measurement's metadata
        :param image: The path to the image (str, Path) or raw image data (bytes)
        """
        if isinstance(image, str):
            image = Path(image)

        if isinstance(image, Path):
            with image.open("rb") as f:
                image = f.read()
        assert isinstance(image, bytes), "Broken invariant."

        super().__init__(self, metadata)

        # TODO(Kyle): Unsure if storing media inline is the
        # right way to go here (not scalable); reassess this.

        self.image: bytes = image
        """The data of the referenced image."""

    def to_model(self) -> ArtifactModel:
        """
        Convert an image value artifact to its corresponding model.
        :return: The artifact model
        """
        return ArtifactModel(
            header=self.build_artifact_header(),
            body=ValueModel(
                metadata=self.metadata,
                value_class=self.get_class_path(),
                value=ImageValueModel(
                    data=base64.encodebytes(self.image).decode("utf-8"),
                ),
            ),
        )

    @classmethod
    def from_model(cls, model: ArtifactModel) -> Image:
        """
        Convert an opaque value model to its corresponding artifact.
        :param model: The model representation
        :return: The real value
        """
        assert model.header.type == ArtifactType.VALUE, "Broken Precondition."
        body = typing.cast(ValueModel, model.body)

        assert body.value.value_type == ValueType.IMAGE, "Broken Precondition."
        return Image(
            metadata=body.metadata,
            image=base64.decodebytes(body.value.data.encode("utf-8")),
        )

    @classmethod
    def register_info(cls, info: str) -> Condition:
        """
        Register info about an image value.
        :param info: The information to record.
        :return: The Condition that can be used to validate a Value.
        """
        condition: Condition = Condition.build_condition(info=info)
        return condition

image = image instance-attribute

The data of the referenced image.

__init__(metadata, image)

Initialize an Image instance.

Parameters:

Name Type Description Default
metadata EvidenceMetadata

The generating measurement's metadata

required
image Union[str, Path, bytes]

The path to the image (str, Path) or raw image data (bytes)

required
Source code in mlte/value/types/image.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(
    self,
    metadata: EvidenceMetadata,
    image: Union[str, Path, bytes],
):
    """
    Initialize an Image instance.
    :param metadata: The generating measurement's metadata
    :param image: The path to the image (str, Path) or raw image data (bytes)
    """
    if isinstance(image, str):
        image = Path(image)

    if isinstance(image, Path):
        with image.open("rb") as f:
            image = f.read()
    assert isinstance(image, bytes), "Broken invariant."

    super().__init__(self, metadata)

    # TODO(Kyle): Unsure if storing media inline is the
    # right way to go here (not scalable); reassess this.

    self.image: bytes = image
    """The data of the referenced image."""

from_model(model) classmethod

Convert an opaque value model to its corresponding artifact.

Parameters:

Name Type Description Default
model ArtifactModel

The model representation

required

Returns:

Type Description
Image

The real value

Source code in mlte/value/types/image.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@classmethod
def from_model(cls, model: ArtifactModel) -> Image:
    """
    Convert an opaque value model to its corresponding artifact.
    :param model: The model representation
    :return: The real value
    """
    assert model.header.type == ArtifactType.VALUE, "Broken Precondition."
    body = typing.cast(ValueModel, model.body)

    assert body.value.value_type == ValueType.IMAGE, "Broken Precondition."
    return Image(
        metadata=body.metadata,
        image=base64.decodebytes(body.value.data.encode("utf-8")),
    )

register_info(info) classmethod

Register info about an image value.

Parameters:

Name Type Description Default
info str

The information to record.

required

Returns:

Type Description
Condition

The Condition that can be used to validate a Value.

Source code in mlte/value/types/image.py
85
86
87
88
89
90
91
92
93
@classmethod
def register_info(cls, info: str) -> Condition:
    """
    Register info about an image value.
    :param info: The information to record.
    :return: The Condition that can be used to validate a Value.
    """
    condition: Condition = Condition.build_condition(info=info)
    return condition

to_model()

Convert an image value artifact to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/value/types/image.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def to_model(self) -> ArtifactModel:
    """
    Convert an image value artifact to its corresponding model.
    :return: The artifact model
    """
    return ArtifactModel(
        header=self.build_artifact_header(),
        body=ValueModel(
            metadata=self.metadata,
            value_class=self.get_class_path(),
            value=ImageValueModel(
                data=base64.encodebytes(self.image).decode("utf-8"),
            ),
        ),
    )