Implement Nova Farma S1 Max: Layered terrain, water mechanics, Y-sorting, and asset cleanup
This commit is contained in:
@@ -1,59 +1,59 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import sys
|
||||
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'
|
||||
def clean_pink_and_soften(input_path, output_path):
|
||||
print(f"Processing {input_path}...")
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
# Read image
|
||||
img = cv2.imread(input_path)
|
||||
img = cv2.imread(input_path, cv2.IMREAD_UNCHANGED)
|
||||
if img is None:
|
||||
print(f"Error: Could not read {input_path}")
|
||||
print("Failed to load image.")
|
||||
return
|
||||
|
||||
# Convert to RGBA
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
||||
# Check channels
|
||||
if img.shape[2] == 3:
|
||||
b, g, r = cv2.split(img)
|
||||
# Create alpha (255)
|
||||
a = np.ones_like(b) * 255
|
||||
img = cv2.merge((b, g, r, a))
|
||||
|
||||
# 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])
|
||||
# Targeted Pink Range in HSV
|
||||
hsv = cv2.cvtColor(img[:,:,0:3], cv2.COLOR_BGR2HSV)
|
||||
lower_pink = np.array([145, 50, 50])
|
||||
upper_pink = np.array([170, 255, 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)
|
||||
pink_mask = cv2.inRange(hsv, lower_pink, upper_pink)
|
||||
|
||||
# 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
|
||||
# 1. REMOVE PINK (Set alpha to 0)
|
||||
# Get current alpha
|
||||
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
|
||||
# Where pink_mask is 255, set alpha to 0
|
||||
# Also combine with existing alpha (if image was already transparent, keep it)
|
||||
new_alpha = cv2.bitwise_and(a, cv2.bitwise_not(pink_mask))
|
||||
|
||||
# Optional: Basic edge smoothing/despiking if needed, but for pixel/vector art simple cut is often better.
|
||||
# 2. EDGE SOFTENING (Alpha Blur 1px)
|
||||
# We blur the alpha channel slightly to soften jagged edges
|
||||
soft_alpha = cv2.GaussianBlur(new_alpha, (3, 3), 0)
|
||||
|
||||
# Save
|
||||
cv2.imwrite(output_path, img)
|
||||
print(f"Successfully saved transparent image to {output_path}")
|
||||
# Thresholding back to keep semi-transparency only on very edge?
|
||||
# Or just keep the blur. For "Vector Style", usually we want sharp, but
|
||||
# user asked for "zmehčaj robove (Alpha Blur: 1px)".
|
||||
# Gaussian (3,3) is roughly 1px radius.
|
||||
|
||||
# 3. COLOR BLEED REMOVAL (Halo effect)
|
||||
# Often pink pixels leave a pink fringe.
|
||||
# We can dilate the non-pink color into the transparent area?
|
||||
# Or simpler: desaturate or darken pixels on the edge of transparency.
|
||||
# For now, let's stick to the requested Blur.
|
||||
|
||||
final_img = cv2.merge((b, g, r, soft_alpha))
|
||||
|
||||
cv2.imwrite(output_path, final_img)
|
||||
print(f"Saved cleaned image to {output_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
remove_pink_bg()
|
||||
# Hardcoded path as per instructions: Vegetation/visoka_trava_v2.png
|
||||
target = 'assets/DEMO_FAZA1/Vegetation/visoka_trava_v2.png'
|
||||
clean_pink_and_soften(target, target)
|
||||
|
||||
Reference in New Issue
Block a user