Last active
March 21, 2017 14:52
-
-
Save AndrewRadev/6d07b768c39b657d58ff663b6739eca9 to your computer and use it in GitHub Desktop.
Convert an image to simple ASCII-art
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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