65 lines
2.5 KiB
Python
65 lines
2.5 KiB
Python
import os
|
|
from PIL import Image
|
|
import colorsys
|
|
|
|
def recolor_yellow_to_purple(input_path, output_path):
|
|
img = Image.open(input_path).convert("RGBA")
|
|
width, height = img.size
|
|
pixels = img.load()
|
|
|
|
# Target purple color in HSV
|
|
# We want a vibrant purple matching --on-primary-container or --primary-container
|
|
# Let's say hue is around 280 degrees (0.77 in 0-1 range)
|
|
target_hue = 280.0 / 360.0
|
|
|
|
recolored_count = 0
|
|
|
|
for x in range(width):
|
|
for y in range(height):
|
|
r, g, b, a = pixels[x, y]
|
|
if a == 0:
|
|
continue
|
|
|
|
# Convert RGB to HSV
|
|
# RGB values are 0-255, colorsys expects 0-1
|
|
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
|
|
|
|
# Check if the pixel is yellow/orange
|
|
# Yellow hue is around 40-75 degrees (0.11 to 0.21)
|
|
# Orange/yellow can start around 35 degrees (0.097)
|
|
# We want to avoid skin tones, which are typically warmer/redder (hue 10-30 degrees, i.e., 0.02 to 0.08)
|
|
# and have lower saturation.
|
|
# Bright yellow elements like the bag, scooter, shirt, helmet have high saturation (s > 0.4)
|
|
# and high value (v > 0.4).
|
|
|
|
is_yellow = False
|
|
|
|
# Bright yellow/orange check
|
|
# Hue range: 38 to 82 degrees (0.105 to 0.228)
|
|
if 0.10 <= h <= 0.23 and s >= 0.35 and v >= 0.30:
|
|
is_yellow = True
|
|
# Let's also include slightly redder orange if it is highly saturated (which skin is not)
|
|
elif 0.07 <= h < 0.10 and s >= 0.70 and v >= 0.50:
|
|
is_yellow = True
|
|
|
|
if is_yellow:
|
|
# We shift the hue to target purple (280 degrees)
|
|
# Keep saturation and value similar, maybe boost saturation a bit for richness
|
|
new_h = target_hue
|
|
new_s = min(s * 1.0, 1.0)
|
|
new_v = v
|
|
|
|
# Convert back to RGB
|
|
new_r, new_g, new_b = colorsys.hsv_to_rgb(new_h, new_s, new_v)
|
|
pixels[x, y] = (int(new_r * 255), int(new_g * 255), int(new_b * 255), a)
|
|
recolored_count += 1
|
|
|
|
img.save(output_path, "PNG")
|
|
print(f"Recolored {recolored_count} pixels and saved to {output_path}")
|
|
|
|
if __name__ == "__main__":
|
|
recolor_yellow_to_purple(
|
|
"public/images/slider-courier-mask.png",
|
|
"public/images/slider-courier-mask-purple.png"
|
|
)
|