Skip to content

pgsql_image_loader

Documentation for PgsqlImageLoader

Overview

PgsqlImageLoader is a specialized loader designed to fetch images from a PostgreSQL database and convert binary image data into PIL Image objects. This class extends PgsqlDataLoader, reusing connection and query generation logic while adding dedicated handling for image data.

Inheritance

PgsqlImageLoader inherits from PgsqlDataLoader, which provides robust SQL query capabilities and connection management. By building on this foundation, PgsqlImageLoader facilitates image-specific conversions, simplifying the process of working with images stored as binary data.

Motivation

Images stored as binary data in databases require conversion to usable formats. PgsqlImageLoader streamlines this process by transforming binary data into PIL Image objects, which benefits image processing and machine learning (ML) pipelines.

Initialization

To use PgsqlImageLoader, initialize it with a PostgreSQL connection string, a query generator, and optionally, the name of the image column to customize its functionality.

Example

loader = PgsqlImageLoader("postgresql://user:pass@host/db", QueryGenerator, image_column="img_data")

Method: _get_item

Functionality

The _get_item method converts binary image data from a dictionary row into a PIL Image object. It expects the image data to be present under the key defined by the image_column attribute. If the image data is missing, the method logs an error and raises a ValueError.

Parameters

  • data: A dictionary representing a row from the database, which should include image data under the key specified by image_column.

Usage

  • Purpose: To convert raw binary image data into a PIL Image object for further processing.

Example

row = {'image_data': binary_data}
image = loader._get_item(row)