|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Image icon processor that filters pixels by color range. |
| 4 | +Sets pixels within specified color bounds to black with alpha = 0. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +from PIL import Image |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def parse_color(color_str): |
| 13 | + """Parse color string in format 'R,G,B' to tuple (R, G, B).""" |
| 14 | + try: |
| 15 | + parts = color_str.split(',') |
| 16 | + if len(parts) != 3: |
| 17 | + raise ValueError(f"Color must have 3 components, got {len(parts)}") |
| 18 | + |
| 19 | + r, g, b = [int(x.strip()) for x in parts] |
| 20 | + |
| 21 | + if not all(0 <= val <= 255 for val in [r, g, b]): |
| 22 | + raise ValueError("Color values must be between 0 and 255") |
| 23 | + |
| 24 | + return (r, g, b) |
| 25 | + except Exception as e: |
| 26 | + raise ValueError(f"Invalid color format '{color_str}': {e}") |
| 27 | + |
| 28 | + |
| 29 | +def is_within_bounds(pixel, lower_bound, upper_bound): |
| 30 | + """Check if pixel color is within the specified bounds.""" |
| 31 | + r, g, b = pixel[:3] # Only check RGB, ignore alpha if present |
| 32 | + lr, lg, lb = lower_bound |
| 33 | + ur, ug, ub = upper_bound |
| 34 | + |
| 35 | + return (lr <= r <= ur and |
| 36 | + lg <= g <= ug and |
| 37 | + lb <= b <= ub) |
| 38 | + |
| 39 | + |
| 40 | +def process_image(input_path, lower_color, upper_color, output_path): |
| 41 | + """ |
| 42 | + Process image by setting pixels within color bounds to black with alpha = 0. |
| 43 | +
|
| 44 | + Args: |
| 45 | + input_path: Path to input image |
| 46 | + lower_color: Lower bound color tuple (R, G, B) |
| 47 | + upper_color: Upper bound color tuple (R, G, B) |
| 48 | + output_path: Path to save processed image |
| 49 | + """ |
| 50 | + try: |
| 51 | + # Load image |
| 52 | + img = Image.open(input_path) |
| 53 | + |
| 54 | + # Convert to RGBA if not already (to support alpha channel) |
| 55 | + if img.mode != 'RGBA': |
| 56 | + img = img.convert('RGBA') |
| 57 | + |
| 58 | + # Get pixel data |
| 59 | + pixels = img.load() |
| 60 | + width, height = img.size |
| 61 | + |
| 62 | + # Process each pixel |
| 63 | + count = 0 |
| 64 | + for y in range(height): |
| 65 | + for x in range(width): |
| 66 | + pixel = pixels[x, y] |
| 67 | + if is_within_bounds(pixel, lower_color, upper_color): |
| 68 | + pixels[x, y] = (0, 0, 0, 0) # Black with alpha = 0 |
| 69 | + count += 1 |
| 70 | + |
| 71 | + # Save processed image |
| 72 | + img.save(output_path, 'PNG') |
| 73 | + print(f"Processed image saved to {output_path}") |
| 74 | + print(f"Modified {count} pixels (out of {width * height} total)") |
| 75 | + |
| 76 | + except FileNotFoundError: |
| 77 | + print(f"Error: Input file '{input_path}' not found", file=sys.stderr) |
| 78 | + sys.exit(1) |
| 79 | + except Exception as e: |
| 80 | + print(f"Error processing image: {e}", file=sys.stderr) |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + parser = argparse.ArgumentParser( |
| 86 | + description='Process image icon by filtering pixels within color bounds.', |
| 87 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 88 | + epilog=''' |
| 89 | +Examples: |
| 90 | + %(prog)s input.png "255,0,0" "255,100,100" -o output.png |
| 91 | + %(prog)s icon.png "0,0,0" "50,50,50" |
| 92 | + ''' |
| 93 | + ) |
| 94 | + |
| 95 | + parser.add_argument('input', help='Path to input image') |
| 96 | + parser.add_argument('lower_color', help='Lower bound color in format "R,G,B"') |
| 97 | + parser.add_argument('upper_color', help='Upper bound color in format "R,G,B"') |
| 98 | + parser.add_argument('-o', '--output', default='output.png', |
| 99 | + help='Output path (default: output.png)') |
| 100 | + |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + # Parse color values |
| 104 | + try: |
| 105 | + lower = parse_color(args.lower_color) |
| 106 | + upper = parse_color(args.upper_color) |
| 107 | + except ValueError as e: |
| 108 | + print(f"Error: {e}", file=sys.stderr) |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + # Process the image |
| 112 | + process_image(args.input, lower, upper, args.output) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + main() |
0 commit comments