Skip to content

Commit a546e2a

Browse files
Merge pull request #4173 from evshiron/fix/encode-pnginfo
add back png info in image api
2 parents 62e3d71 + b6cfaaa commit a546e2a

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

modules/api/api.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
1111
from modules.sd_samplers import all_samplers
1212
from modules.extras import run_extras, run_pnginfo
13+
from PIL import PngImagePlugin
1314
from modules.sd_models import checkpoints_list
1415
from modules.realesrgan_model import get_realesrgan_models
1516
from typing import List
@@ -34,9 +35,21 @@ def setUpscalers(req: dict):
3435

3536

3637
def encode_pil_to_base64(image):
37-
buffer = io.BytesIO()
38-
image.save(buffer, format="png")
39-
return base64.b64encode(buffer.getvalue())
38+
with io.BytesIO() as output_bytes:
39+
40+
# Copy any text-only metadata
41+
use_metadata = False
42+
metadata = PngImagePlugin.PngInfo()
43+
for key, value in image.info.items():
44+
if isinstance(key, str) and isinstance(value, str):
45+
metadata.add_text(key, value)
46+
use_metadata = True
47+
48+
image.save(
49+
output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
50+
)
51+
bytes_data = output_bytes.getvalue()
52+
return base64.b64encode(bytes_data)
4053

4154

4255
class Api:
@@ -205,7 +218,7 @@ def interruptapi(self):
205218
shared.state.interrupt()
206219

207220
return {}
208-
221+
209222
def get_config(self):
210223
options = {}
211224
for key in shared.opts.data.keys():
@@ -214,9 +227,9 @@ def get_config(self):
214227
options.update({key: shared.opts.data.get(key, shared.opts.data_labels.get(key).default)})
215228
else:
216229
options.update({key: shared.opts.data.get(key, None)})
217-
230+
218231
return options
219-
232+
220233
def set_config(self, req: OptionsModel):
221234
# currently req has all options fields even if you send a dict like { "send_seed": false }, which means it will
222235
# overwrite all options with default values.
@@ -237,13 +250,13 @@ def get_samplers(self):
237250

238251
def get_upscalers(self):
239252
upscalers = []
240-
253+
241254
for upscaler in shared.sd_upscalers:
242255
u = upscaler.scaler
243256
upscalers.append({"name":u.name, "model_name":u.model_name, "model_path":u.model_path, "model_url":u.model_url})
244-
257+
245258
return upscalers
246-
259+
247260
def get_sd_models(self):
248261
return [{"title":x.title, "model_name":x.model_name, "hash":x.hash, "filename": x.filename, "config": x.config} for x in checkpoints_list.values()]
249262

@@ -255,11 +268,11 @@ def get_face_restorers(self):
255268

256269
def get_realesrgan_models(self):
257270
return [{"name":x.name,"path":x.data_path, "scale":x.scale} for x in get_realesrgan_models(None)]
258-
271+
259272
def get_promp_styles(self):
260273
styleList = []
261274
for k in shared.prompt_styles.styles:
262-
style = shared.prompt_styles.styles[k]
275+
style = shared.prompt_styles.styles[k]
263276
styleList.append({"name":style[0], "prompt": style[1], "negative_prompr": style[2]})
264277

265278
return styleList

0 commit comments

Comments
 (0)