sd-parsers


Namesd-parsers JSON
Version 0.5 PyPI version JSON
download
home_pageNone
Summarya library to read metadata from images created by Stable Diffusion
upload_time2024-11-24 19:09:39
maintainerNone
docs_urlNone
authord3x-at
requires_python>=3.8
licenseMIT License Copyright (c) 2023 d3x-at Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Features

Supports reading metadata from images generated with:
* Automatic1111's Stable Diffusion web UI
* ComfyUI *
* Fooocus
* 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

From command line: ```python3 -m sd_parsers <filenames>```.


### Basic usage:

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

#### Read prompt information from a given filename with `parse()`:
```python
from sd_parsers import ParserManager

parser_manager = ParserManager()

def main():
    prompt_info = parser_manager.parse("image.png")

    if prompt_info:
        for prompt in prompt_info.prompts:
            print(f"Prompt: {prompt.value}")
```

#### Read prompt information from an already opened image:
```python
from PIL import Image
from sd_parsers import ParserManager

parser_manager = ParserManager()

def main():
    with Image.open('image.png') as image:
        prompt_info = parser_manager.parse(image)
```

#### Each parser module can also be used directly, omitting the use of ```ParserManager```:

```python
from PIL import Image
from sd_parsers.data import PromptInfo
from sd_parsers.exceptions import ParserError
from sd_parsers.parsers import AUTOMATIC1111Parser

parser = AUTOMATIC1111Parser()


def main():
    try:
        with Image.open("image.png") as image:
            # read_parameters() returns relevant image metadata parameters
            # and optional context information needed for parsing
            parameters, parsing_context = parser.read_parameters(image)

        # parse() builds a standardized data structure from the raw parameters
        generator, samplers, metadata = parser.parse(parameters, parsing_context)

    except ParserError:
        ...

    # creating a PromptInfo object from the obtained data allows for the use
    # of convenience poperties like ".prompts" or ".models"
    prompt_info = PromptInfo(generator, samplers, metadata, parameters)
```

### Output
The `parse()` method returns a `PromptInfo` ([source](src/sd_parsers/data/prompt_info.py)) object when suitable metadata is found.

> Use ```python3 -m sd_parsers <image.png>``` to get an idea of the data parsed from an image file.

> To get a result in JSON form, an approach as demonstrated in https://github.com/d3x-at/sd-parsers-web can be used.

`PromptInfo` contains the following properties :
* `generator`: Specifies the image [generator](src/sd_parsers/data/generators.py) that may have been used for creating the image.

* `full_prompt`: A full prompt, if present in the image metadata.

  Otherwise, a simple concatenation of all prompts found.

* `full_negative_prompt`: A full negative prompt if present in the image metadata. 
  
  Otherwise, a simple concatenation of all negative prompts found.

* `prompts`: All [prompts](src/sd_parsers/data/prompt.py) found in the parsed metadata.

* `negative_prompts`: All negative [prompts](src/sd_parsers/data/prompt.py) found in the parsed metadata.

* `models`: [Models](src/sd_parsers/data/model.py) used in the image generation process.

* `samplers`: [Samplers](src/sd_parsers/data/sampler.py) used in the image generation process.

  A Sampler contains the following properties specific to itself:
    * `name`: The name of the sampler

    * `parameters`: Generation parameters, including _cfg_scale_, _seed_, _steps_ and others.

    * `sampler_id`: A unique id of the sampler (if present in the metadata)

    * `model`: The model used by this sampler.

    * `prompts`: A list of positive prompts used by this sampler.
    
    * `negative_prompts`: A list of negative prompts used by this sampler.

* `metadata`: Additional metadata which could not be attributed to one of the former described.

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

* `raw_parameters`: The unprocessed metadata entries as found in the parsed image (if present).

## Contributing
As i don't have the time and resources to keep up with all the available AI-based image generators out there, the scale and features of this library is depending greatly on your help.

If you find the sd-parsers library unable to read metadata from an image, feel free to open an [issue](https://github.com/d3x-at/sd-parsers/issues).

See [CONTRIBUTING.md](https://github.com/d3x-at/sd-parsers/blob/master/.github/CONTRIBUTING.md), if you are willing to help with improving the library itself and/or to create/maintain an additional parser module.


## Credits
Idea and motivation using AUTOMATIC1111's stable diffusion webui
- https://github.com/AUTOMATIC1111/stable-diffusion-webui

Example workflows for testing the ComfyUI parser
- https://github.com/comfyanonymous/ComfyUI_examples

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sd-parsers",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "d3x-at",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/17/4a/611958d10d9a6c4794b3f699730baf86596298be4daf8658b9eeaa09d16e/sd_parsers-0.5.tar.gz",
    "platform": null,
    "description": "## Features\n\nSupports reading metadata from images generated with:\n* Automatic1111's Stable Diffusion web UI\n* ComfyUI *\n* Fooocus\n* InvokeAI\n* NovelAI\n\nProvides a list of prompts used in the generation of the image, as well as generator-specific metadata.\n\n\\* Custom ComfyUI nodes might parse incorrectly / with incomplete data.\n\n## Installation\n```\npip install sd-parsers\n```\n\n## Usage\n\nFrom command line: ```python3 -m sd_parsers <filenames>```.\n\n\n### Basic usage:\n\nFor a simple query, import ```ParserManager``` from ```sd_parsers``` and use its ```parse()``` method to parse an image. (see [examples](examples))\n\n#### Read prompt information from a given filename with `parse()`:\n```python\nfrom sd_parsers import ParserManager\n\nparser_manager = ParserManager()\n\ndef main():\n    prompt_info = parser_manager.parse(\"image.png\")\n\n    if prompt_info:\n        for prompt in prompt_info.prompts:\n            print(f\"Prompt: {prompt.value}\")\n```\n\n#### Read prompt information from an already opened image:\n```python\nfrom PIL import Image\nfrom sd_parsers import ParserManager\n\nparser_manager = ParserManager()\n\ndef main():\n    with Image.open('image.png') as image:\n        prompt_info = parser_manager.parse(image)\n```\n\n#### Each parser module can also be used directly, omitting the use of ```ParserManager```:\n\n```python\nfrom PIL import Image\nfrom sd_parsers.data import PromptInfo\nfrom sd_parsers.exceptions import ParserError\nfrom sd_parsers.parsers import AUTOMATIC1111Parser\n\nparser = AUTOMATIC1111Parser()\n\n\ndef main():\n    try:\n        with Image.open(\"image.png\") as image:\n            # read_parameters() returns relevant image metadata parameters\n            # and optional context information needed for parsing\n            parameters, parsing_context = parser.read_parameters(image)\n\n        # parse() builds a standardized data structure from the raw parameters\n        generator, samplers, metadata = parser.parse(parameters, parsing_context)\n\n    except ParserError:\n        ...\n\n    # creating a PromptInfo object from the obtained data allows for the use\n    # of convenience poperties like \".prompts\" or \".models\"\n    prompt_info = PromptInfo(generator, samplers, metadata, parameters)\n```\n\n### Output\nThe `parse()` method returns a `PromptInfo` ([source](src/sd_parsers/data/prompt_info.py)) object when suitable metadata is found.\n\n> Use ```python3 -m sd_parsers <image.png>``` to get an idea of the data parsed from an image file.\n\n> To get a result in JSON form, an approach as demonstrated in https://github.com/d3x-at/sd-parsers-web can be used.\n\n`PromptInfo` contains the following properties :\n* `generator`: Specifies the image [generator](src/sd_parsers/data/generators.py) that may have been used for creating the image.\n\n* `full_prompt`: A full prompt, if present in the image metadata.\n\n  Otherwise, a simple concatenation of all prompts found.\n\n* `full_negative_prompt`: A full negative prompt if present in the image metadata. \n  \n  Otherwise, a simple concatenation of all negative prompts found.\n\n* `prompts`: All [prompts](src/sd_parsers/data/prompt.py) found in the parsed metadata.\n\n* `negative_prompts`: All negative [prompts](src/sd_parsers/data/prompt.py) found in the parsed metadata.\n\n* `models`: [Models](src/sd_parsers/data/model.py) used in the image generation process.\n\n* `samplers`: [Samplers](src/sd_parsers/data/sampler.py) used in the image generation process.\n\n  A Sampler contains the following properties specific to itself:\n    * `name`: The name of the sampler\n\n    * `parameters`: Generation parameters, including _cfg_scale_, _seed_, _steps_ and others.\n\n    * `sampler_id`: A unique id of the sampler (if present in the metadata)\n\n    * `model`: The model used by this sampler.\n\n    * `prompts`: A list of positive prompts used by this sampler.\n    \n    * `negative_prompts`: A list of negative prompts used by this sampler.\n\n* `metadata`: Additional metadata which could not be attributed to one of the former described.\n\n  Highly dependent on the provided data structure of the respective image generator.\n\n* `raw_parameters`: The unprocessed metadata entries as found in the parsed image (if present).\n\n## Contributing\nAs i don't have the time and resources to keep up with all the available AI-based image generators out there, the scale and features of this library is depending greatly on your help.\n\nIf you find the sd-parsers library unable to read metadata from an image, feel free to open an [issue](https://github.com/d3x-at/sd-parsers/issues).\n\nSee [CONTRIBUTING.md](https://github.com/d3x-at/sd-parsers/blob/master/.github/CONTRIBUTING.md), if you are willing to help with improving the library itself and/or to create/maintain an additional parser module.\n\n\n## Credits\nIdea and motivation using AUTOMATIC1111's stable diffusion webui\n- https://github.com/AUTOMATIC1111/stable-diffusion-webui\n\nExample workflows for testing the ComfyUI parser\n- https://github.com/comfyanonymous/ComfyUI_examples\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 d3x-at  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "a library to read metadata from images created by Stable Diffusion",
    "version": "0.5",
    "project_urls": {
        "repository": "https://github.com/d3x-at/sd-parsers"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ea4f3087aab92ab0445d0e7eb693349b6450a3813d39f1e5ee47efb3cea3c86e",
                "md5": "c3fa1e2359e21318716af69c69f7d93b",
                "sha256": "7cadaebfa7e0b5708142de917b0fa499485da10c15b61ab3344c3491ae8625af"
            },
            "downloads": -1,
            "filename": "sd_parsers-0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c3fa1e2359e21318716af69c69f7d93b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 25694,
            "upload_time": "2024-11-24T19:09:37",
            "upload_time_iso_8601": "2024-11-24T19:09:37.808104Z",
            "url": "https://files.pythonhosted.org/packages/ea/4f/3087aab92ab0445d0e7eb693349b6450a3813d39f1e5ee47efb3cea3c86e/sd_parsers-0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "174a611958d10d9a6c4794b3f699730baf86596298be4daf8658b9eeaa09d16e",
                "md5": "ab9233a9bbf177e712dddea3737707aa",
                "sha256": "c722798b8d8e77f9c78d8909f23d287a572a85fe8dac453d36567fccdfde6053"
            },
            "downloads": -1,
            "filename": "sd_parsers-0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "ab9233a9bbf177e712dddea3737707aa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 46700,
            "upload_time": "2024-11-24T19:09:39",
            "upload_time_iso_8601": "2024-11-24T19:09:39.544407Z",
            "url": "https://files.pythonhosted.org/packages/17/4a/611958d10d9a6c4794b3f699730baf86596298be4daf8658b9eeaa09d16e/sd_parsers-0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-24 19:09:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "d3x-at",
    "github_project": "sd-parsers",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sd-parsers"
}
        
Elapsed time: 0.39288s