fix sorting and sdxl workflow prompt text

This commit is contained in:
Karl Hudgell 2025-04-24 20:23:33 +01:00
parent 81140d720b
commit 9aea4e63fc
2 changed files with 10 additions and 5 deletions

View File

@ -41,11 +41,11 @@ def gallery() -> str:
images = []
for f in os.listdir(image_folder):
if f.lower().endswith(('png', 'jpg', 'jpeg', 'gif')):
path = os.path.join(image_folder, f)
prompt = get_prompt_from_png(path) # Youd define this function to read metadata
images.append({'filename': f, 'prompt': prompt})
path = os.path.join(image_folder, f) # Full path to the image
prompt = get_prompt_from_png(path) # Your method to extract the prompt
images.append({'filename': f, 'prompt': prompt, 'path': path}) # Add 'path' to the dictionary
images = sorted(images, key=lambda x: os.path.getmtime(x['path']), reverse=True)
return render_template("gallery.html", images=images)

7
lib.py
View File

@ -274,7 +274,12 @@ def create_image(prompt: str | None = None) -> None:
def get_prompt_from_png(path):
try:
with Image.open(path) as img:
meta = img.info.get("parameters").split("Negative")[0] # ComfyUI usually stores it here
try:
# Flux workflow
meta = img.info.get("parameters").split("Negative")[0]
except AttributeError:
# SDXL workflow
meta = json.loads(img.info["prompt"])['6']['inputs']['text']
return meta or ""
except Exception as e:
print(f"Error reading metadata from {path}: {e}")