← All ivy-nodes
IVYXSTUDIO · IVY NODE
I
Image Processor
ivy.node.image-processor · v0.0.0
ivyx✓
Loads and processes images from BytesIO buffers using the Pillow (PIL) library. This node validates image data, loads it into a Pillow Image object, and provides an image processor instance for further operations. Supports common image formats (JPEG, PNG, etc.). The output image_processor is used with image-show for display or can be used for image manipulation tasks. The buffer can come from minio-file-reader or other file sources.
#Pillow#Image Processing#BytesIO#Image Loading#Image Manipulation#Image Object#PIL#Data Stream
Inputs
| Field | Type | Description |
|---|---|---|
| bufferrequired | object | A BytesIO object containing the raw image data. This buffer should represent a valid image format (e.g., JPEG, PNG). (Node reference) |
| features | object | Features |
| labels | object | Labels |
Outputs
| Field | Type | Description |
|---|---|---|
| image_processorrequired | object | Custom type: PillowImageProcessor |
| features | object | Features |
| labels | object | Labels |
Source
python
# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
buffer = inp["buffer"]
# Compute
from PIL import Image, UnidentifiedImageError
import io
class PillowImageProcessor:
"""
A class to handle image operations using the Pillow library.
"""
def __init__(self, buffer: io.BytesIO):
"""
Initialize the PillowImageProcessor with a buffer containing image data.
:param buffer: A BytesIO object containing the raw image data.
This buffer should represent a valid image format (e.g., JPEG, PNG).
:raises ValueError: If the buffer is empty.
"""
if buffer.getbuffer().nbytes == 0:
raise ValueError("Buffer is empty. Cannot load an image.")
self.buffer = buffer
self.image = None # Placeholder for the loaded Pillow Image object.
self.load_image() # Automatically load the image when an instance is created.
def load_image(self):
"""
Load an image from the provided buffer into a Pillow Image object.
This method resets the buffer pointer to the start of the stream
and uses Pillow to load the image. The loaded image is stored in the
`self.image` attribute.
:raises UnidentifiedImageError: If the buffer does not contain valid image data.
"""
try:
self.buffer.seek(0) # Reset the buffer pointer to the start.
self.image = Image.open(self.buffer)
except UnidentifiedImageError:
raise ValueError("Buffer does not contain a valid image.")
def get_image(self) -> Image.Image:
"""
Get the loaded Pillow Image object.
:return: The loaded Pillow Image object.
:raises RuntimeError: If the image has not been loaded yet.
"""
if self.image is None:
raise RuntimeError("Image not loaded. Call load_image() first.")
return self.image
image_processor = PillowImageProcessor(buffer=buffer)
# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["image_processor"] = image_processorTests
Requires: python:3.11
- basic-image-loading
Load valid image buffer (should succeed).
imageprocessingloadingbasic - error-empty-buffer
Empty buffer should raise an error.
error-handlingvalidation