import cv2 import numpy as np import os def remove_pink_bg(): input_path = '/Users/davidkotnik/.gemini/antigravity/brain/63340bd3-91a9-439d-b1d9-5692ce5adaea/visoka_trava_v2_pink_bg_1769436757738.png' output_path = 'assets/DEMO_FAZA1/Vegetation/visoka_trava_v2.png' # Ensure directory exists os.makedirs(os.path.dirname(output_path), exist_ok=True) # Read image img = cv2.imread(input_path) if img is None: print(f"Error: Could not read {input_path}") return # Convert to RGBA img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA) # Define Magenta range (B, G, R) - standard magenta is (255, 0, 255) in BGR # We'll allow a small tolerance to catch anti-aliasing edges sensitivity = 20 lower_magenta = np.array([255 - sensitivity, 0, 255 - sensitivity]) upper_magenta = np.array([255, sensitivity, 255]) # Create mask # Note: OpenCV reads as BGR, so Pink #FF00FF is (255, 0, 255) # Using simple color thresholding mask = cv2.inRange(img[:,:,:3], lower_magenta, upper_magenta) # Invert mask (we want to keep non-magenta) mask_inv = cv2.bitwise_not(mask) # Apply mask to alpha channel # Where mask is white (magenta pixels), alpha becomes 0 # But mask_inv is white for KEEP pixels. # So we want Alpha to be 0 where mask is 255. # Better approach: # 1. Split channels b, g, r, a = cv2.split(img) # 2. Update alpha channel using the inverted mask # If mask pixel is 255 (magenta), mask_inv is 0. We want alpha 0 there. # If mask pixel is 0 (not magenta), mask_inv is 255. We want alpha 255 there. # However, original alpha is 255 everywhere inside the image bounds. # So we can just take bitwise_and or just set alpha to mask_inv. img[:, :, 3] = mask_inv # Optional: Basic edge smoothing/despiking if needed, but for pixel/vector art simple cut is often better. # Save cv2.imwrite(output_path, img) print(f"Successfully saved transparent image to {output_path}") if __name__ == "__main__": remove_pink_bg()