avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Managing PNGInfo in Images Generated by Stable Diffusion Web UI

Stable Diffusion Web UI uniquely integrates generation parameters into a PNG's exif meta information using a custom tag, parameters. This proprietary approach is instrumental for users who aim to conduct advanced Stable Diffusion generation tasks, such as text-to-image or image-to-image transformations, as it maintains a traceable lineage of generation parameters.

Reading PNGInfo

While Stable Diffusion Web UI comes equipped with an in-built feature to peruse the PNGInfo, the program's robustness might not align with the capabilities of all hardware setups. The platform, while undeniably powerful, requires substantial resources to operate smoothly. In scenarios where a quick metadata peek is the goal, booting up such an extensive program might seem overkill. For such instances, a straightforward command-line utility like exiftool becomes the go-to, efficiently extracting the embedded data.

exiftool image.png # Reveals the entirety of the exif metadata.
# For a more focused output:
exiftool -Parameters image.png # Specifically targets the generation parameters.

Maintaining Metadata Post Edits

During external image edits, it's not uncommon for the embedded custom pnginfo tag to be lost. While exiftool is adept at retrieving this metadata, its inability to process the custom parameters tag becomes evident. To tackle this challenge, I wrote a dedicated Python program. This program ensures the pnginfo from the original image is transferred to its edited counterpart.

from PIL import Image, PngImagePlugin
import sys
import os
import shutil

def copy_pnginfo_and_image(source_path, target_path):
    # Extract the file base name and extension of the target image
    base, ext = os.path.splitext(target_path)
    # Define the output path for the new image with updated metadata
    output_path = f"{base}-pnginfo{ext}"

    # Open the source image to extract 'Parameters' metadata
    with Image.open(source_path) as source_img:
        source_info = source_img.info.get("parameters", None)
        # Check if 'Parameters' metadata is present in the source image
        if source_info is None:
            print(f"'Parameters' not found in {source_path}")
            return

    # Copy the binary data of the target image to the output file
    with open(target_path, 'rb') as target_file:
        with open(output_path, 'wb') as output_file:
            shutil.copyfileobj(target_file, output_file)

    # Update the metadata (Parameters) in the output file
    with Image.open(output_path) as output_img:
        pnginfo = PngImagePlugin.PngInfo()
        pnginfo.add_text("parameters", source_info)
        output_img.save(output_path, "PNG", pnginfo=pnginfo)

    print(f"Updated the 'Parameters' metadata in {output_path}")

def main():
    if len(sys.argv) < 3:
        print("Usage: cppnginfo <source_path> <target_path>")
        sys.exit(1)

    # Get the source and target image paths from command-line arguments
    source_path = sys.argv[1]
    target_path = sys.argv[2]

    # Call the function to copy metadata and update the image
    copy_pnginfo_and_image(source_path, target_path)

if __name__ == '__main__':
    # Call the main function when the script is executed
    main()

This CLI tool (Command Line Interface) can be smoothly integrated via Python's package manager, pip:

pip install git+https://github.com/ShinChven/cppnginfo.git

Upon successful installation, you can run the following command:

cppnginfo source.png target.png

This command extracts the pnginfo from source.png and incorporates it into target.png. Following the process, an output image is created with the name derived from the target image, but with an appended -pnginfo suffix. This system ensures that you always have a distinct version of the edited image with the restored metadata, making it a hassle-free approach to maintain the integrity of your image's data lineage.

Why Retaining Metadata Matters

Imagine you've generated a unique image using specific parameters that took hours of fine-tuning. You then edit this image using third-party software, only to discover that you've lost the original generation parameters. Without this critical metadata, replicating or building upon the original image becomes a daunting task. This underscores the value of the metadata and the importance of the Python tool in ensuring its continuity.

Conclusion

In the realm of AI image generation, the significance of embedded metadata cannot be understated. Stable Diffusion Web UI's distinct approach to integrating the parameters tag within PNG images showcases the platform's foresight in ensuring data continuity and traceability. However, challenges arise when external edits risk erasing this invaluable information. This is where the importance of tools like the cppnginfo Python program I developed comes into play. By providing a streamlined way to read and restore metadata, even when external tools falter, we not only uphold the integrity of our images but also ensure that the rich history of our creative processes remains intact and accessible.