Advertisement


Is there a command-line program that can convert an image to an ASCII art image?


Question

Is there a command-line program (for Linux) that can convert an image to ascii art and then save that output as an image e.g. jpg or png?

To illustrate what I'm in need of, I want to convert the image below into ascii art enter image description here

The output that I desire from a program should be similar to this one, which was created by taking a screenshot of the output of an image-to-ascii converter (the output was pure text)

enter image description here

I'm aware that GIMP has the ability to save an image as ascii art and aview can also convert an image to ascii. However, the former cannot be done via the command line and the latter only lets you view the output, not save it (I don't want to resort to taking a screenshot of the output).

I'm also aware that libcaca can save its output to an image, but it only outputs colourful ascii, like below.

enter image description here

Made by converting the source image to greyscale and then with the following command:

img2txt -W 70 -f tga input.jpg > output.tga

The reason for wanting a command-line program is to batch process a folder full of images.

2019/10/15
1
19
10/15/2019 4:55:00 PM

Accepted Answer

You can use this python code I created to create ascii art from still images and videos:

https://gist.github.com/lindylex/9374326

Example output

Original enter image description here

ASCII art output (python ascii_movie_image_ver_1.py cat.jpg 4 10) enter image description here

2014/05/05
7
5/5/2014 8:26:00 PM

In case you seem to be running Linux you may stitch the applications easily together to get the desired result. This can then also be incorporated in your batch processing script.

The example below makes use of jp2a, a powerfull JPG to ASCII-art converter with many options to achieve desired results (consult the manpage). We then further process the resulting ASCII text file to an image using convert from the ImageMagick suite. Again there are many options to fine tune the result. Both applications are likely to be available from your distribution's repositories.

Below sample script will produce an ASCII-art image (format depending on the extension we give, .jpg is for JPEG) from an input JPEG-image. The source is rendered to 160 character width at 6 pt. This will then just fit on the default page size as pre-defined from convert.

#! /bin/bash
#Usage ./j2asc infile.jpg outfile.jpg

echo "converting $1 to $2"
jp2a --width=160 -i  $1 --output=tmp.txt
convert -font Courier -pointsize 6 tmp.txt $2
2018/11/21