10
10
from modules .processing import StableDiffusionProcessingTxt2Img , StableDiffusionProcessingImg2Img , process_images
11
11
from modules .sd_samplers import all_samplers
12
12
from modules .extras import run_extras , run_pnginfo
13
+ from PIL import PngImagePlugin
13
14
from modules .sd_models import checkpoints_list
14
15
from modules .realesrgan_model import get_realesrgan_models
15
16
from typing import List
@@ -34,9 +35,21 @@ def setUpscalers(req: dict):
34
35
35
36
36
37
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 )
40
53
41
54
42
55
class Api :
@@ -205,7 +218,7 @@ def interruptapi(self):
205
218
shared .state .interrupt ()
206
219
207
220
return {}
208
-
221
+
209
222
def get_config (self ):
210
223
options = {}
211
224
for key in shared .opts .data .keys ():
@@ -214,9 +227,9 @@ def get_config(self):
214
227
options .update ({key : shared .opts .data .get (key , shared .opts .data_labels .get (key ).default )})
215
228
else :
216
229
options .update ({key : shared .opts .data .get (key , None )})
217
-
230
+
218
231
return options
219
-
232
+
220
233
def set_config (self , req : OptionsModel ):
221
234
# currently req has all options fields even if you send a dict like { "send_seed": false }, which means it will
222
235
# overwrite all options with default values.
@@ -237,13 +250,13 @@ def get_samplers(self):
237
250
238
251
def get_upscalers (self ):
239
252
upscalers = []
240
-
253
+
241
254
for upscaler in shared .sd_upscalers :
242
255
u = upscaler .scaler
243
256
upscalers .append ({"name" :u .name , "model_name" :u .model_name , "model_path" :u .model_path , "model_url" :u .model_url })
244
-
257
+
245
258
return upscalers
246
-
259
+
247
260
def get_sd_models (self ):
248
261
return [{"title" :x .title , "model_name" :x .model_name , "hash" :x .hash , "filename" : x .filename , "config" : x .config } for x in checkpoints_list .values ()]
249
262
@@ -255,11 +268,11 @@ def get_face_restorers(self):
255
268
256
269
def get_realesrgan_models (self ):
257
270
return [{"name" :x .name ,"path" :x .data_path , "scale" :x .scale } for x in get_realesrgan_models (None )]
258
-
271
+
259
272
def get_promp_styles (self ):
260
273
styleList = []
261
274
for k in shared .prompt_styles .styles :
262
- style = shared .prompt_styles .styles [k ]
275
+ style = shared .prompt_styles .styles [k ]
263
276
styleList .append ({"name" :style [0 ], "prompt" : style [1 ], "negative_prompr" : style [2 ]})
264
277
265
278
return styleList
0 commit comments