Skip to content

Commit 506d529

Browse files
committed
rework #5012 to also work for pictures dragged into the prompt and also add Clip skip + ENSD to parameters
1 parent 488f831 commit 506d529

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

modules/extras.py

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22
import math
33
import os
4+
import sys
5+
import traceback
46

57
import numpy as np
68
from PIL import Image
@@ -12,7 +14,7 @@
1214
from functools import partial
1315
from dataclasses import dataclass
1416

15-
from modules import processing, shared, images, devices, sd_models
17+
from modules import processing, shared, images, devices, sd_models, sd_samplers
1618
from modules.shared import opts
1719
import modules.gfpgan_model
1820
from modules.ui import plaintext_to_html
@@ -22,7 +24,6 @@
2224
import gradio as gr
2325
import safetensors.torch
2426

25-
2627
class LruCache(OrderedDict):
2728
@dataclass(frozen=True)
2829
class Key:
@@ -214,39 +215,8 @@ def run_pnginfo(image):
214215
if image is None:
215216
return '', '', ''
216217

217-
items = image.info
218-
geninfo = ''
219-
220-
if "exif" in image.info:
221-
exif = piexif.load(image.info["exif"])
222-
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
223-
try:
224-
exif_comment = piexif.helper.UserComment.load(exif_comment)
225-
except ValueError:
226-
exif_comment = exif_comment.decode('utf8', errors="ignore")
227-
228-
items['exif comment'] = exif_comment
229-
geninfo = exif_comment
230-
231-
for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
232-
'loop', 'background', 'timestamp', 'duration']:
233-
items.pop(field, None)
234-
235-
geninfo = items.get('parameters', geninfo)
236-
237-
# nai prompt
238-
if "Software" in items.keys() and items["Software"] == "NovelAI":
239-
import json
240-
json_info = json.loads(items["Comment"])
241-
geninfo = f'{items["Description"]}\r\nNegative prompt: {json_info["uc"]}\r\n'
242-
sampler = "Euler a"
243-
if json_info["sampler"] == "k_euler_ancestral":
244-
sampler = "Euler a"
245-
elif json_info["sampler"] == "k_euler":
246-
sampler = "Euler"
247-
model_hash = '925997e9' # assuming this is the correct model hash
248-
# not sure with noise and strength parameter
249-
geninfo += f'Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Model hash: {model_hash}' # , Denoising strength: {json_info["noise"]}'
218+
geninfo, items = images.read_info_from_image(image)
219+
items = {**{'parameters': geninfo}, **items}
250220

251221
info = ''
252222
for key, text in items.items():

modules/generation_parameters_copypaste.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def integrate_settings_paste_fields(component_dict):
7575
'CLIP_stop_at_last_layers': 'Clip skip',
7676
'inpainting_mask_weight': 'Conditional mask weight',
7777
'sd_model_checkpoint': 'Model hash',
78+
'eta_noise_seed_delta': 'ENSD',
7879
}
7980
settings_paste_fields = [
8081
(component_dict[k], lambda d, k=k, v=v: ui.apply_setting(k, d.get(v, None)))

modules/images.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from PIL import Image, ImageFont, ImageDraw, PngImagePlugin
1616
from fonts.ttf import Roboto
1717
import string
18+
import json
1819

1920
from modules import sd_samplers, shared, script_callbacks
2021
from modules.shared import opts, cmd_opts
@@ -553,10 +554,45 @@ def exif_bytes():
553554
return fullfn, txt_fullfn
554555

555556

557+
def read_info_from_image(image):
558+
items = image.info or {}
559+
560+
geninfo = items.pop('parameters', None)
561+
562+
if "exif" in items:
563+
exif = piexif.load(items["exif"])
564+
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
565+
try:
566+
exif_comment = piexif.helper.UserComment.load(exif_comment)
567+
except ValueError:
568+
exif_comment = exif_comment.decode('utf8', errors="ignore")
569+
570+
items['exif comment'] = exif_comment
571+
geninfo = exif_comment
572+
573+
for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
574+
'loop', 'background', 'timestamp', 'duration']:
575+
items.pop(field, None)
576+
577+
if items.get("Software", None) == "NovelAI":
578+
try:
579+
json_info = json.loads(items["Comment"])
580+
sampler = sd_samplers.samplers_map.get(json_info["sampler"], "Euler a")
581+
582+
geninfo = f"""{items["Description"]}
583+
Negative prompt: {json_info["uc"]}
584+
Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}, Seed: {json_info["seed"]}, Size: {image.width}x{image.height}, Clip skip: 2, ENSD: 31337"""
585+
except Exception:
586+
print(f"Error parsing NovelAI iamge generation parameters:", file=sys.stderr)
587+
print(traceback.format_exc(), file=sys.stderr)
588+
589+
return geninfo, items
590+
591+
556592
def image_data(data):
557593
try:
558594
image = Image.open(io.BytesIO(data))
559-
textinfo = image.text["parameters"]
595+
textinfo, _ = read_info_from_image(image)
560596
return textinfo, None
561597
except Exception:
562598
pass

modules/sd_samplers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
SamplerData = namedtuple('SamplerData', ['name', 'constructor', 'aliases', 'options'])
1919

2020
samplers_k_diffusion = [
21-
('Euler a', 'sample_euler_ancestral', ['k_euler_a'], {}),
21+
('Euler a', 'sample_euler_ancestral', ['k_euler_a', 'k_euler_ancestral'], {}),
2222
('Euler', 'sample_euler', ['k_euler'], {}),
2323
('LMS', 'sample_lms', ['k_lms'], {}),
2424
('Heun', 'sample_heun', ['k_heun'], {}),

0 commit comments

Comments
 (0)