tori29umai commited on
Commit
6f995fe
·
verified ·
1 Parent(s): ab003b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
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 or not layers:
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) Load mask layer and binarize ----
210
- layer = layers[0]
211
- if isinstance(layer, Image.Image) and layer.mode == "RGBA":
212
- layer = layer.convert("RGB")
213
- mask_array = np.array(layer)
214
- if mask_array.ndim == 3 and mask_array.shape[2] == 4:
215
- mask_array = mask_array[..., :3]
216
-
217
- # convert to gray + binary
218
- if mask_array.ndim == 3:
219
- mask_gray = cv2.cvtColor(mask_array, cv2.COLOR_RGB2GRAY)
220
- else:
221
- mask_gray = mask_array
222
- _, binary_mask = cv2.threshold(mask_gray, 1, 255, cv2.THRESH_BINARY)
 
223
 
224
- # 市松模様合成ロジック
225
- total_pixels = img_array.shape[0] * img_array.shape[1]
226
- cell_size = max(int(np.sqrt(total_pixels) / 20), 10)
227
- checkerboard = create_checkerboard(img_array.shape[1], img_array.shape[0], cell_size)
228
 
229
- result = img_array.copy()
230
- binary_mask_3ch = np.stack([binary_mask]*3, axis=2) // 255
231
- for c in range(3):
232
- result[..., c] = result[..., c] * (1 - binary_mask_3ch[..., c]) + checkerboard[..., c] * binary_mask_3ch[..., c]
233
 
234
- return result.astype(np.uint8)
 
 
 
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):