2026-03-26 17:33:17 +08:00
|
|
|
"""Object erasing / inpainting using OpenCV."""
|
2026-03-22 04:31:49 +08:00
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
2026-03-23 01:38:19 +08:00
|
|
|
def emit_progress(percent, stage):
|
|
|
|
|
"""Emit structured progress to stderr for bridge.ts to capture."""
|
|
|
|
|
print(json.dumps({"progress": percent, "stage": stage}), file=sys.stderr, flush=True)
|
|
|
|
|
|
|
|
|
|
|
2026-03-22 04:31:49 +08:00
|
|
|
def main():
|
|
|
|
|
input_path = sys.argv[1]
|
|
|
|
|
mask_path = sys.argv[2]
|
|
|
|
|
output_path = sys.argv[3]
|
|
|
|
|
|
|
|
|
|
try:
|
2026-03-26 17:33:17 +08:00
|
|
|
emit_progress(10, "Preparing")
|
2026-03-22 04:31:49 +08:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
try:
|
2026-03-26 17:33:17 +08:00
|
|
|
import cv2
|
|
|
|
|
import numpy as np
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-03-26 17:33:17 +08:00
|
|
|
emit_progress(20, "Ready")
|
2026-03-23 01:38:19 +08:00
|
|
|
|
2026-03-22 04:31:49 +08:00
|
|
|
img = Image.open(input_path).convert("RGB")
|
|
|
|
|
mask = Image.open(mask_path).convert("L")
|
|
|
|
|
|
|
|
|
|
# Resize mask to match image if needed
|
2026-03-26 17:33:17 +08:00
|
|
|
emit_progress(30, "Analyzing mask")
|
2026-03-22 04:31:49 +08:00
|
|
|
if mask.size != img.size:
|
|
|
|
|
mask = mask.resize(img.size, Image.NEAREST)
|
|
|
|
|
|
2026-03-26 17:33:17 +08:00
|
|
|
img_array = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
2026-03-22 04:31:49 +08:00
|
|
|
mask_array = np.array(mask)
|
|
|
|
|
|
2026-03-26 17:33:17 +08:00
|
|
|
# Threshold mask to binary (ensure clean white/black)
|
|
|
|
|
_, mask_binary = cv2.threshold(mask_array, 127, 255, cv2.THRESH_BINARY)
|
|
|
|
|
|
|
|
|
|
# Inpaint radius scales with image size for better results
|
|
|
|
|
inpaint_radius = max(3, min(img_array.shape[0], img_array.shape[1]) // 200)
|
|
|
|
|
|
|
|
|
|
emit_progress(50, "Erasing")
|
|
|
|
|
result = cv2.inpaint(img_array, mask_binary, inpaint_radius, cv2.INPAINT_TELEA)
|
|
|
|
|
|
|
|
|
|
emit_progress(90, "Saving")
|
|
|
|
|
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
|
|
|
|
Image.fromarray(result_rgb).save(output_path)
|
|
|
|
|
|
|
|
|
|
print(json.dumps({"success": True, "method": "opencv-telea"}))
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-03-22 19:28:57 +08:00
|
|
|
except ImportError:
|
|
|
|
|
print(
|
|
|
|
|
json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"success": False,
|
2026-03-26 17:33:17 +08:00
|
|
|
"error": "Object eraser requires OpenCV. Install with: pip install opencv-python-headless",
|
2026-03-22 19:28:57 +08:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
sys.exit(1)
|
2026-03-22 04:31:49 +08:00
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
print(
|
|
|
|
|
json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"success": False,
|
|
|
|
|
"error": "Pillow is not installed. Install with: pip install Pillow",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(json.dumps({"success": False, "error": str(e)}))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|