@@ -117,12 +117,27 @@ def ui(self, is_img2img):
117
117
if not is_img2img :
118
118
return None
119
119
120
- max_size = gr .Slider (label = "Masking preview size" , minimum = 512 , maximum = 2048 , step = 8 , value = 1024 )
121
- ask_on_each_run = gr .Checkbox (label = 'Draw new mask on every run' , value = False )
120
+ initialSize = 1024
122
121
123
- return [max_size ,ask_on_each_run ]
122
+ try :
123
+ import tkinter as tk
124
+ root = tk .Tk ()
125
+ screen_width = int (root .winfo_screenwidth ())
126
+ screen_height = int (root .winfo_screenheight ())
127
+ print (screen_width ,screen_height )
128
+ initialSize = min (screen_width ,screen_height )- 50
129
+ print (initialSize )
130
+ except Exception as e :
131
+ print (e )
124
132
125
- def run (self , p , max_size , ask_on_each_run ):
133
+ max_size = gr .Slider (label = "Masking preview size" , minimum = 512 , maximum = initialSize * 2 , step = 8 , value = initialSize )
134
+ with gr .Row ():
135
+ ask_on_each_run = gr .Checkbox (label = 'Draw new mask on every run' , value = False )
136
+ non_contigious_split = gr .Checkbox (label = 'Process non-contigious masks separately' , value = False )
137
+
138
+ return [max_size ,ask_on_each_run ,non_contigious_split ]
139
+
140
+ def run (self , p , max_size , ask_on_each_run , non_contigious_split ):
126
141
127
142
if not hasattr (self ,'lastImg' ):
128
143
self .lastImg = None
@@ -145,7 +160,36 @@ def run(self, p, max_size, ask_on_each_run):
145
160
elif hasattr (self ,'lastMask' ) and self .lastMask is not None :
146
161
p .image_mask = self .lastMask .copy ()
147
162
148
- proc = process_images (p )
149
- images = proc .images
163
+ if non_contigious_split :
164
+ maskImgArr = np .array (p .image_mask )
165
+ ret , markers = cv2 .connectedComponents (maskImgArr )
166
+ markerCount = markers .max ()
167
+
168
+ if markerCount > 1 :
169
+ tempimages = []
170
+ tempMasks = []
171
+ for maski in range (1 ,markerCount + 1 ):
172
+ print ('maski' ,maski )
173
+ maskSection = np .zeros_like (maskImgArr )
174
+ maskSection [markers == maski ] = 255
175
+ p .image_mask = Image .fromarray ( maskSection .copy () )
176
+ proc = process_images (p )
177
+ images = proc .images
178
+ tempimages .append (np .array (images [0 ]))
179
+ tempMasks .append (np .array (maskSection .copy ()))
180
+
181
+ finalImage = tempimages [0 ].copy ()
182
+
183
+ for outimg ,outmask in zip (tempimages ,tempMasks ):
184
+ finalImage [outmask == 255 ] = outimg [outmask == 255 ]
185
+ images = [finalImage ]
186
+
187
+
188
+ else :
189
+ proc = process_images (p )
190
+ images = proc .images
191
+ else :
192
+ proc = process_images (p )
193
+ images = proc .images
150
194
151
195
return Processed (p , images , p .seed , "" )
0 commit comments