LogicGoInfotechSpaces commited on
Commit
62974a7
·
1 Parent(s): d670035

fix: correct binary mask encoding - pink pixels get alpha=0 so 255-alpha becomes white (255) for removal

Browse files
Files changed (1) hide show
  1. api/main.py +28 -19
api/main.py CHANGED
@@ -329,21 +329,25 @@ def inpaint_multipart(
329
  Image.fromarray(result).save(result_path, "PNG", optimize=False, compress_level=1)
330
  return {"result": result_name, "error": "pink/magenta paint detection failed - very few pixels detected"}
331
 
332
- # Create black/white mask: white = remove (pink areas), black = keep (everything else)
 
 
 
 
333
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
334
- mask_rgba[:, :, 0] = binmask # R: white where pink detected
335
- mask_rgba[:, :, 1] = binmask # G: white where pink detected
336
- mask_rgba[:, :, 2] = binmask # B: white where pink detected
337
- mask_rgba[:, :, 3] = 255 # Alpha: fully opaque
 
338
 
339
- log.info("Successfully created black/white mask: %d white pixels (to remove), %d black pixels (to keep)",
340
  nonzero, binmask.shape[0] * binmask.shape[1] - nonzero)
341
  else:
342
  mask_rgba = _load_rgba_mask_from_image(m)
343
 
344
- # When mask_is_painted=true, we create white=remove masks, so invert_mask should be False
345
- # (white pixels should stay white to indicate removal)
346
- actual_invert = invert_mask if not mask_is_painted else False
347
  log.info("Using invert_mask=%s (mask_is_painted=%s)", actual_invert, mask_is_painted)
348
 
349
  result = process_inpaint(np.array(img), mask_rgba, invert_mask=actual_invert)
@@ -423,20 +427,25 @@ def remove_pink_segments(
423
  "error": "No pink/magenta segments detected. Please paint areas to remove with magenta/pink color (RGB 255,0,255)."
424
  }
425
 
426
- # Create mask:
427
- # - White = pink areas (remove)
428
- # - Black = everything else (keep) - automatically, no black painting needed
 
 
429
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
430
- mask_rgba[:, :, 0] = binmask # R: white where pink detected
431
- mask_rgba[:, :, 1] = binmask # G: white where pink detected
432
- mask_rgba[:, :, 2] = binmask # B: white where pink detected
433
- mask_rgba[:, :, 3] = 255 # Alpha: fully opaque
 
 
 
434
 
435
  total_pixels = binmask.shape[0] * binmask.shape[1]
436
- log.info(f"Created mask: {nonzero} white pixels (remove/pink), {total_pixels - nonzero} black pixels (keep - everything else automatically)")
437
 
438
- # Process with invert_mask=False because white pixels = remove (standard)
439
- result = process_inpaint(np.array(img), mask_rgba, invert_mask=False)
440
  result_name = f"output_{uuid.uuid4().hex}.png"
441
  result_path = os.path.join(OUTPUT_DIR, result_name)
442
  Image.fromarray(result).save(result_path, "PNG", optimize=False, compress_level=1)
 
329
  Image.fromarray(result).save(result_path, "PNG", optimize=False, compress_level=1)
330
  return {"result": result_name, "error": "pink/magenta paint detection failed - very few pixels detected"}
331
 
332
+ # Create binary mask: Pink pixels white (255), Everything else black (0)
333
+ # Encode in RGBA format for process_inpaint
334
+ # process_inpaint does: mask = 255 - mask[:,:,3]
335
+ # So: alpha=0 (transparent/pink) → becomes 255 (white/remove)
336
+ # alpha=255 (opaque/keep) → becomes 0 (black/keep)
337
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
338
+ mask_rgba[:, :, 0] = binmask # R: white where pink (for visualization)
339
+ mask_rgba[:, :, 1] = binmask # G: white where pink
340
+ mask_rgba[:, :, 2] = binmask # B: white where pink
341
+ # Alpha: invert so pink areas get alpha=0 will become white after 255-alpha
342
+ mask_rgba[:, :, 3] = 255 - binmask
343
 
344
+ log.info("Successfully created binary mask: %d pink pixels → white (255), %d pixels → black (0)",
345
  nonzero, binmask.shape[0] * binmask.shape[1] - nonzero)
346
  else:
347
  mask_rgba = _load_rgba_mask_from_image(m)
348
 
349
+ # When mask_is_painted=true, we encode pink as alpha=0, so process_inpaint's default invert_mask=True works correctly
350
+ actual_invert = invert_mask # Use default True for painted masks
 
351
  log.info("Using invert_mask=%s (mask_is_painted=%s)", actual_invert, mask_is_painted)
352
 
353
  result = process_inpaint(np.array(img), mask_rgba, invert_mask=actual_invert)
 
427
  "error": "No pink/magenta segments detected. Please paint areas to remove with magenta/pink color (RGB 255,0,255)."
428
  }
429
 
430
+ # Create binary mask: Pink pixels → white (255), Everything else → black (0)
431
+ # But we need to encode it in RGBA format that process_inpaint expects
432
+ # process_inpaint does: mask = 255 - mask[:,:,3]
433
+ # So: alpha=0 (transparent/pink) → becomes 255 (white/remove)
434
+ # alpha=255 (opaque/keep) → becomes 0 (black/keep)
435
  mask_rgba = np.zeros((binmask.shape[0], binmask.shape[1], 4), dtype=np.uint8)
436
+ # RGB channels don't matter for process_inpaint, but set them to white where pink for visualization
437
+ mask_rgba[:, :, 0] = binmask # R: white where pink
438
+ mask_rgba[:, :, 1] = binmask # G: white where pink
439
+ mask_rgba[:, :, 2] = binmask # B: white where pink
440
+ # Alpha: 0 (transparent) where pink → will become white after 255-alpha
441
+ # 255 (opaque) everywhere else → will become black after 255-alpha
442
+ mask_rgba[:, :, 3] = 255 - binmask # Invert: pink areas get alpha=0, rest get alpha=255
443
 
444
  total_pixels = binmask.shape[0] * binmask.shape[1]
445
+ log.info(f"Created binary mask: {nonzero} pink pixels → white (255) in final mask, {total_pixels - nonzero} black (0) - everything else automatically")
446
 
447
+ # Process with invert_mask=True (default) because process_inpaint expects alpha=0 for removal
448
+ result = process_inpaint(np.array(img), mask_rgba, invert_mask=True)
449
  result_name = f"output_{uuid.uuid4().hex}.png"
450
  result_path = os.path.join(OUTPUT_DIR, result_name)
451
  Image.fromarray(result).save(result_path, "PNG", optimize=False, compress_level=1)