ASCII Camera in command line
Published: Thu, April 11th, 2024Over the past week, I've been working on a command line tool which allows you to output the camera as an ASCII representation. It uses OpenCV to process camera feed and curses library to display the processed image nicely in the command line.
The output 'pixels' are matched from camera feed pixel colors to the piece of rendered vocabulary closest in its appearance, e.g. a dark pixel will be mapped to a character which appears dark when displayed on the screen, like 'W' or 'M'.
Cool things I've discovered
Curses library
An early version of this script would output the image provided to a text file. "That's kind of boring" I thought, so I plugged the input from the device camera using OpenCV library. Part way through trying to generate gifs based on the feed, I realised that it would look visually about the same if the output was printed in command line.
I've never used curses library before, so my early attempts were disappointing, as the command line would scroll infinitely. What would end up looking better is replacing the whole content of the command line while the script is running, as you can see in the demo.
Some emoji codes suck
Do you like how monospace fonts and emojis in the demo seem to align to create a rectangular picture? Well, for emojis this simple pleasant effect is unreasonably tricky. I got a list of emojii codes via the 'emoji' library. Some of them correspond to multiple pictures, or are rendered with non-standard width. I had to prune those, leaving me with a subset of actual emojis.