Spaces:
Runtime error
Runtime error
root
commited on
Commit
•
148834c
1
Parent(s):
34aa62f
Front-Back connect test
Browse files
app.py
CHANGED
@@ -126,14 +126,32 @@ def call_JS(sd_method, **kwargs):
|
|
126 |
param_str = json.dumps(kwargs)
|
127 |
return f"async (...x) => {{ return await SD.{sd_method}({{ x, ...{param_str} }}) ?? []; }}"
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def img2img(*args):
|
130 |
|
131 |
# 处理image
|
132 |
for i, item in enumerate(args):
|
133 |
# print(type(item))
|
134 |
if type(item) == dict:
|
135 |
-
args[i]['image'] = item['image']
|
136 |
-
args[i]['mask'] = item['mask']
|
137 |
else:
|
138 |
print(i,type(item))
|
139 |
print(item)
|
@@ -248,6 +266,7 @@ if __name__ == "__main__":
|
|
248 |
source="upload",
|
249 |
interactive=True,
|
250 |
tool="sketch",
|
|
|
251 |
elem_id="img2img_mask",
|
252 |
image_mode="RGBA"
|
253 |
)
|
@@ -256,6 +275,7 @@ if __name__ == "__main__":
|
|
256 |
source="upload",
|
257 |
interactive=True,
|
258 |
tool="select",
|
|
|
259 |
visible=False,
|
260 |
image_mode="RGBA",
|
261 |
elem_id="img2img_editor"
|
|
|
126 |
param_str = json.dumps(kwargs)
|
127 |
return f"async (...x) => {{ return await SD.{sd_method}({{ x, ...{param_str} }}) ?? []; }}"
|
128 |
|
129 |
+
def encode_pil_to_base64(pil_image):
|
130 |
+
with BytesIO() as output_bytes:
|
131 |
+
|
132 |
+
# Copy any text-only metadata
|
133 |
+
use_metadata = False
|
134 |
+
metadata = PngImagePlugin.PngInfo()
|
135 |
+
for key, value in pil_image.info.items():
|
136 |
+
if isinstance(key, str) and isinstance(value, str):
|
137 |
+
metadata.add_text(key, value)
|
138 |
+
use_metadata = True
|
139 |
+
|
140 |
+
pil_image.save(
|
141 |
+
output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
|
142 |
+
)
|
143 |
+
bytes_data = output_bytes.getvalue()
|
144 |
+
base64_str = str(base64.b64encode(bytes_data), "utf-8")
|
145 |
+
return "data:image/png;base64," + base64_str
|
146 |
+
|
147 |
def img2img(*args):
|
148 |
|
149 |
# 处理image
|
150 |
for i, item in enumerate(args):
|
151 |
# print(type(item))
|
152 |
if type(item) == dict:
|
153 |
+
args[i]['image'] = encode_pil_to_base64(item['image'])
|
154 |
+
args[i]['mask'] = encode_pil_to_base64(item['mask'])
|
155 |
else:
|
156 |
print(i,type(item))
|
157 |
print(item)
|
|
|
266 |
source="upload",
|
267 |
interactive=True,
|
268 |
tool="sketch",
|
269 |
+
type='pil',
|
270 |
elem_id="img2img_mask",
|
271 |
image_mode="RGBA"
|
272 |
)
|
|
|
275 |
source="upload",
|
276 |
interactive=True,
|
277 |
tool="select",
|
278 |
+
type='pil',
|
279 |
visible=False,
|
280 |
image_mode="RGBA",
|
281 |
elem_id="img2img_editor"
|