Skip to content

Instantly share code, notes, and snippets.

@andir
Created February 22, 2026 12:51
Show Gist options
  • Select an option

  • Save andir/76aac0c30b3725aea02e7503d2395d15 to your computer and use it in GitHub Desktop.

Select an option

Save andir/76aac0c30b3725aea02e7503d2395d15 to your computer and use it in GitHub Desktop.
import sys
import os
from PIL import Image
import numpy as np
def recolor_blue_to_red(image_path):
img = Image.open(image_path)
# Preserve original format
original_format = img.format
print(image_path, img.mode)
# Handle alpha channel if present
if img.mode in ("RGBA", "LA", "P"):
img = img.convert("RGBA")
alpha = img.getchannel("A")
img = img.convert("RGB")
has_alpha = True
else:
img = img.convert("RGB")
has_alpha = False
# Convert to HSV
hsv = img.convert("HSV")
hsv_np = np.array(hsv)
h = hsv_np[:, :, 0]
# Blue hue range in Pillow HSV (0–255 scale)
blue_min = 140
blue_max = 200
# Mask blue pixels
mask = (h >= blue_min) & (h <= blue_max)
# Shift blue hue to red (red ≈ 0)
h[mask] = 0
hsv_np[:, :, 0] = h
recolored = Image.fromarray(hsv_np, "HSV").convert("RGB")
# Reattach alpha if needed
if has_alpha:
recolored.putalpha(alpha)
# Save to same path, same format
recolored.save(image_path, format=original_format)
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: python {sys.argv[0]} <image_path>")
sys.exit(1)
image_path = sys.argv[1]
if not os.path.isfile(image_path):
print("Error: File not found.")
sys.exit(1)
recolor_blue_to_red(image_path)
print("Image recolored successfully.")
{ pkgs, ... }:
let
recolor = pkgs.writers.writePython3 "recolor" {
libraries = [pkgs.python3Packages.pillow pkgs.python3Packages.numpy];
} (builtins.readFile ./deluge-recolor.py);
in
{
config.services.deluge = {
enable = true;
declarative = true;
package = pkgs.deluge-2_x.overrideAttrs ({postInstall ? "", ...}: {
postInstall = postInstall + ''
for file in $out/${pkgs.python3Packages.python.sitePackages}/deluge/ui/web/icons/deluge-*.png; do
${recolor} $file
done
'';
});
# ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment