Filters - Capsize-Games/airunner GitHub Wiki

Image Filters

The AI Runner application includes a robust set of image filters that can be applied to images. These filters are implemented in the airunner.filters package and follow a common interface defined by the BaseFilter class.

Filter Architecture

All filters in AI Runner extend the BaseFilter class, which provides a standardized interface for applying transformations to images. The filters use PIL (Python Imaging Library) for image manipulation.

BaseFilter

The BaseFilter class provides:

  • A common initialization method that accepts arbitrary parameters as attributes
  • A caching mechanism to avoid redundant processing
  • The main filter method that delegates to apply_filter implementations

Available Filters

AI Runner includes the following image filters:

Basic Filters

  • BoxBlur: Applies a box blur with a specified radius
  • GaussianBlur: Applies a Gaussian blur with a specified radius
  • UnsharpMask: Enhances edges using an unsharp mask algorithm

Color Manipulation

  • Invert: Inverts the colors of an image
  • ColorBalanceFilter: Adjusts the color balance across red, green, and blue channels
  • SaturationFilter: Adjusts color saturation
  • RGBNoiseFilter: Adds random noise to each color channel

Special Effects

  • Dither: Applies Floyd-Steinberg dithering to create a black and white image
  • FilmFilter: Combines box blur and noise to simulate a film-like effect
  • HalftoneFilter: Creates a halftone effect using dots of varying sizes
  • PixelFilter: Creates a pixel art effect by reducing colors and resizing
  • RegistrationErrorFilter: Simulates color channel misalignment

Usage

Filters can be instantiated with parameters and applied to PIL Image objects:

from airunner.filters import BoxBlur
from PIL import Image

# Load an image
image = Image.open("my_image.jpg")

# Create and apply a filter
blurred_image = BoxBlur(radius=2).filter(image)

# Save the result
blurred_image.save("my_blurred_image.jpg")

Creating Custom Filters

To create a custom filter, extend the BaseFilter class and implement the apply_filter method:

from airunner.filters import BaseFilter

class MyCustomFilter(BaseFilter):
    def apply_filter(self, image, do_reset=False):
        # Implement your filter logic here
        # The do_reset parameter indicates if internal state should be reset
        
        # Return the filtered image
        return processed_image

Implementation Details

All filters in AI Runner follow the PEP 8 style guide and include comprehensive docstrings. The filter architecture uses caching to improve performance when applying the same filter multiple times to the same image.