ASCII-Gen, a CLI tool that converts images to ASCII art
github.com
github.com
EDIT: Have added an example of both a shrunken output, and a detailed zoomed out output.
let luminance = (0.2126 * avg_r as f32 + 0.7152 * avg_g as f32 + 0.0722 * avg_b as f32) as u8;
let character = match luminance {
0..=31 => '#',
32..=63 => '@',
64..=95 => '8',
96..=127 => '&',
128..=159 => 'o',
160..=191 => ':',
192..=223 => '*',
224..=255 => '.',
};
https://github.com/thed24/ascii-gen/blob/main/src/converter....``` /// Implementation for converting an image to ASCII art. impl ToAsciiArt for ImageConverter { fn to_ascii_art(&self, options: Option<AsciiOptions>) -> String { let options = options.unwrap_or_default();
let target_width = options.width;
let target_height = options.height;
let width_ratio = self.image.width() as f32 / target_width as f32;
let height_ratio = self.image.height() as f32 / target_height as f32;
let mut ascii_art = String::with_capacity((target_width * target_height) as usize);
for y in 0..target_height {
for x in 0..target_width {
let start_x = (x as f32 * width_ratio) as u32;
let start_y = (y as f32 * height_ratio) as u32;
let mut total_r = 0;
let mut total_g = 0;
let mut total_b = 0;
for dy in 0..height_ratio as u32 {
for dx in 0..width_ratio as u32 {
let pixel = self.image.get_pixel(start_x + dx, start_y + dy);
let channels = pixel.channels();
total_r += channels[0] as u32;
total_g += channels[1] as u32;
total_b += channels[2] as u32;
}
}
let count = (width_ratio * height_ratio) as u32;
let avg_r = (total_r / count) as u8;
let avg_g = (total_g / count) as u8;
let avg_b = (total_b / count) as u8;
let luminance = (0.2126 * avg_r as f32 + 0.7152 * avg_g as f32 + 0.0722 * avg_b as f32) as u8;
let character = match luminance {
0..=31 => '#',
32..=63 => '@',
64..=95 => '8',
96..=127 => '&',
128..=159 => 'o',
160..=191 => ':',
192..=223 => '*',
224..=255 => '.',
};
ascii_art.push(character);
}
ascii_art.push('\n');
}
ascii_art
}
}
```As for OP, he should try one of the libcaca wrapper for Rust, or make one as an exercise.
Twice now I have inadvertently navigated off of the comment page while trying to live text to a part of my phone’s screen that isn’t smashed and can actually display. So this is the third attempt to share this with you, let’s hope it elevates is the charm.
The page I am about to link to explains and demonstrates what IMHO looks much better than the output of similar tools, even when running the plain old black text on white background version of the tool. Though the demo picture
Since you are reading this article and at least some of the comments, I hope you will check it out and enjoy using it. The somewhat arbitrarily chosen
I hope my rambling doesn’t come across as an assholish response; I’m just kinda thinking aloud.
Truth be told, cranking the width and height up on my tool spits out a pretty accurate-looking result too, though granted without offsetting individual characters you can only go so far haha! Scaling the width and height down averages out the image to create a less detailed approximation.
Yes, it’s open source I. The sense that it runs entirely in your browser. :-) i don’t think I put any license in the code but I do t remember for sure.). That’s the web though right? If you like it, view it Learn it use it, right?
I haven’t put it in my GitHub because it’s really heinous code with lots of commented out nonsense as I was trying to figure out the html canvas, css, and JavaScript to make it work. (None of those pay my bills so it was as much a curiosity for me as it is to you. )
But yeah, if you promise not to point and laugh at how sloppy it is, just “view source”, or Ctrl-shift/I, or right-click|Inspect.. whatever your preferred method is. I may clean it up and make it more accessible if it seems of interest to folks.
Thanks again for taking a look and making time to comment on my tool, especially as a minor threadjack my comment was. :-)
gst-launch-1.0 filesrc location=my video.webm ! videoconvert ! aasink
This is a great way to watch videos inside your terminal.
Or, a terrible way to watch videos, I suppose, it depends on how much you like ASCII art.