Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -195,7 +195,7 @@ def process_image_mask(image_mask_dict):
|
|
195 |
background = image_mask_dict.get("background")
|
196 |
layers = image_mask_dict.get("layers")
|
197 |
|
198 |
-
if background is None
|
199 |
return None
|
200 |
|
201 |
# ---- 1) Drop alpha from background ----
|
@@ -206,32 +206,36 @@ def process_image_mask(image_mask_dict):
|
|
206 |
if img_array.ndim == 3 and img_array.shape[2] == 4:
|
207 |
img_array = img_array[..., :3]
|
208 |
|
209 |
-
# ---- 2)
|
210 |
-
|
211 |
-
|
212 |
-
layer
|
213 |
-
|
214 |
-
|
215 |
-
mask_array
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
|
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
|
234 |
-
|
|
|
|
|
|
|
235 |
|
236 |
# 最も近い解像度を見つける関数
|
237 |
def find_nearest_resolution(width, height):
|
|
|
195 |
background = image_mask_dict.get("background")
|
196 |
layers = image_mask_dict.get("layers")
|
197 |
|
198 |
+
if background is None:
|
199 |
return None
|
200 |
|
201 |
# ---- 1) Drop alpha from background ----
|
|
|
206 |
if img_array.ndim == 3 and img_array.shape[2] == 4:
|
207 |
img_array = img_array[..., :3]
|
208 |
|
209 |
+
# ---- 2) マスクがある場合のみマスク処理 ----
|
210 |
+
if layers and len(layers) > 0:
|
211 |
+
layer = layers[0]
|
212 |
+
if isinstance(layer, Image.Image) and layer.mode == "RGBA":
|
213 |
+
layer = layer.convert("RGB")
|
214 |
+
mask_array = np.array(layer)
|
215 |
+
if mask_array.ndim == 3 and mask_array.shape[2] == 4:
|
216 |
+
mask_array = mask_array[..., :3]
|
217 |
+
|
218 |
+
# convert to gray + binary
|
219 |
+
if mask_array.ndim == 3:
|
220 |
+
mask_gray = cv2.cvtColor(mask_array, cv2.COLOR_RGB2GRAY)
|
221 |
+
else:
|
222 |
+
mask_gray = mask_array
|
223 |
+
_, binary_mask = cv2.threshold(mask_gray, 1, 255, cv2.THRESH_BINARY)
|
224 |
|
225 |
+
# 市松模様合成ロジック
|
226 |
+
total_pixels = img_array.shape[0] * img_array.shape[1]
|
227 |
+
cell_size = max(int(np.sqrt(total_pixels) / 20), 10)
|
228 |
+
checkerboard = create_checkerboard(img_array.shape[1], img_array.shape[0], cell_size)
|
229 |
|
230 |
+
result = img_array.copy()
|
231 |
+
binary_mask_3ch = np.stack([binary_mask]*3, axis=2) // 255
|
232 |
+
for c in range(3):
|
233 |
+
result[..., c] = result[..., c] * (1 - binary_mask_3ch[..., c]) + checkerboard[..., c] * binary_mask_3ch[..., c]
|
234 |
|
235 |
+
return result.astype(np.uint8)
|
236 |
+
else:
|
237 |
+
# マスクがない場合は元の画像をそのまま返す
|
238 |
+
return img_array
|
239 |
|
240 |
# 最も近い解像度を見つける関数
|
241 |
def find_nearest_resolution(width, height):
|