Show HN: Pixel Art Generator Using Genetic Algorithm

4 months ago 2

A genetic algorithm implementation that evolves pixel art by attempting to recreate a target image through evolutionary computation. The program generates an animated GIF showing the evolution process from random noise to the final approximation.

This project uses a genetic algorithm to evolve RGB pixel values for each position in a 100x100 image grid. Each pixel is represented by a chromosome containing binary genes for red, green, and blue color channels. Through selection, crossover, and mutation operations, the algorithm iteratively improves the image quality to match a target image.

Genetic Algorithm Parameters

const IMG_SIZE: usize = 100; // Image dimensions (100x100) const POPULATION_SIZE: usize = 6; // Population size per pixel const ITERATION: usize = 50; // Number of generations const MUTATION_RATE: f64 = 0.05; // Bit-flip mutation probability const CROSSOVER_RATE: f64 = 0.8; // Crossover probability const GENE_LENGTH: usize = 8; // Bits per color channel const RGB_CHANNELS: usize = 3; // Red, Green, Blue channels const TOURNAMENT_SIZE: usize = 3; // Tournament selection size const ELITE_SIZE: usize = 2; // Number of elite individuals preserved

Add these dependencies to your Cargo.toml:

[dependencies] image = "0.24" rand = "0.8" gif = "0.12"
# Place a target image named target.png in the project directory. cargo run
  • result.png: Final evolved image
  • result.gif: Animated evolution process
  • target_sample.png: Copy of the target image used
Read Entire Article