image_artifact#

Bases: BlobArtifact “)

Attributes#

Name

Type

Description

<a href="image_artifact.mdx#griptape.artifacts.image_artifact.ImageArtifact.format" title="format = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute (griptape.artifacts.image_artifact.ImageArtifact.format)"><code class="language-python">format</code></a>

str

The format of the image data. Used when building the MIME type.<br/>

<a href="image_artifact.mdx#griptape.artifacts.image_artifact.ImageArtifact.width" title="width = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute (griptape.artifacts.image_artifact.ImageArtifact.width)"><code class="language-python">width</code></a>

int

The width of the image.<br/>

<a href="image_artifact.mdx#griptape.artifacts.image_artifact.ImageArtifact.height" title="height = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute (griptape.artifacts.image_artifact.ImageArtifact.height)"><code class="language-python">height</code></a>

int

The height of the image<br/>

Source Code in griptape/artifacts/image_artifact.py

@define
class ImageArtifact(BlobArtifact):
    """Stores image data.

    Attributes:
        format: The format of the image data. Used when building the MIME type.
        width: The width of the image.
        height: The height of the image
    """

    format: str = field(kw_only=True, metadata={"serializable": True})
    width: int = field(kw_only=True, metadata={"serializable": True})
    height: int = field(kw_only=True, metadata={"serializable": True})

    def __attrs_post_init__(self) -> None:
        # Generating the name string requires attributes set by child classes.
        # This waits until all attributes are available before generating a name.
        if self.name == self.id:
            self.name = self.make_name()

    @property
    def mime_type(self) -> str:
        return f"image/{self.format}"

    def to_bytes(self, **kwargs) -> bytes:
        return self.value

    def to_text(self) -> str:
        return f"Image, format: {self.format}, size: {len(self.value)} bytes"

    def make_name(self) -> str:
        entropy = "".join(random.choices(string.ascii_lowercase + string.digits, k=4))
        fmt_time = time.strftime("%y%m%d%H%M%S", time.localtime())

        return f"image_artifact_{fmt_time}_{entropy}.{self.format}"
  • format = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute

  • height = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute

  • mime_type property

  • width = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute

attrs_post_init()#

Source Code in griptape/artifacts/image_artifact.py

def __attrs_post_init__(self) -> None:
    # Generating the name string requires attributes set by child classes.
    # This waits until all attributes are available before generating a name.
    if self.name == self.id:
        self.name = self.make_name()

make_name()#

Source Code in griptape/artifacts/image_artifact.py

def make_name(self) -> str:
    entropy = "".join(random.choices(string.ascii_lowercase + string.digits, k=4))
    fmt_time = time.strftime("%y%m%d%H%M%S", time.localtime())

    return f"image_artifact_{fmt_time}_{entropy}.{self.format}"

to_bytes(**kwargs)#

Source Code in griptape/artifacts/image_artifact.py

def to_bytes(self, **kwargs) -> bytes:
    return self.value

to_text()#

Source Code in griptape/artifacts/image_artifact.py

def to_text(self) -> str:
    return f"Image, format: {self.format}, size: {len(self.value)} bytes"