Skip to main content

SD Parsers - read metadata from images created by Stable Diffusion

Project description

Features

Supports reading metadata from images generated with:

  • Automatic1111's Stable Diffusion web UI
  • ComfyUI *
  • InvokeAI
  • NovelAI

Provides a list of prompts used in the generation of the image, as well as generator-specific metadata.

* Custom ComfyUI nodes might parse incorrectly / with incomplete data.

Installation

pip install sd-parsers

Usage

For a simple query, import ParserManager from sdparsers and use its parse() method to parse an image. (see examples)

The ParserManager() constructor takes two arguments:

  • config_file: If you want to provide alternate processing instructions. (absolute path to config file)
  • process_items: If the parser should try to normalize the output across the different image sources. (defaults to True)

Basic usage:

Read prompt information from a given filename:

from sdparsers import ParserManager

parser_manager = ParserManager()
prompt_data = parser_manager.parse('image.png')

for prompt, negative_prompt in prompt_data.prompts:
    if prompt:
        print(f"Prompt: {prompt.value}")
    if negative_prompt:
        print(f"Negative Prompt: {negative_prompt.value}")

Read prompt information from an already opened image:

from PIL import Image
from sdparsers import ParserManager

parser_manager = ParserManager()
with Image.open('image.png') as image:
    prompt_data = parser_manager.parse(image)

...

Each parser can also be used directly, omitting the use of ParserManager.

For that simply import the specific parser instead:

from PIL import Image
from sdparsers import AUTOMATIC1111Parser

parser = AUTOMATIC1111Parser()
with Image.open("image.png") as image:
    prompt_info = parser.parse(image)

Directly using a specific parser, the configuration file is not read automatically. (see ParserManager.__init__())

Also, when not called via ParserManager, the parse() method now requires a Pillow Image object as argument. (see ParserManager.parse())

Output

The parse() method returns a PromptInfo type with the following properties (or None if no metadata was found):

  • generator: A simple string, specifying the parsing module that was used.

    ("AUTOMATIC1111" | "AUTOMATICStealth" | "ComfyUI" | "InvokeAI")

  • prompts: A list of tuples of prompts as found in the parsed metadata.

    In the form of (prompt, negative_prompt).

  • samplers: An unordered list of found samplers (and parameters).

    The key values of the parameters dictionary will be normalized to be more consistent across image generators if ParserManager is used with process_items set to True.

  • models: An unordered list of models used in the generation process.

  • metadata: A dictionary of metadata besides the prompt information.

    Contains additional parameters which are found in the image metadata.

    Highly dependent on the provided data structure of the respective image generator.

    The key values of this dictionary will be normalized to be more consistent across image generators if ParserManager is used with process_items set to True.

  • raw_params: A dictionary of the unmodified metadata entries found in the parsed image (if present).

    • Automatic1111: "parameters"
    • InvokeAI: "sd-metadata" and "Dream"
    • ComfyUI: "prompt" and "workflow"

Credits

Idea and motivation using AUTOMATIC1111's stable diffusion webui

Stealth PNGInfo code adopted from ashen-sensored's sd webui extension

Example workflows for testing the ComfyUI parser

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sd-parsers-0.2.3.tar.gz (26.9 kB view hashes)

Uploaded Source

Built Distribution

sd_parsers-0.2.3-py3-none-any.whl (13.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page