73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
import cv2
|
|
import numpy as np
|
|
import os
|
|
import shutil
|
|
|
|
def clean_pipe_stream():
|
|
# Source is the uploaded JPG
|
|
src_path = '/Users/davidkotnik/.gemini/antigravity/brain/07019d04-a214-43ab-9565-86f4e8f17e5b/uploaded_media_1769607894587.jpg'
|
|
|
|
print(f"Loading {src_path}")
|
|
img = cv2.imread(src_path)
|
|
if img is None:
|
|
print("Failed to load image")
|
|
return
|
|
|
|
# Add Alpha Channel
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
|
|
|
# Advanced Background Removal using FloodFill from corners
|
|
# The background is likely uniform-ish but with JPG noise.
|
|
# We create a mask initialized with zeros (2 pixels larger than image)
|
|
h, w = img.shape[:2]
|
|
mask = np.zeros((h+2, w+2), np.uint8)
|
|
|
|
# Floodfill from (0,0) with a tolerance
|
|
# LoDiff and UpDiff allow for noise (e.g. +/- 10 in color value)
|
|
flood_flags = 4 | (255 << 8) | cv2.FLOODFILL_MASK_ONLY | cv2.FLOODFILL_FIXED_RANGE
|
|
|
|
# We need to know the 'seed' color to guess tolerance.
|
|
seed_color = img[0,0][:3].tolist()
|
|
print(f"Seed Color at 0,0: {seed_color}")
|
|
|
|
# Run floodFill from top-left
|
|
# Using a generous tolerance because the background looks like a solid color render but might have gradients/noise
|
|
cv2.floodFill(img, mask, (0,0), (0,0,0,0), (10,10,10), (10,10,10), flood_flags)
|
|
|
|
# Also try top-right, bottom-left, bottom-right just in case
|
|
cv2.floodFill(img, mask, (w-1, 0), (0,0,0,0), (10,10,10), (10,10,10), flood_flags)
|
|
cv2.floodFill(img, mask, (0, h-1), (0,0,0,0), (10,10,10), (10,10,10), flood_flags)
|
|
cv2.floodFill(img, mask, (w-1, h-1), (0,0,0,0), (10,10,10), (10,10,10), flood_flags)
|
|
|
|
# The 'mask' now contains 255 where the background was found.
|
|
# Note: floodFill mask is 2 pixels bigger. Crop it back.
|
|
mask = mask[1:-1, 1:-1]
|
|
|
|
# Set alpha to 0 where mask is set
|
|
img[mask > 0, 3] = 0
|
|
|
|
# Crop the image to the actual content (non-transparent parts)
|
|
# Find bounding box of alpha > 0
|
|
coords = cv2.findNonZero(img[:,:,3]) # Points where alpha is NOT 0
|
|
if coords is not None:
|
|
x, y, cw, ch = cv2.boundingRect(coords)
|
|
print(f"Cropping to: {x},{y} {cw}x{ch}")
|
|
img = img[y:y+ch, x:x+cw]
|
|
|
|
# Destinations
|
|
targets = [
|
|
'/Users/davidkotnik/repos/novafarma/main/assets',
|
|
'/Users/davidkotnik/repos/novafarma/assets/DEMO_FAZA1/Environment'
|
|
]
|
|
|
|
dest_name = 'stream_pipe.png'
|
|
|
|
for t in targets:
|
|
if not os.path.exists(t): os.makedirs(t, exist_ok=True)
|
|
final_path = os.path.join(t, dest_name)
|
|
cv2.imwrite(final_path, img)
|
|
print(f"Saved cleaned asset to {final_path}")
|
|
|
|
if __name__ == "__main__":
|
|
clean_pipe_stream()
|