11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+
14
15
import base64
15
16
import io
16
17
import warnings
17
18
18
- from PIL import Image
19
+ from PIL import Image , ImageOps
19
20
20
21
21
22
class SDAPIToDiffusersConverter :
@@ -30,7 +31,7 @@ class SDAPIToDiffusersConverter:
30
31
txt2img_arg_mapping = {
31
32
"steps" : "num_inference_steps" ,
32
33
"cfg_scale" : "guidance_scale" ,
33
- # "denoising_strength": "strength",
34
+ "denoising_strength" : "strength" ,
34
35
}
35
36
img2img_identical_args = {
36
37
"prompt" ,
@@ -42,9 +43,11 @@ class SDAPIToDiffusersConverter:
42
43
}
43
44
img2img_arg_mapping = {
44
45
"init_images" : "image" ,
46
+ "mask" : "mask_image" ,
45
47
"steps" : "num_inference_steps" ,
46
48
"cfg_scale" : "guidance_scale" ,
47
49
"denoising_strength" : "strength" ,
50
+ "inpaint_full_res_padding" : "padding_mask_crop" ,
48
51
}
49
52
50
53
@staticmethod
@@ -121,12 +124,38 @@ def _decode_b64_img(img_str: str) -> Image:
121
124
122
125
def img2img (self , ** kwargs ):
123
126
init_images = kwargs .pop ("init_images" , [])
124
- kwargs ["init_images" ] = [self ._decode_b64_img (i ) for i in init_images ]
127
+ kwargs ["init_images" ] = init_images = [
128
+ self ._decode_b64_img (i ) for i in init_images
129
+ ]
130
+ if len (init_images ) == 1 :
131
+ kwargs ["init_images" ] = init_images [0 ]
132
+ mask_image = kwargs .pop ("mask" , None )
133
+ if mask_image :
134
+ if kwargs .pop ("inpainting_mask_invert" ):
135
+ mask_image = ImageOps .invert (mask_image )
136
+
137
+ kwargs ["mask" ] = self ._decode_b64_img (mask_image )
138
+
139
+ # process inpaint_full_res and inpaint_full_res_padding
140
+ if kwargs .pop ("inpaint_full_res" , None ):
141
+ kwargs ["inpaint_full_res_padding" ] = kwargs .pop (
142
+ "inpaint_full_res_padding" , 0
143
+ )
144
+ else :
145
+ # inpaint_full_res_padding is turned `into padding_mask_crop`
146
+ # in diffusers, if padding_mask_crop is passed, it will do inpaint_full_res
147
+ # so if not inpaint_full_rs, we need to pop this option
148
+ kwargs .pop ("inpaint_full_res_padding" , None )
149
+
125
150
clip_skip = kwargs .get ("override_settings" , {}).get ("clip_skip" )
126
151
converted_kwargs = self ._check_kwargs ("img2img" , kwargs )
127
152
if clip_skip :
128
153
converted_kwargs ["clip_skip" ] = clip_skip
129
- result = self .image_to_image (response_format = "b64_json" , ** converted_kwargs ) # type: ignore
154
+
155
+ if not converted_kwargs .get ("mask_image" ):
156
+ result = self .image_to_image (response_format = "b64_json" , ** converted_kwargs ) # type: ignore
157
+ else :
158
+ result = self .inpainting (response_format = "b64_json" , ** converted_kwargs ) # type: ignore
130
159
131
160
# convert to SD API result
132
161
return {
0 commit comments