From 0420874f562ecb10f9744cb9d8315d2188412ed4 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Tue, 19 Sep 2023 14:59:44 -0400 Subject: [PATCH 1/2] reimplement the old invokeai-metadata command --- .../backend/image_util/invoke_metadata.py | 46 +++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 invokeai/backend/image_util/invoke_metadata.py diff --git a/invokeai/backend/image_util/invoke_metadata.py b/invokeai/backend/image_util/invoke_metadata.py new file mode 100644 index 0000000000..424eba54c3 --- /dev/null +++ b/invokeai/backend/image_util/invoke_metadata.py @@ -0,0 +1,46 @@ +# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Development Team + +"""Very simple functions to fetch and print metadata from InvokeAI-generated images.""" + +import json +import sys +from pathlib import Path +from typing import Any, Dict + +from PIL import Image + + +def get_invokeai_metadata(image_path: Path) -> Dict[str, Any]: + """ + Retrieve "invokeai_metadata" field from png image. + + :param image_path: Path to the image to read metadata from. + May raise: + OSError -- image path not found + KeyError -- image doesn't contain the metadata field + """ + image: Image = Image.open(image_path) + return json.loads(image.text["invokeai_metadata"]) + + +def print_invokeai_metadata(image_path: Path): + """Pretty-print the metadata.""" + try: + metadata = get_invokeai_metadata(image_path) + print(f"{image_path}:\n{json.dumps(metadata, sort_keys=True, indent=4)}") + except OSError: + print(f"{image_path}:\nNo file found.") + except KeyError: + print(f"{image_path}:\nNo metadata found.") + print() + + +def main(): + """Run the command-line utility.""" + image_paths = sys.argv[1:] + if not image_paths: + print(f"Usage: {Path(sys.argv[0]).name} image1 image2 image3 ...") + print("\nPretty-print InvokeAI image metadata from the listed png files.") + sys.exit(-1) + for img in image_paths: + print_invokeai_metadata(img) diff --git a/pyproject.toml b/pyproject.toml index 2ea5455c3d..6f68d5b362 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -142,7 +142,7 @@ dependencies = [ "invokeai-model-install" = "invokeai.frontend.install.model_install:main" "invokeai-migrate3" = "invokeai.backend.install.migrate_to_3:main" "invokeai-update" = "invokeai.frontend.install.invokeai_update:main" -"invokeai-metadata" = "invokeai.frontend.CLI.sd_metadata:print_metadata" +"invokeai-metadata" = "invokeai.backend.image_util.invoke_metadata:main" "invokeai-node-cli" = "invokeai.app.cli_app:invoke_cli" "invokeai-node-web" = "invokeai.app.api_app:invoke_api" "invokeai-import-images" = "invokeai.frontend.install.import_images:main" From 2d71f6f4b8152539993b03c0358afebca76ebdc2 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Tue, 19 Sep 2023 17:12:43 -0400 Subject: [PATCH 2/2] add documentation --- docs/features/index.md | 3 +++ docs/index.md | 1 + 2 files changed, 4 insertions(+) diff --git a/docs/features/index.md b/docs/features/index.md index 490da5e31f..bd37366314 100644 --- a/docs/features/index.md +++ b/docs/features/index.md @@ -51,6 +51,9 @@ Prevent InvokeAI from displaying unwanted racy images. ### * [Controlling Logging](LOGGING.md) Control how InvokeAI logs status messages. +### * [Command-line Utilities](UTILITIES.md) +A list of the command-line utilities available with InvokeAI. +