Convert images to ASCII art without native dependencies
ascii-image allows you to convert images to ASCII art without native
dependencies. This means that all you need to do is npm install ascii-image
,
instead of brew
ing and apt-get
ing other packages.
- Support for most common image types
- Color and B/W
- Numerous resizing options
- CLI tool
Just install with npm
:
$ npm install asciify-image
Or, if you want to use it directly in the command line:
$ npm install -g asciify-image
This API applies to asciify-image both as a Node.js module (example)
and as a CLI tool. Use the -h
or --help
flag to see more about the CLI tool.
The path to the image you wish to asciify. Currently supported formats are:
- JPG
- PNG
- GIF
Default: true
If options.color
is set to true
, the asciified image will be in color when
printed in your terminal. If set to false
, the image will be in black and
white.
Default: 'original', CLI default: 'box'
The fit to which to resize the image:
box
- Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio.width
- Resize the image by scaling the width to the specified width. Maintains aspect ratio.height
- Resize the image by scaling the height to the specified height. Maintains aspect ratio.original
- Doesn't resize the image.none
- Scales the width and height to the specified values, ignoring original aspect ratio.
Default: original image width, CLI default: window width
The width to which to resize the image.
Default: original image height, CLI default: window height
The height to which to resize the image.
Default: 'string'
The format in which to return the asciified image. Can be "string" or "array".
Default: 2
Since a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't really need to change this.
The function to call after the image is asciified. Receives the asciified text as a parameter.
var asciify = require('asciify-image');
var options = {
fit: 'box',
width: 200,
height: 100
}
asciify('path/to/image.png', options, function (asciified) {
// Print to console
console.log(asciified);
});
Images are represented by pixels. This package reads each pixel as an RGBa value. Each of these values is converted into a single integer, called "intensity". A darker pixel would have a higher intensity, and a lighter pixel would have a lower intensity.
For each pixel, a character is substituted: for a light pixel, the character "," may be substituted, but for a darker pixel, the character "8" would be substituted. Since these characters are different sizes, they look lighter or darker in the big picture (pun somewhat intended).
Some inspiration from image-to-ascii, but the code is written from scratch. Mostly created this because I didn't like the native dependencies required in existing asciification libraries.