Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active March 21, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRadev/6d07b768c39b657d58ff663b6739eca9 to your computer and use it in GitHub Desktop.
Save AndrewRadev/6d07b768c39b657d58ff663b6739eca9 to your computer and use it in GitHub Desktop.
Convert an image to simple ASCII-art
extern crate image;
use std::io::File;
use image::GenericImage;
fn main() {
let path = Path::new("examples/test_image.png");
let file = File::open(&path);
match image::load(file, image::ImageFormat::PNG) {
Ok(raw_image) => {
let image = raw_image.resize(180, 180, image::FilterType::Nearest).grayscale();
let (w, h) = image.dimensions();
for j in range(0, h) {
for i in range(0, w) {
let (red, _, _, _) = image.get_pixel(i, j).channels();
print!("{}", ascii_color(red));
}
println!("")
}
},
Err(e) => println!("Error! {}", e),
}
}
fn ascii_color(value: u8) -> char {
match value / 64 {
0 => '.',
1 => '-',
2 => '/',
3 => '#',
_ => ' ',
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment