alien-glow
2025-02-14
Adds a white glow effect around transparent PNG images. Useful for making icons or sprites pop against dark backgrounds.
#!/usr/bin/env python
from PIL import Image, ImageFilter
import argparse
def add_alien_glow(input_path, output_path, glow_radius=2, glow_intensity=105):
# Open image with transparency
img = Image.open(input_path).convert("RGBA")
# Extract alpha channel (transparency)
alpha = img.split()[3]
# Create a white base for the glow
white_glow = Image.new("RGBA", img.size, (255, 255, 255, 0))
glow_mask = alpha
# Expand the alpha to create the "glow radius"
for _ in range(glow_radius):
glow_mask = glow_mask.filter(ImageFilter.MaxFilter(3))
# Blur for a softer look
glow_mask = glow_mask.filter(ImageFilter.GaussianBlur(glow_radius))
# Put white into the glow area
# Create a lookup table for the glow intensity
# This resolves the "Operator > not supported" error with ImagePointTransform
lut = [min(255, int(p * (glow_intensity / 255.0))) for p in range(256)]
white_glow.putalpha(glow_mask.point(lut))
# Composite the glow behind the original image
final_img = Image.alpha_composite(white_glow, img)
final_img.save(output_path, "PNG")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Add an alien glow effect to an image.")
parser.add_argument("input_path", type=str, help="Path to the input image file (e.g., input.png).")
parser.add_argument("output_path", type=str, help="Path to save the output image file (e.g., output_glow.png).")
parser.add_argument("-r", "--glow_radius", type=int, default=3, help="Radius of the glow effect. Default is 25.")
parser.add_argument("-i", "--glow_intensity", type=int, default=125, help="Intensity of the glow effect (0-255). Default is 255.")
args = parser.parse_args()
add_alien_glow(args.input_path, args.output_path, args.glow_radius, args.glow_intensity)
Usage:
python alien-glow.py input.png output.png -r 5 -i 150