image_loader
Adapted from the Griptape AI Framework documentation.
Bases:
BaseFileLoader[ImageArtifact]
Attributes
Name | Type | Description |
---|---|---|
format | Optional[str] | If provided, attempts to ensure image artifacts are in this format when loaded. For example, when set to 'PNG', loading image.jpg will return an ImageArtifact containing the image bytes in PNG format. |
Source Code in griptape/loaders/image_loader.py
@define class ImageLoader(BaseFileLoader[ImageArtifact]): """Loads images into image artifacts. Attributes: format: If provided, attempts to ensure image artifacts are in this format when loaded. For example, when set to 'PNG', loading image.jpg will return an ImageArtifact containing the image bytes in PNG format. """ format: Optional[str] = field(default=None, kw_only=True) def try_parse(self, data: bytes) -> ImageArtifact: pil_image = import_optional_dependency("PIL.Image") image = pil_image.open(BytesIO(data)) # Normalize format only if requested. if self.format is not None: byte_stream = BytesIO() image.save(byte_stream, format=self.format) image = pil_image.open(byte_stream) data = byte_stream.getvalue() return ImageArtifact(data, format=image.format.lower(), width=image.width, height=image.height)
format = field(default=None, kw_only=True)
class-attribute instance-attribute
try_parse(data)
Source Code in griptape/loaders/image_loader.py
def try_parse(self, data: bytes) -> ImageArtifact: pil_image = import_optional_dependency("PIL.Image") image = pil_image.open(BytesIO(data)) # Normalize format only if requested. if self.format is not None: byte_stream = BytesIO() image.save(byte_stream, format=self.format) image = pil_image.open(byte_stream) data = byte_stream.getvalue() return ImageArtifact(data, format=image.format.lower(), width=image.width, height=image.height)
- On this page
- Attributes
- try_parse(data)
Could this page be better? Report a problem or suggest an addition!