Home Command line utilities How To Convert Images Into ASCII Format In Linux

How To Convert Images Into ASCII Format In Linux

By sk
Published: Last Updated on 25.1K views

This guide teaches how to convert images into ASCII format in Linux. For the purpose of this task, we will be using Jp2a. Jp2a is a command line tool that helps you to convert the given image(s) into ascii character format. You can specify a mixture of image files and URLs and the Jp2a tool will convert all of them into ascii characters instantly. Jp2a has many useful options to manipulate the images. Here are some couple things that Jp2a can do for you!

  • read images from standard input,
  • set background mode as light or dark,
  • set border,
  • set output height and width,
  • set custom dimension for output images,
  • flip output image vertically or horizontally,
  • use a specific characters when producing the output ASCII image,
  • invert images,
  • download images from web and convert them,
  • And many.

Install Jp2a in Linux

Jp2a is packaged for many Linux distributions and is available in the default repositories.

To install Jp2a on Arch Linux and its variants like Manjaro Linux, run:

$ sudo pacman -S jp2a

On Debian, Ubuntu, Linux mint:

$ sudo apt install jp2a

On Fedora:

$ sudo dnf install jp2a

On CentOS / RHEL:

$ sudo yum install epel-release
$ sudo yum install jp2a

On openSUSE:

$ sudo zypper install jp2a

Convert Images Into ASCII Format In Linux

To convert a given JPG/JPEG image into ASCII character format, simply run:

jp2a <path-to-image>

Example:

$ jp2a arch.jpg

Sample output:

MWXMWXWWXWWXWWXWWXWMXWMXNMNNMNNMNNMNNMNXWNXMWXWWXWWXWWXWMXWMXNMXNMNNMNNMNNMNXMNXMW
NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWdxWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNkccxWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNW0ccccOWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWW
NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWKlccccc0WNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNXlccccccl0WWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNocccccccclXWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWWNWW
NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNdccccccccccoXWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNkccccccccccccoXWWNWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNkccccccccccccccdNWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWW
NWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNkccccccccccccccccdNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWXxccccccccccccccccdXWWNWWNWWNNWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWNWWNWWNNWNOoOXNklccccccccccccccdNWNWWNWWNWWNNWNNWNNWNNWNNWWNWW
NWWNNWNNWNNWNNWNNWWNWWNWWNWWNOccccok0kdcccccccccccccoXWNNWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNWWNNOcccccccccodoccccccccccccoXWWNWWNWWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWNWWNWWNkccccccccccccccccccccccccccoNWNWWNWWNWWNNWNNWNNWNNWWNWW
NWWNWWNNWNNWNNWNNWWNWWNWWNkccccccccccccccccccccccccccccoXWWNWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNWWNNkccccccccccccccccccccccccccccccoKWWNWWNNWNNWNNWNNWNNWWNWW
WWNWWNWWNWWNWWNWWNWWNWWNxcccccccccccccccccccccccccccccccclXWNNWNNWNNWNNWNNWNNWNNWW
NWWNNWNNWNNWNNWNNWWNWWXdcccccccccccccccccccccccccccccccccclKWWNWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNWWNNdccccccccccccccccccccccccccccccccccccl0NWNNWNNWNNWNNWNNWNNWW
WWNWWNWWNWWNWWNWWNWWXoccccccccccccccccdx0Oxdlcccccccccccccccl0WNWWNNWNNWNNWNNWWNWW
NWWNWWNNWNNWNNWNNWWKlcccccccccccccclkXWWNWWNNOlcccccccccccccccOWWNWWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNWWNKlcccccccccccccclKNWWNWWNWWNXocccccccccccccccONWNNWNNWNNWNNWWNWW
WWNWWNWWNWWNWWNWW0lcccccccccccccclXNNWNNWWNWWNWNoccccccccccccccckWNNWNNWNNWNNWNNWW
NWWNNWNNWNNWNNWNkccccccccccccccccONWWNWWNWWNNWNNXccccccccccccccccdNWNWWNWWNWWNWWNN
WWNWWNWWNWWNWWNkcccccccccccccccclNWNNWWNWWNWWNWWNdcccccccccloolcccdNWNNWNNWNNWWNWW
WWNWWNWWNWWNWWxcccccccccccccccccoNWNNWWNWWNWWNWWNkcccccccccccoxO0OddNNNWNNWNNWNNWW
NWWNNWNNWNNWXdccccccccccccccccccoWNWWNWWNWWNNWNNWkcccccccccccccclxKNXWWNWWNWWNWWNN
WWNWWNWWNWWXoccccccccccccccccccccXWNNWWNWWNWWNWWNdcccccccccccccccccoOXNWNNWNNWWNWW
WWNWWNWWNWXocccccccccccccccldxk00XWNNWNNWWNWWNWWNK0kxdlcccccccccccccccxKNNWNNWNNWW
NWWNNWNNWKlccccccccccldxO0NWNWWNWWNWWNWWNWWNNWNNWNNWNNWXK0xdlccccccccccl0WNWWMMMMM
WWNWWNWWOccccccccoxOXNWNWWNWWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNKOxoccccccccOWNWMMMMM
WWNWWNWOcccccox0NNWWNWWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNNWNNN0xoccccckNNMMMMM
NWWNNWkccok0XNWNNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWWNWWNWWNWWNN0xocckWNWWNW
WWNWNxdOXWWNWWNWWNWWNWWNWWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNXOxxNNNWW
NWWNXNNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWWNWWNWWNNWNNWNNWNNWNNWNNWWNWWNWWNWWNWWNWNXWWNN

Convert Images Into ASCII Format Using Jp2a In Linux

Simple, isn't it?

You can specify multiple images and convert all of them in one go.

$ jp2a img1.jpg img2.jpg

This command converts the given images into ASCII and will display them one after another.

Like I already said, Jp2a has many options and flags to manipulate the input images. Let us see the usage of some options with examples.

Read images from standard input

To read images from standard input and prints the ASCII characters in the standard output, make use of pipe command like below.

$ cat arch.jpg | jp2a -

Mind the hyphen symbol (-) at the end.

Write output to a file

Instead of displaying the ASCII image in the standard output, you can write it to a file as shown below.

$ jp2a arch.jpg --output=arch_ascii

This command will append the ASCII output in a text file called arch_ascii. You can view the file's contents using the "cat" command:

$ cat arch_ascii

Write ascii output to a file

Print images with specific height/width

You can convert the image to ASCII format and print them with a specific height or width of your choice.

To print using a specific height, for example 30 rows, simply do:

$ jp2a --height=30 arch.jpg

The width will be automatically calculated from the aspect ratio of the image.

Similarly, to specify the width, run:

$ jp2a --weight=30 arch.jpg

You can also combine the both options and print the image.

$ jp2a --height=20 --width=40 arch.jpg

Print images in ASCII format in X columns and Y rows

The following command converts the given image file into ASCII and prints the output in 50 columns and 30 rows.

$ jp2a --size=50x30 arch.jpg

Sample output:

print images in ASCII format in X columns and Y rows using Jp2a

Set border to ASCII characters

If you want to set border around the ASCII output, use border option like below:

$ jp2a --border --size=50x30 arch.jpg

Sample output:

Set border to ASCII characters using Jp2a

Print images in light/dark background

Jp2a has options to print the ASCII characters in light and dark backgrounds.

$ jp2a --background=light arch.jpg
$ jp2a --background=dark arch.jpg

Print images in light or dark background using Jp2a

Print images into ASCII format in dark background

If you view a picture with white background, but you are using a display with light characters on a dark background, you should invert the image using the invert flag.

$ jp2a arch.jpg --invert

Sample output:

  .  .  .  .  .  .  .  .               .  .  .  .  .  .  .  .  .            .  .  
                                        :;                                        
                                       ,oo;                                       
                                      .oooo'                                      
                                     .looooo.                                     
                                    .looooool.                                    
                                    cooooooool.                                   
                                   :ooooooooooc.                                  
                                  ,ooooooooooooc.                                 
                                 ,oooooooooooooo:                                 
                                ,oooooooooooooooo:                                
                                .;oooooooooooooooo:.                              
                              'c'. ,loooooooooooooo:                              
                             'ooooc,.,:oooooooooooooc.                            
                            'oooooooooc:cooooooooooooc.                           
                           ,ooooooooooooooooooooooooooc                           
                          ,ooooooooooooooooooooooooooooc.                         
                         ,ooooooooooooooooooooooooooooooc.                        
                        ;ooooooooooooooooooooooooooooooool.                       
                      .:ooooooooooooooooooooooooooooooooool.                      
                      :ooooooooooooooooooooooooooooooooooool.                     
                    .coooooooooooooooo:;.';:loooooooooooooool.                    
                   .looooooooooooool,.       'looooooooooooooo'                   
                  .looooooooooooool.          .cooooooooooooooo'                  
                 .looooooooooooool.             cooooooooooooooo,                 
                ,oooooooooooooooo'              .oooooooooooooooo:                
               ,ooooooooooooooool                :ooooooooolcclooo:               
              ;oooooooooooooooooc                ,oooooooooooc;'.'::              
            .:ooooooooooooooooooc                ,ooooooooooooool;. .             
           .coooooooooooooooooooo.               :oooooooooooooooooc'.            
          .coooooooooooooool:;,...               ..,;:looooooooooooooo;.          
         .looooooooool:;'.                             ...;:looooooooool.         
        'ooooooooc;'.                                        .';coooooooo'        
       'oooooc;.                                                  .;cooooo,       
      ,ooc,..                                                         .;coo,      
     ;:'.                                                                .';;     
    .                                                                        .

Convert Images Into ASCII Format With invert option Using Jp2a

Print images with specific characters only

Instead of printing the image in the default characters, you can use some custom characters of your choice.

$ jp2a --size=50x30 --chars=" ...sskk@@" arch.jpg

Download images directly from Internet

Not just local images, it is possible to directly download the images from Internet and convert them into ASCII.

To download images directly from Internet and print them in ASCII format, run:

$ jp2a --size=50x30 https://upload.wikimedia.org/wikipedia/commons/d/dd/Linux_logo.jpg

Download images directly from Internet using Jp2a

Alternatively, use curl or wget programs to download the images and jp2a will read the image from standard input like below.

$ curl -s https://upload.wikimedia.org/wikipedia/commons/d/dd/Linux_logo.jpg | jp2a -

Convert images other than JPG/JPEG

What if you have the different image type other than the JPG/JPEG? In that case, you can use ImageMagick's Convert tool like below. ImageMagick is available in the default repositories of many Linux distributions.

On Arch Linux and its variants like Manjaro Linux:

$ sudo pacman -S imagemagick

On Debian, Ubuntu, Linux Mint:

$ sudo apt install imagemagick

After installing ImageMagick, run the following command to convert the given PNG format image to Jpeg/Jpg format and then convert it to ASCII format:

$ convert arch.png jpg:- | jp2a -

Please note the hyphen symbol (-) at the end.

Convert PNG image into ASCII using Jp2a

Similarly, you can simply convert any image format into JPEG/JPG and then convert it again to ASCII format.

Produce strict HTML output

Jp2a has an option to produce strict XHTML 1.0 output.

$ jp2a --size=50x30 --html arch.jpg --output=arch.html

Sample output:

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'>
<head>
<title>jp2a converted image</title>
<style type='text/css'>
body {
background-color: black;
}
.ascii {
   font-family: Courier;
   color: white;
   font-size:8pt;
   font-weight: bold;
}
</style>
</head>
<body>
<div class='ascii'><pre>
MNWNWNWWNWNWNMXMNWNWNWWNNXWNWXMXWNWNWNNWNWNWXMXWNW
NWNWNWWWWNWNWNWNWNWNWWWWkkNWNWNWNWNWWWWWWNWNWNWNWN
NWNWNWWWWWWNWNWNWNWNWWWKccKWNWNWNWNWWWWWWNWNWNWNWN
NWNWNWWWWWWNWNWNWNWNWWNoccoNNWNWNWNWWWWWWNWNWNWNWN
WNWNWWWWWWNWNWNWNWNWWWOcccckWNWNWNWNWWWWNWNWNWNWNW
WNWNWWWWNWNWNWNWNWNWWXlcccccKNWNWNWNWWWWNWNWNWNWNW
WNWWWWWWWWNWNWNWNWWWNdcccccclNWNWNWWWWWWWWNWNWNWNW
NWNWNWWWWNWNWNWNWNWNOccccccccxNWNWNWWWWWWNWNWNWNWN
NWNWNWWWWWWNWNWNWNWXlccccccccc0WNWNWWWWWWNWNWNWNWN
WWWWWWWWWWWWWWWWWWNNkccccccccclXWWWWWWWWWWWWWWWWWW
WNWNWWWWNWNWNWNWNWxlkKdccccccccoNNWNWWWWNWNWNWNWNW
WNWNWWWWWWNWNWNWNOccccddocccccccxNWNWWWWNWNWNWNWNW
WWWWWWWWWWWWWWWWKccccccccccccccccOWWWWWWWWWWWWWWWW
NWNWNWWWWNWNWNWXlcccccccccccccccccKWWWWWWNWNWNWNWN
NWNWNWWWWNWNWNNdcccccccccccccccccclXNWWWWNWNWNWNWN
WWWWWWWWWWWWWWxccccccccccccccccccccdNWWWWWWWWWWWWW
WNWNWWWWWWNWNOccccccccccccccccccccccxNWWNWNWNWNWNW
WNWNWWWWWWNWKccccccccclx0OklcccccccccOWWNWNWNWNWNW
WWWWWWWWWWWXlccccccccoXWWWWXdccccccccc0WWWWWWWWWWW
NWNWNWWWWNNocccccccclNWWNWNWNocccccccclXWNWNWNWNWN
NWNWNWWWWNdcccccccccOWWWNWNWNKcccccccccoNNWNWNWNWN
WWWWWWWWNxccccccccccXWWWWWWWWNlcccccldlcoNWWWWWWWW
WNWNWWWWOcccccccccccNWWWWNWNWNocccccccx0O0NWNWNWNW
WNWNWWW0ccccccccccccXWWWWNWNWNlcccccccccdKNWNWNWNW
NWNWNWKlcccccccccodkXWWWNWNWNNkdocccccccccdKWNWNWN
NWNWNXlccccccdk0NNWNWWWWNWNWNWNWNN0kdcccccclXNWMMM
NWNWNoccclx0NNWNWNWNWWWWNWNWNWNWNWNWWN0xlccclXWMMM
WNWNdclkKNNWNWNWNWNWWWWWWNWNWNWNWNWWWWWWNKklcdNWMW
WNWkxKNWWWNWNWNWNWNWWWWWWNWNWNWNWNWNWWWWNWNWKxkWNW
NWXNNWWNWNWNWNWNWNWNWWNWNWNWNWNWNWNWNWWNWNWNWNNNWN
</pre>
</div>
</body>
</html>

This can be useful when you want to view the ASCII characters of an image in a web browser.

Produce strict HTML output using Jp2a

There are a few more options available such as flip the images in X and Y direction, set RGB to grayscale conversion weight, use ANSI colors in output, use terminal display height/width etc. Look at the help section or man pages to learn to use them.

For help, run:

$ jp2a --help
$ man jp2a

Suggested read:


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More