imgutils.sd.nai

This module provides functionality for handling metadata in images, specifically for the Novel AI (NAI) format. It includes classes and functions for extracting, creating, and saving NAI metadata in images.

The module offers the following main features:

  1. Extraction of NAI metadata from images

  2. Creation of NAI metadata objects

  3. Adding NAI metadata to images

  4. Saving images with NAI metadata

This module is particularly useful for working with AI-generated images and their associated metadata.

NAIMetaData

class imgutils.sd.nai.NAIMetaData(software: str, source: str, parameters: dict, title: str | None = None, generation_time: float | None = None, description: str | None = None)[source]

A dataclass representing Novel AI (NAI) metadata.

This class encapsulates various metadata fields associated with NAI-generated images.

Parameters:
  • software (str) – The software used to generate the image.

  • source (str) – The source of the image.

  • parameters (dict) – A dictionary containing generation parameters.

  • title (Optional[str]) – The title of the image (optional).

  • generation_time (Optional[float]) – The time taken to generate the image (optional).

  • description (Optional[str]) – A description of the image (optional).

__init__(software: str, source: str, parameters: dict, title: str | None = None, generation_time: float | None = None, description: str | None = None) None
property pnginfo: PngInfo

Convert the NAIMetadata to a PngInfo object.

This property creates a PngInfo object with the metadata information, which can be used when saving PNG images.

Returns:

A PngInfo object containing the metadata.

Return type:

PngInfo

NAIMetadata

class imgutils.sd.nai.NAIMetadata

Alias of NAIMetaData

get_naimeta_from_image

imgutils.sd.nai.get_naimeta_from_image(image: str | PathLike | bytes | bytearray | BinaryIO | Image) NAIMetaData | None[source]

Extract and create a NAIMetadata object from an image.

This function attempts to extract NAI metadata from the given image and create a NAIMetadata object. If the required metadata fields are not present, it returns None.

Parameters:

image (ImageTyping) – The input image.

Returns:

A NAIMetadata object if successful, None otherwise.

Return type:

Optional[NAIMetaData]

add_naimeta_to_image

imgutils.sd.nai.add_naimeta_to_image(image: str | PathLike | bytes | bytearray | BinaryIO | Image, metadata: NAIMetaData) Image[source]

Add NAI metadata to an image using LSB (Least Significant Bit) encoding.

Parameters:
  • image (ImageTyping) – The input image.

  • metadata (NAIMetaData) – The NAIMetadata object to add to the image.

Returns:

The image with added metadata.

Return type:

Image.Image

save_image_with_naimeta

imgutils.sd.nai.save_image_with_naimeta(image: str | PathLike | bytes | bytearray | BinaryIO | Image, dst_file: str | PathLike, metadata: NAIMetaData, add_lsb_meta: str | bool = 'auto', save_metainfo: str | bool = 'auto', **kwargs) Image[source]

Save an image with NAI metadata.

This function saves the given image with the provided NAI metadata. It can add LSB metadata and save metainfo based on the image format and user preferences.

Parameters:
  • image (ImageTyping) – The input image.

  • dst_file (Union[str, os.PathLike]) – The destination file path.

  • metadata (NAIMetaData) – The NAIMetadata object to include in the image.

  • add_lsb_meta (Union[str, bool]) – Whether to add LSB metadata. Can be ‘auto’, True, or False.

  • save_metainfo (Union[str, bool]) – Whether to save metainfo. Can be ‘auto’, True, or False.

  • kwargs – Additional keyword arguments for image saving.

Returns:

The saved image.

Return type:

Image.Image

Raises:
  • ValueError – If LSB metadata cannot be saved to the specified image format.

  • SystemError – If the image format is not supported for saving metainfo.