Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev AndrewRadev/ascii_image.rs
Last active Mar 21, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.