Loading docs/source/api_doc/metadata/geninfo.rst 0 → 100644 +50 −0 Original line number Diff line number Diff line imgutils.metadata.geninfo ========================================== .. currentmodule:: imgutils.metadata.geninfo .. automodule:: imgutils.metadata.geninfo read_geninfo_parameters ---------------------------------------------------------- .. autofunction:: read_geninfo_parameters read_geninfo_exif ---------------------------------------------------------- .. autofunction:: read_geninfo_exif read_geninfo_gif ---------------------------------------------------------- .. autofunction:: read_geninfo_gif write_geninfo_parameters ---------------------------------------------------------- .. autofunction:: write_geninfo_parameters write_geninfo_exif ---------------------------------------------------------- .. autofunction:: write_geninfo_exif write_geninfo_gif ---------------------------------------------------------- .. autofunction:: write_geninfo_gif docs/source/api_doc/metadata/index.rst +1 −0 Original line number Diff line number Diff line Loading @@ -9,5 +9,6 @@ imgutils.metadata .. toctree:: :maxdepth: 3 geninfo lsb imgutils/metadata/__init__.py +11 −0 Original line number Diff line number Diff line """ This module provides functionality for reading and writing generation information (geninfo) and metadata using various methods. It includes functions for handling generation information in different formats (parameters, EXIF, GIF) and working with LSB (Least Significant Bit) metadata. The module is designed to be used in image processing and generation tasks, particularly in the context of AI-generated images. """ from .geninfo import read_geninfo_parameters, read_geninfo_exif, read_geninfo_gif, \ write_geninfo_parameters, write_geninfo_exif, write_geninfo_gif from .lsb import read_lsb_raw_bytes, read_lsb_metadata, write_lsb_raw_bytes, write_lsb_metadata, LSBReadError imgutils/sd/__init__.py +16 −1 Original line number Diff line number Diff line """ Overview: Utilities for dealing with data from `AUTOMATIC1111/stable-diffusion-webui <https://github.com/AUTOMATIC1111/stable-diffusion-webui>`_. Utilities for dealing with data from AUTOMATIC1111/stable-diffusion-webui and NovelAI generated images. This module provides a collection of utilities for handling metadata and image processing related to Stable Diffusion WebUI (SDWUI) and NovelAI generated images. It includes functions for parsing and extracting metadata, reading and writing model metadata, and manipulating NovelAI image metadata. Submodules: - metadata: Functions for parsing and extracting Stable Diffusion metadata. - model: Utilities for reading and writing metadata from/to model files. - nai: Functions for handling NovelAI image metadata. For detailed usage of each function and class, please refer to their individual docstrings. """ from .metadata import parse_sdmeta_from_text, get_sdmeta_from_image, SDMetaData from .model import read_metadata, save_with_metadata from .nai import get_naimeta_from_image, NAIMetadata, add_naimeta_to_image, save_image_with_naimeta imgutils/sd/model.py +58 −16 Original line number Diff line number Diff line """ Overview: A utility for reading and writing metadata from/to model files in the A41 WebUI format. A utility module for reading and writing metadata from/to model files in the A41 WebUI format. This module provides functions to read metadata from model files and save model files with custom metadata. It supports operations on SafeTensors files, which are a safe and efficient way to store tensors. .. note:: ``torch`` and ``safetensors`` are required by this model. Please install them with the following command before start using this module. ``torch`` and ``safetensors`` are required by this module. Please install them with the following command before using this module: .. code:: shell pip install dghs-imgutils[model] Functions: - read_metadata: Reads metadata from a model file. - save_with_metadata: Saves a model file with custom metadata. - _check_env: Internal function to check if required dependencies are installed. Dependencies: - torch - safetensors """ from typing import Dict Loading @@ -26,8 +40,13 @@ except (ImportError, ModuleNotFoundError): # pragma: no cover def _check_env(): """ Checks if the required dependencies (Safetensors and Torch) are installed. Raises EnvironmentError if they are not installed. Check if the required dependencies (Safetensors and Torch) are installed. This function verifies that both Safetensors and Torch are available in the current environment. If either is missing, it raises an EnvironmentError with instructions on how to install the missing dependency. :raises EnvironmentError: If Safetensors or Torch is not installed. """ if not safetensors: raise EnvironmentError( Loading @@ -39,12 +58,22 @@ def _check_env(): def read_metadata(model_file: str) -> Dict[str, str]: """ Reads metadata from a model file and returns it as a dictionary. Read metadata from a model file and return it as a dictionary. This function opens the specified model file using SafeTensors and extracts its metadata. The metadata is returned as a dictionary where both keys and values are strings. :param model_file: The path to the model file. :param model_file: The path to the model file to read metadata from. :type model_file: str :return: The metadata extracted from the model file. :return: A dictionary containing the metadata of the model file. :rtype: Dict[str, str] :raises EnvironmentError: If required dependencies are not installed. Usage: >>> metadata = read_metadata("path/to/model.safetensors") >>> print(metadata) {'key1': 'value1', 'key2': 'value2', ...} """ _check_env() with safetensors.safe_open(model_file, 'pt') as f: Loading @@ -53,16 +82,29 @@ def read_metadata(model_file: str) -> Dict[str, str]: def save_with_metadata(src_model_file: str, dst_model_file: str, metadata: Dict[str, str], clear: bool = False): """ Saves a model file with metadata. Optionally, existing metadata can be cleared before adding new metadata. Save a model file with custom metadata. This function reads the source model file, adds or updates its metadata, and saves it to a new destination file. It can optionally clear existing metadata before adding new metadata. :param src_model_file: The path to the source model file. :type src_model_file: str :param dst_model_file: The path to save the new model file. :param dst_model_file: The path where the new model file will be saved. :type dst_model_file: str :param metadata: The metadata to add to the model file. :param metadata: A dictionary of metadata to add or update in the model file. :type metadata: Dict[str, str] :param clear: Whether to clear existing metadata before adding new metadata. Default is False. :param clear: If True, clear existing metadata before adding new metadata. Default is False. :type clear: bool :raises EnvironmentError: If required dependencies are not installed. Usage: >>> new_metadata = {"author": "John Doe", "version": "1.0"} >>> save_with_metadata("input_model.safetensors", "output_model.safetensors", new_metadata) # This will add or update the "author" and "version" metadata in the new file >>> save_with_metadata("input_model.safetensors", "output_model.safetensors", new_metadata, clear=True) # This will clear all existing metadata and only include the new metadata in the output file """ _check_env() with safetensors.safe_open(src_model_file, framework='pt') as f: Loading Loading
docs/source/api_doc/metadata/geninfo.rst 0 → 100644 +50 −0 Original line number Diff line number Diff line imgutils.metadata.geninfo ========================================== .. currentmodule:: imgutils.metadata.geninfo .. automodule:: imgutils.metadata.geninfo read_geninfo_parameters ---------------------------------------------------------- .. autofunction:: read_geninfo_parameters read_geninfo_exif ---------------------------------------------------------- .. autofunction:: read_geninfo_exif read_geninfo_gif ---------------------------------------------------------- .. autofunction:: read_geninfo_gif write_geninfo_parameters ---------------------------------------------------------- .. autofunction:: write_geninfo_parameters write_geninfo_exif ---------------------------------------------------------- .. autofunction:: write_geninfo_exif write_geninfo_gif ---------------------------------------------------------- .. autofunction:: write_geninfo_gif
docs/source/api_doc/metadata/index.rst +1 −0 Original line number Diff line number Diff line Loading @@ -9,5 +9,6 @@ imgutils.metadata .. toctree:: :maxdepth: 3 geninfo lsb
imgutils/metadata/__init__.py +11 −0 Original line number Diff line number Diff line """ This module provides functionality for reading and writing generation information (geninfo) and metadata using various methods. It includes functions for handling generation information in different formats (parameters, EXIF, GIF) and working with LSB (Least Significant Bit) metadata. The module is designed to be used in image processing and generation tasks, particularly in the context of AI-generated images. """ from .geninfo import read_geninfo_parameters, read_geninfo_exif, read_geninfo_gif, \ write_geninfo_parameters, write_geninfo_exif, write_geninfo_gif from .lsb import read_lsb_raw_bytes, read_lsb_metadata, write_lsb_raw_bytes, write_lsb_metadata, LSBReadError
imgutils/sd/__init__.py +16 −1 Original line number Diff line number Diff line """ Overview: Utilities for dealing with data from `AUTOMATIC1111/stable-diffusion-webui <https://github.com/AUTOMATIC1111/stable-diffusion-webui>`_. Utilities for dealing with data from AUTOMATIC1111/stable-diffusion-webui and NovelAI generated images. This module provides a collection of utilities for handling metadata and image processing related to Stable Diffusion WebUI (SDWUI) and NovelAI generated images. It includes functions for parsing and extracting metadata, reading and writing model metadata, and manipulating NovelAI image metadata. Submodules: - metadata: Functions for parsing and extracting Stable Diffusion metadata. - model: Utilities for reading and writing metadata from/to model files. - nai: Functions for handling NovelAI image metadata. For detailed usage of each function and class, please refer to their individual docstrings. """ from .metadata import parse_sdmeta_from_text, get_sdmeta_from_image, SDMetaData from .model import read_metadata, save_with_metadata from .nai import get_naimeta_from_image, NAIMetadata, add_naimeta_to_image, save_image_with_naimeta
imgutils/sd/model.py +58 −16 Original line number Diff line number Diff line """ Overview: A utility for reading and writing metadata from/to model files in the A41 WebUI format. A utility module for reading and writing metadata from/to model files in the A41 WebUI format. This module provides functions to read metadata from model files and save model files with custom metadata. It supports operations on SafeTensors files, which are a safe and efficient way to store tensors. .. note:: ``torch`` and ``safetensors`` are required by this model. Please install them with the following command before start using this module. ``torch`` and ``safetensors`` are required by this module. Please install them with the following command before using this module: .. code:: shell pip install dghs-imgutils[model] Functions: - read_metadata: Reads metadata from a model file. - save_with_metadata: Saves a model file with custom metadata. - _check_env: Internal function to check if required dependencies are installed. Dependencies: - torch - safetensors """ from typing import Dict Loading @@ -26,8 +40,13 @@ except (ImportError, ModuleNotFoundError): # pragma: no cover def _check_env(): """ Checks if the required dependencies (Safetensors and Torch) are installed. Raises EnvironmentError if they are not installed. Check if the required dependencies (Safetensors and Torch) are installed. This function verifies that both Safetensors and Torch are available in the current environment. If either is missing, it raises an EnvironmentError with instructions on how to install the missing dependency. :raises EnvironmentError: If Safetensors or Torch is not installed. """ if not safetensors: raise EnvironmentError( Loading @@ -39,12 +58,22 @@ def _check_env(): def read_metadata(model_file: str) -> Dict[str, str]: """ Reads metadata from a model file and returns it as a dictionary. Read metadata from a model file and return it as a dictionary. This function opens the specified model file using SafeTensors and extracts its metadata. The metadata is returned as a dictionary where both keys and values are strings. :param model_file: The path to the model file. :param model_file: The path to the model file to read metadata from. :type model_file: str :return: The metadata extracted from the model file. :return: A dictionary containing the metadata of the model file. :rtype: Dict[str, str] :raises EnvironmentError: If required dependencies are not installed. Usage: >>> metadata = read_metadata("path/to/model.safetensors") >>> print(metadata) {'key1': 'value1', 'key2': 'value2', ...} """ _check_env() with safetensors.safe_open(model_file, 'pt') as f: Loading @@ -53,16 +82,29 @@ def read_metadata(model_file: str) -> Dict[str, str]: def save_with_metadata(src_model_file: str, dst_model_file: str, metadata: Dict[str, str], clear: bool = False): """ Saves a model file with metadata. Optionally, existing metadata can be cleared before adding new metadata. Save a model file with custom metadata. This function reads the source model file, adds or updates its metadata, and saves it to a new destination file. It can optionally clear existing metadata before adding new metadata. :param src_model_file: The path to the source model file. :type src_model_file: str :param dst_model_file: The path to save the new model file. :param dst_model_file: The path where the new model file will be saved. :type dst_model_file: str :param metadata: The metadata to add to the model file. :param metadata: A dictionary of metadata to add or update in the model file. :type metadata: Dict[str, str] :param clear: Whether to clear existing metadata before adding new metadata. Default is False. :param clear: If True, clear existing metadata before adding new metadata. Default is False. :type clear: bool :raises EnvironmentError: If required dependencies are not installed. Usage: >>> new_metadata = {"author": "John Doe", "version": "1.0"} >>> save_with_metadata("input_model.safetensors", "output_model.safetensors", new_metadata) # This will add or update the "author" and "version" metadata in the new file >>> save_with_metadata("input_model.safetensors", "output_model.safetensors", new_metadata, clear=True) # This will clear all existing metadata and only include the new metadata in the output file """ _check_env() with safetensors.safe_open(src_model_file, framework='pt') as f: Loading