Skip to content

Instantly share code, notes, and snippets.

@raymondctc
Created February 14, 2025 19:03
Show Gist options
  • Save raymondctc/9f44d42324219b7aa8ae823b05b07cff to your computer and use it in GitHub Desktop.
Save raymondctc/9f44d42324219b7aa8ae823b05b07cff to your computer and use it in GitHub Desktop.
Applying a monotone filter to PNG for Android app icons
import os
from PIL import Image, ImageEnhance
def apply_monotone(image_path, output_path, color=(0, 0, 255)):
"""Apply a monotone filter while preserving transparency."""
img = Image.open(image_path).convert("RGBA") # Ensure RGBA mode
r, g, b, alpha = img.split() # Separate channels
# Convert image to grayscale
grayscale = ImageEnhance.Color(img).enhance(0)
# Apply color overlay only to non-transparent areas
color_layer = Image.new("RGBA", img.size, color + (255,))
blended = Image.blend(grayscale, color_layer, alpha=0.5) # Blend monotone color
# Restore the original alpha channel
blended.putalpha(alpha)
# Save the modified image
os.makedirs(os.path.dirname(output_path), exist_ok=True)
blended.save(output_path)
print(f"Processed: {output_path}")
def process_icons(input_root="res", output_root="debug_res", color=(0, 0, 255)):
"""Find all ic_launcher.png files and apply the monotone filter."""
for root, _, files in os.walk(input_root):
for filename in files:
if filename == "ic_launcher.png":
input_path = os.path.join(root, filename)
output_path = input_path.replace(input_root, output_root, 1)
apply_monotone(input_path, output_path, color)
# Run the script (change color for different debug colors)
process_icons(color=(0, 0, 255)) # Blue for debug build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment