Spaces:
Running
Running
File size: 1,520 Bytes
2eb04f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | from PIL import Image
import numpy as np
def remove_black_background(input_path, output_path):
try:
img = Image.open(input_path).convert("RGBA")
data = np.array(img)
# Calculate distance from black
# We'll use a threshold. If r,g,b are all low, make it transparent.
# Or better: use the brightness as alpha?
# Let's try a simple threshold first, or a linear interpolation for smoothness.
red, green, blue, alpha = data.T
# Define what is "black"
# We want to keep the colored parts (purple/blue) and remove the dark background.
# Let's say if (r+g+b) is low, it's background.
# A better approach for "glowing" logos on black:
# Set alpha based on lightness.
# But the logo has white/bright parts too.
# Let's try to just mask out pure black and near black.
# (0,0,0) -> Alpha 0
# Create a mask where pixels are close to black
black_areas = (red < 20) & (green < 20) & (blue < 20)
# Set alpha to 0 for black areas
data[..., 3][black_areas.T] = 0
# Save
new_img = Image.fromarray(data)
new_img.save(output_path)
print(f"Successfully saved to {output_path}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
remove_black_background("frontend/public/logo-symbol.png", "frontend/public/logo-symbol-transparent.png")
|