File size: 7,096 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import argparse
import json
from PIL import Image, PngImagePlugin
from rich.console import Console
from rich import print
from rich_argparse import RichHelpFormatter
import os
from pathlib import Path

console = Console()

# BNK_CutoffSetRegions
# BNK_CutoffRegionsToConditioning
# BNK_CutoffBasePrompt


# Extracts metadata from a PNG image and returns it as a dictionary
def extract_metadata(image_path):
    image = Image.open(image_path)
    prompt = image.info.get("prompt", "")
    workflow = image.info.get("workflow", "")

    if workflow:
        workflow = json.loads(workflow)

    if prompt:
        prompt = json.loads(prompt)

    console.print(f"Metadata extracted from [cyan]{image_path}[/cyan].")

    return {
        "prompt": prompt,
        "workflow": workflow,
    }


# Embeds metadata into a PNG image
def embed_metadata(image_path, metadata):
    image = Image.open(image_path)
    o_metadata = image.info

    pnginfo = PngImagePlugin.PngInfo()
    if prompt := metadata.get("prompt"):
        pnginfo.add_text("prompt", json.dumps(prompt))
    elif "prompt" in o_metadata:
        pnginfo.add_text("prompt", o_metadata["prompt"])

    if workflow := metadata.get("workflow"):
        pnginfo.add_text("workflow", json.dumps(workflow))
    elif "workflow" in o_metadata:
        pnginfo.add_text("workflow", o_metadata["workflow"])

    imgp = Path(image_path)
    output = imgp.with_stem(f"{imgp.stem}_comfy_embed")
    index = 1
    while output.exists():
        output = imgp.with_stem(f"{imgp.stem}_{index}_comfy_embed").with_suffix(".png")
        index += 1

    image.save(output, pnginfo=pnginfo)
    console.print(f"Metadata embedded into [cyan]{output}[/cyan].")


# CLI subcommand: extract
def extract(args):
    input_files = []
    for input_path in args.input:
        if os.path.isdir(input_path):
            folder_path = input_path
            input_files.extend(
                [
                    os.path.join(folder_path, file_name)
                    for file_name in os.listdir(folder_path)
                    if file_name.lower().endswith((".png", ".jpg", ".jpeg"))
                ]
            )
        else:
            input_files.append(input_path)

    if len(input_files) == 1:
        metadata = extract_metadata(input_files[0])
        if args.print_output:
            print(json.dumps(metadata, indent=4))
        else:
            if not args.output:
                output = Path(input_files[0]).with_suffix(".json")
                index = 1
                while output.exists():
                    output = (
                        Path(input_files[0])
                        .with_stem(f"{Path(input_files[0]).stem}_{index}")
                        .with_suffix(".json")
                    )
                    index += 1
            else:
                output = args.output
            with open(output, "w") as file:
                json.dump(metadata, file, indent=4)
            console.print(f"Metadata extracted and saved to [cyan]{output}[/cyan].")
    else:
        metadata_dict = {}
        for input_file in input_files:
            metadata = extract_metadata(input_file)
            filename = os.path.basename(input_file)
            output = (
                Path(args.output) / f"{filename}.json"
                if args.output
                else Path(input_file).with_suffix(".json")
            )
            index = 1
            while output.exists():
                output = Path(args.output).parent / f"{filename}_{index}.json"
                index += 1
            with open(output, "w") as file:
                json.dump(metadata, file, indent=4)
            metadata_dict[filename] = metadata
        if args.output:
            with open(args.output, "w") as file:
                json.dump(metadata_dict, file, indent=4)
            console.print(
                f"Metadata extracted and saved to [cyan]{args.output}[/cyan]."
            )
        else:
            console.print("Multiple metadata files created.")


# CLI subcommand: embed
def embed(args):
    input_files = []
    for input_path in args.input:
        if os.path.isdir(input_path):
            folder_path = input_path
            input_files.extend(
                [
                    os.path.join(folder_path, file_name)
                    for file_name in os.listdir(folder_path)
                    if file_name.lower().endswith(".json")
                ]
            )
        else:
            input_files.append(input_path)

    for input_file in input_files:
        with open(input_file) as file:
            metadata = json.load(file)
        image_path = input_file.replace(".json", ".png")
        if args.output:
            output_dir = args.output
            if os.path.isdir(output_dir):
                output_path = os.path.join(output_dir, os.path.basename(image_path))
                index = 1
                while os.path.exists(output_path):
                    output_path = os.path.join(
                        output_dir,
                        f"{os.path.basename(image_path)}_{index}.png",
                    )
                    index += 1
            else:
                output_path = output_dir
        else:
            output_path = image_path.replace(".png", "_comfy_embed.png")

        embed_metadata(image_path, metadata)
        # os.rename(image_path, output_path)
        console.print(f"Metadata embedded into [cyan]{output_path}[/cyan].")


if __name__ == "__main__":
    # Create the main CLI parser
    parser = argparse.ArgumentParser(
        prog="image-metadata-cli", formatter_class=RichHelpFormatter
    )
    subparsers = parser.add_subparsers(title="subcommands")

    # Parser for the "extract" subcommand
    extract_parser = subparsers.add_parser(
        "extract",
        help="Extract metadata from PNG image(s) or folder",
        formatter_class=RichHelpFormatter,
    )
    extract_parser.add_argument(
        "input", nargs="+", help="Input PNG image file(s) or folder path"
    )
    extract_parser.add_argument(
        "--print",
        dest="print_output",
        action="store_true",
        help="Print the output to stdout",
    )
    extract_parser.add_argument("--output", help="Output JSON file(s) or directory")
    extract_parser.set_defaults(func=extract)

    # Parser for the "embed" subcommand
    embed_parser = subparsers.add_parser(
        "embed",
        help="Embed metadata into PNG image(s) or folder",
        formatter_class=RichHelpFormatter,
    )
    embed_parser.add_argument(
        "input", nargs="+", help="Input JSON file(s) or folder path"
    )
    embed_parser.add_argument("--output", help="Output PNG image file(s) or directory")
    embed_parser.set_defaults(func=embed)

    # Parse the command-line arguments and execute the appropriate subcommand
    args = parser.parse_args()
    if hasattr(args, "func"):
        try:
            args.func(args)
        except ValueError as e:
            console.print(f"[bold red]Error:[/bold red] {str(e)}")
    else:
        parser.print_help()