Skip to content

Media

Media

Bases: ABC

Abstract base class representing a multimedia object. Provides multiple constructors and conversion methods.

ImageMedia

Bases: Media

Image media class using Pillow as internal representation.

This class provides a wrapper around PIL (Pillow) Image objects to handle image media with additional functionality for base64 encoding and metadata management.

Attributes:

Name Type Description
_image Image

Internal PIL Image object

_metadata Optional[dict]

Metadata associated with the image

Example

from PIL import Image pil_img = Image.open('image.jpg') image_media = ImageMedia(pil_img, {'author': 'John Doe'})

Convert to base64

base64_data = image_media.to_base64()

Save with metadata

image_media.save_to_file('output.png')

save_to_file(filepath)

Save the image to a file with metadata support.

Parameters:

Name Type Description Default
filepath str

Path where the image should be saved

required
Example

image_media.save_to_file('output.png')

to_base64(format='PNG')

Convert the image to a base64 encoded string.

Parameters:

Name Type Description Default
format str

Image format for encoding (default: 'PNG')

'PNG'

Returns:

Name Type Description
str str

Base64 encoded string representation of the image

Example

base64_str = image_media.to_base64('JPEG')