Extract prompt from EXIF with Reg Ex

This is a process starting with an AI image generated using ComfyUI and extracting the prompt into a JSON format using Python. This demonstrates my understanding of fundamental concepts such as regular expressions, different file formats, and python syntax.

{“prompt”: {“5”: {“inputs”: {“width”: 1024, “height”: 1024, “batch_size”: 1}, “class_type”: “EmptyLatentImage”}, “6”: {“inputs”: {“text”: “(title text at the top that says \”Reg Ex Demonstration\”), digital glitch haze fragmentation, extreme detail, vibrant colors, deep shadows, metallic oil iridescent chromatic aberration, rainbow clean professional, pink fun whimsical celebration), marker sharpie, \n(text that says \”jaykrown\” in the bottom right, bold word), \nvery high quality, high detail, high resolution, photographic 4k”, “clip”: [“11”, 0]}, “class_type”: “CLIPTextEncode”}, “8”: {“inputs”: {“samples”: [“30”, 0], “vae”: [“10”, 0]}, “class_type”: “VAEDecode”}, “10”: {“inputs”: {“vae_name”: “ae.safetensors”}, “class_type”: “VAELoader”}, “11”: {“inputs”: {“clip_name1”: “t5xxl_fp16.safetensors”, “clip_name2”: “clip_l.safetensors”, “type”: “flux”}, “class_type”: “DualCLIPLoader”}, “27”: {“inputs”: {“unet_name”: “FusionDS_v0_Q8.gguf”}, “class_type”: “UnetLoaderGGUF”}, “30”: {“inputs”: {“seed”: 644729377021114, “steps”: 10, “cfg”: 1.0, “sampler_name”: “euler”, “scheduler”: “simple”, “denoise”: 1.0, “model”: [“27”, 0], “positive”: [“6”, 0], “negative”: [“31”, 0], “latent_image”: [“5”, 0]}, “class_type”: “KSampler”}, “31”: {“inputs”: {“text”: “”, “clip”: [“11”, 0]}, “class_type”: “CLIPTextEncode”}, “34”: {“inputs”: {“filename_prefix”: “ComfyUI”, “file_type”: “JPEG”, “images”: [“8”, 0]}, “class_type”: “SaveImageExtended”}}, “workflow”: {“last_node_id”: 34, “last_link_id”: 63, “nodes”: [{“id”: 8, “type”: “VAEDecode”, “pos”: {“0”: 1170, “1”: 90, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 140, “1”: 60}, “flags”: {}, “order”: 7, “mode”: 0, “inputs”: [{“name”: “samples”, “type”: “LATENT”, “link”: 58}, {“name”: “vae”, “type”: “VAE”, “link”: 47}], “outputs”: [{“name”: “IMAGE”, “type”: “IMAGE”, “links”: [63], “slot_index”: 0}], “properties”: {“Node name for S&R”: “VAEDecode”}}, {“id”: 10, “type”: “VAELoader”, “pos”: {“0”: 910, “1”: 90, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 250, “1”: 60}, “flags”: {}, “order”: 0, “mode”: 0, “inputs”: [], “outputs”: [{“name”: “VAE”, “type”: “VAE”, “links”: [47], “slot_index”: 0, “shape”: 3}], “properties”: {“Node name for S&R”: “VAELoader”}, “widgets_values”: [“ae.safetensors”]}, {“id”: 11, “type”: “DualCLIPLoader”, “pos”: {“0”: 140, “1”: 340, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 290, “1”: 110}, “flags”: {}, “order”: 1, “mode”: 0, “inputs”: [], “outputs”: [{“name”: “CLIP”, “type”: “CLIP”, “links”: [59, 60], “slot_index”: 0, “shape”: 3}], “properties”: {“Node name for S&R”: “DualCLIPLoader”}, “widgets_values”: [“t5xxl_fp16.safetensors”, “clip_l.safetensors”, “flux”]}, {“id”: 27, “type”: “UnetLoaderGGUF”, “pos”: {“0”: 450, “1”: 90, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 450, “1”: 60}, “flags”: {}, “order”: 2, “mode”: 0, “inputs”: [], “outputs”: [{“name”: “MODEL”, “type”: “MODEL”, “links”: [57], “slot_index”: 0, “shape”: 3}], “properties”: {“Node name for S&R”: “UnetLoaderGGUF”}, “widgets_values”: [“FusionDS_v0_Q8.gguf”]}, {“id”: 31, “type”: “CLIPTextEncode”, “pos”: {“0”: 450, “1”: 490, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 450, “1”: 100}, “flags”: {}, “order”: 4, “mode”: 0, “inputs”: [{“name”: “clip”, “type”: “CLIP”, “link”: 59}], “outputs”: [{“name”: “CONDITIONING”, “type”: “CONDITIONING”, “links”: [56], “slot_index”: 0}], “properties”: {“Node name for S&R”: “Negative Prompt”}, “widgets_values”: [“”], “color”: “#322”, “bgcolor”: “#533”}, {“id”: 34, “type”: “SaveImageExtended”, “pos”: {“0”: 1325, “1”: 92, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 718.59033203125, “1”: 1342.2855224609375}, “flags”: {}, “order”: 8, “mode”: 0, “inputs”: [{“name”: “images”, “type”: “IMAGE”, “link”: 63}], “outputs”: [], “properties”: {“Node name for S&R”: “SaveImageExtended”}, “widgets_values”: [“ComfyUI”, “JPEG”]}, {“id”: 5, “type”: “EmptyLatentImage”, “pos”: {“0”: 690, “1”: 630, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 210, “1”: 110}, “flags”: {}, “order”: 3, “mode”: 0, “inputs”: [], “outputs”: [{“name”: “LATENT”, “type”: “LATENT”, “links”: [53], “slot_index”: 0}], “properties”: {“Node name for S&R”: “EmptyLatentImage”}, “widgets_values”: [1024, 1024, 1]}, {“id”: 30, “type”: “KSampler”, “pos”: {“0”: 910, “1”: 190, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 320, “1”: 262}, “flags”: {}, “order”: 6, “mode”: 0, “inputs”: [{“name”: “model”, “type”: “MODEL”, “link”: 57}, {“name”: “positive”, “type”: “CONDITIONING”, “link”: 54}, {“name”: “negative”, “type”: “CONDITIONING”, “link”: 56}, {“name”: “latent_image”, “type”: “LATENT”, “link”: 53}], “outputs”: [{“name”: “LATENT”, “type”: “LATENT”, “links”: [58], “slot_index”: 0, “shape”: 3}], “properties”: {“Node name for S&R”: “KSampler”}, “widgets_values”: [644729377021114, “randomize”, 10, 1, “euler”, “simple”, 1]}, {“id”: 6, “type”: “CLIPTextEncode”, “pos”: {“0”: 450, “1”: 190, “2”: 0, “3”: 0, “4”: 0, “5”: 0, “6”: 0, “7”: 0, “8”: 0, “9”: 0}, “size”: {“0”: 450, “1”: 260}, “flags”: {}, “order”: 5, “mode”: 0, “inputs”: [{“name”: “clip”, “type”: “CLIP”, “link”: 60}], “outputs”: [{“name”: “CONDITIONING”, “type”: “CONDITIONING”, “links”: [54], “slot_index”: 0}], “properties”: {“Node name for S&R”: “CLIPTextEncode”}, “widgets_values”: [“(title text at the top that says \”Reg Ex Demonstration\”), digital glitch haze fragmentation, extreme detail, vibrant colors, deep shadows, metallic oil iridescent chromatic aberration, rainbow clean professional, pink fun whimsical celebration), marker sharpie, \n(text that says \”jaykrown\” in the bottom right, bold word), \nvery high quality, high detail, high resolution, photographic 4k”], “color”: “#232”, “bgcolor”: “#353”}], “links”: [[47, 10, 0, 8, 1, “VAE”], [53, 5, 0, 30, 3, “LATENT”], [54, 6, 0, 30, 1, “CONDITIONING”], [56, 31, 0, 30, 2, “CONDITIONING”], [57, 27, 0, 30, 0, “MODEL”], [58, 30, 0, 8, 0, “LATENT”], [59, 11, 0, 31, 0, “CLIP”], [60, 11, 0, 6, 0, “CLIP”], [63, 8, 0, 34, 0, “IMAGE”]], “groups”: [], “config”: {}, “extra”: {“ds”: {“scale”: 0.876922695000036, “offset”: [234.54340870758506, 100.03176217164219]}}, “version”: 0.4}}

Python script:

import re
import json
from PIL import Image
img = Image.open('ComfyUI_00001_.jpg')
pattern = r'"text": "(.*?)", "clip": '
exif_data = [(tag, value) for tag, value in img._getexif().items() if re.search(pattern, str(value))]
matches = [re.search(pattern, str(value)).group(1) for tag, value in exif_data]
json_data = []
for text in matches:
    words = text.split()
    for word in words:
        json_data.append(word)
with open('output.json', 'w') as f:
    json.dump(json_data, f)

Output:

["(title", "text", "at", "the", "top", "that", "says", "\\\"Reg", "Ex", "Demonstration\\\"),", "digital", "glitch", "haze", "fragmentation,", "extreme", "detail,", "vibrant", "colors,", "deep", "shadows,", "metallic", "oil", "iridescent", "chromatic", "aberration,", "rainbow", "clean", "professional,", "pink", "fun", "whimsical", "celebration),", "marker", "sharpie,", "\\n(text", "that", "says", "\\\"jaykrown\\\"", "in", "the", "bottom", "right,", "bold", "word),", "\\nvery", "high", "quality,", "high", "detail,", "high", "resolution,", "photographic", "4k"]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *