Freak-ppa commited on
Commit
eaae6bc
·
verified ·
1 Parent(s): 9c20447

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -3
app.py CHANGED
@@ -239,14 +239,22 @@ def merge_alpha(img, sigma=0.0):
239
  print(f"img.shape: {img.shape}")
240
 
241
  if C == 3:
 
242
  return img
243
  elif C == 4:
244
- rgb = img[:, :, :3]
245
- alpha = img[:, :, 3]
246
- result = 127 + (rgb.astype(np.float32) - 127 + sigma) * alpha[:, :, np.newaxis]
 
 
 
 
 
247
  return np.clip(result, 0, 255).astype(np.uint8)
248
  else:
249
  raise ValueError(f"Unexpected number of channels: {C}")
 
 
250
 
251
 
252
  @torch.inference_mode()
 
239
  print(f"img.shape: {img.shape}")
240
 
241
  if C == 3:
242
+ img, _ = run_rmbg(img)
243
  return img
244
  elif C == 4:
245
+ rgb = img[:, :, :3].astype(np.float32)
246
+ alpha = img[:, :, 3].astype(np.float32) / 255.0
247
+
248
+ result = rgb * alpha[:, :, np.newaxis] + 255 * (1 - alpha[:, :, np.newaxis])
249
+
250
+ if sigma != 0:
251
+ result += sigma * alpha[:, :, np.newaxis]
252
+
253
  return np.clip(result, 0, 255).astype(np.uint8)
254
  else:
255
  raise ValueError(f"Unexpected number of channels: {C}")
256
+ else:
257
+ raise ValueError(f"Unexpected number of channels: {C}")
258
 
259
 
260
  @torch.inference_mode()