Squashed commit of the following:

commit 67fbaa7c31
Author: Lincoln Stein <lincoln.stein@gmail.com>
Date:   Fri Sep 16 16:57:54 2022 -0400

    reconciled conflicting changes to pngwriter call

commit ddc68b01f7
Merge: f9feaac cbac95b
Author: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
Date:   Sat Sep 17 06:39:22 2022 +1000

    Merge remote-tracking branch 'upstream/development' into development

commit f9feaac8c7
Author: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
Date:   Sat Sep 17 06:16:16 2022 +1000

    Fixes metadata related to new args

commit d1de1e357a
Author: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
Date:   Sat Sep 17 06:15:55 2022 +1000

    Fixes PromptFormatter import bug
This commit is contained in:
Lincoln Stein 2022-09-16 16:58:16 -04:00
parent cbac95b02a
commit fe12c6c099
3 changed files with 16 additions and 15 deletions

View File

@ -19,7 +19,8 @@ from uuid import uuid4
from ldm.gfpgan.gfpgan_tools import real_esrgan_upscale
from ldm.gfpgan.gfpgan_tools import run_gfpgan
from ldm.generate import Generate
from ldm.dream.pngwriter import PngWriter
from ldm.dream.pngwriter import PngWriter, retrieve_metadata
from modules.parameters import parameters_to_command, create_cmd_parser
@ -146,15 +147,12 @@ def handle_request_all_images():
paths.sort(key=lambda x: os.path.getmtime(x))
image_array = []
for path in paths:
image = Image.open(path)
metadata = {}
if 'Dream' in image.info:
try:
metadata = vars(parser.parse_args(shlex.split(image.info['Dream'])))
except SystemExit:
# TODO: Unable to parse metadata, ignore it for now,
# this can happen when metadata is missing a prompt
pass
# image = Image.open(path)
all_metadata = retrieve_metadata(path)
if 'Dream' in all_metadata and not all_metadata['sd-metadata']:
metadata = vars(parser.parse_args(shlex.split(all_metadata['Dream'])))
else:
metadata = all_metadata['sd-metadata']
image_array.append({'path': path, 'metadata': metadata})
return make_response("OK", data=image_array)
@ -307,7 +305,7 @@ def save_image(image, parameters, output_dir, step_index=None, postprocessing=Fa
command = parameters_to_command(parameters)
path = pngwriter.save_image_and_prompt_to_png(image, command, filename)
path = pngwriter.save_image_and_prompt_to_png(image, command, metadata=parameters, name=filename)
return path

View File

@ -34,6 +34,7 @@ class PngWriter:
# saves image named _image_ to outdir/name, writing metadata from prompt
# returns full path of output
def save_image_and_prompt_to_png(self, image, dream_prompt, name, metadata=None):
print(f'self.outdir={self.outdir}, name={name}')
path = os.path.join(self.outdir, name)
info = PngImagePlugin.PngInfo()
info.add_text('Dream', dream_prompt)
@ -48,7 +49,8 @@ class PngWriter:
metadata stored there, as a dict
'''
path = os.path.join(self.outdir,img_basename)
return retrieve_metadata(path)
all_metadata = retrieve_metadata(path)
return all_metadata['sd-metadata']
def retrieve_metadata(img_path):
'''
@ -56,6 +58,7 @@ def retrieve_metadata(img_path):
metadata stored there, as a dict
'''
im = Image.open(img_path)
md = im.text.get('sd-metadata',{})
return json.loads(md)
md = im.text.get('sd-metadata', '{}')
dream_prompt = im.text.get('Dream', '')
return {'sd-metadata': json.loads(md), 'Dream': dream_prompt}

View File

@ -13,7 +13,7 @@ filenames = sys.argv[1:]
for f in filenames:
try:
metadata = retrieve_metadata(f)
print(f'{f}:\n',json.dumps(metadata, indent=4))
print(f'{f}:\n',json.dumps(metadata['sd-metadata'], indent=4))
except FileNotFoundError:
sys.stderr.write(f'{f} not found\n')
continue