4
4
import uvicorn
5
5
from threading import Lock
6
6
from io import BytesIO
7
- from gradio .processing_utils import encode_pil_to_base64 , decode_base64_to_file , decode_base64_to_image
7
+ from gradio .processing_utils import decode_base64_to_file
8
8
from fastapi import APIRouter , Depends , FastAPI , HTTPException
9
9
from fastapi .security import HTTPBasic , HTTPBasicCredentials
10
10
from secrets import compare_digest
@@ -41,6 +41,10 @@ def setUpscalers(req: dict):
41
41
reqDict .pop ('upscaler_2' )
42
42
return reqDict
43
43
44
+ def decode_base64_to_image (encoding ):
45
+ if encoding .startswith ("data:image/" ):
46
+ encoding = encoding .split (";" )[1 ].split ("," )[1 ]
47
+ return Image .open (BytesIO (base64 .b64decode (encoding )))
44
48
45
49
def encode_pil_to_base64 (image ):
46
50
with io .BytesIO () as output_bytes :
@@ -134,10 +138,7 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
134
138
135
139
mask = img2imgreq .mask
136
140
if mask :
137
- if mask .startswith ("data:image/" ):
138
- mask = decode_base64_to_image (mask )
139
- else :
140
- mask = Image .open (BytesIO (base64 .b64decode (mask )))
141
+ mask = decode_base64_to_image (mask )
141
142
142
143
populate = img2imgreq .copy (update = { # Override __init__ params
143
144
"sd_model" : shared .sd_model ,
@@ -151,10 +152,7 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
151
152
152
153
imgs = []
153
154
for img in init_images :
154
- if img .startswith ("data:image/" ):
155
- img = decode_base64_to_image (img )
156
- else :
157
- img = Image .open (BytesIO (base64 .b64decode (img )))
155
+ img = decode_base64_to_image (img )
158
156
imgs = [img ] * p .batch_size
159
157
160
158
p .init_images = imgs
0 commit comments