Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an ASCII picture from a string [closed]

I want to make a program, where the user enters their name and the program generates an ASCII picture of their name. Is it possible to do that without using 3rd party tools? What I came up with was to create each letter in the alphabet manually and then convert the string into a char array. Then return each image that corresponds with the letter. Is there a better or more efficent way of doing this?

like image 680
Jeel Shah Avatar asked Aug 17 '11 20:08

Jeel Shah


2 Answers

You can write text to a bitmap in any font you want and scan the bitmap to produce the ASCII art. You can even use anti-aliasing for effect. ;)


The last time I did this was about tens years ago. ;)

BufferedImage image = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(new Font("Dialog", Font.PLAIN, 24));
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString("Hello World!", 6, 24);
ImageIO.write(image, "png", new File("text.png"));

for (int y = 0; y < 32; y++) {
    StringBuilder sb = new StringBuilder();
    for (int x = 0; x < 144; x++)
        sb.append(image.getRGB(x, y) == -16777216 ? " " : image.getRGB(x, y) == -1 ? "#" : "*");
    if (sb.toString().trim().isEmpty()) continue;
    System.out.println(sb);
}

writes out an image

Hello World

and prints

    ##         ##                 ##    ##                      *#*      *##**      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*      *###*      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*      *#*#*      *#*                       ##            ##     ##       
    ##         ##                 ##    ##                      *#*     *******     *#*                       ##            ##     ##       
    ##         ##     *******     ##    ##     *******           *#*    *#* *#*    *#*    *******     ##****  ##     ****** ##     ##       
    ##         ##    **#####**    ##    ##    **#####**          *#*    *#* *#*    *#*   **#####**    ##*##*  ##    **#####*##     ##       
    ##         ##   **#*****#**   ##    ##   **#** **#**         *#*   **#* *#*    *#*  **#** **#**   ##****  ##   **#** ***##     ##       
    #############   *#*     *#*   ##    ##   *#*     *#*         *#*   *#*   *#*   *#*  *#*     *#*   ##*     ##   *#*     *##     ##       
    #############   *#*     *#*   ##    ##   *#*     *#*         ****  *#*   *#*  ****  *#*     *#*   ##*     ##   *#*     *##     ##       
    ##         ##   *#########*   ##    ##   *#*     *#*          *#*  *#*   *#*  *#*   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#########*   ##    ##   *#       #*          *#* *#*     *#* *#*   *#       #*   ##      ##   *#       ##     ##       
    ##         ##   *#*           ##    ##   *#*     *#*          *#* *#*     *#* *#*   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#*           ##    ##   *#*     *#*          *****#*     *#*****   *#*     *#*   ##      ##   *#*     *##     ##       
    ##         ##   *#*     *#*   ##    ##   *#*     *#*           *#*#*       *#*#*    *#*     *#*   ##      ##   *#*     *##              
    ##         ##   **#** *****   ##    ##   **#** **#**           *#*#*       *#*#*    **#** **#**   ##      ##   **#** ***##              
    ##         ##    **######*    ##    ##    **#####**            *###*       *###*     **#####**    ##      ##    **#####*##     ##       
    ##         ##     *******     ##    ##     *******             *##**       **##*      *******     ##      ##     ****** ##     ##    

change to BOLD

    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        *##*     *###*     *##*                          ###            ###    #
    ###       ###                 ###    ###                        **##*   **###**   *##**                          ###            ###    #
    ###       ###     *******     ###    ###      *******            *##*   *##*##*   *##*     *******      ###****  ###     ****** ###    *
    ###       ###    **#####**    ###    ###    **#######**          *##*   *##*##*   *##*   **#######**    ###*##*  ###    **#####*###    *
    ###       ###   **#######**   ###    ###    *#########*          *##**  *#* *#*  **#**   *#########*    #####**  ###   **##########    *
    #############   *##** **##*   ###    ###   *###** **###*          *##* *##* *##* *##*   *###** **###*   ###**    ###   *##******###    *
    #############   *##*   *##*   ###    ###   *##*     *##*          *##* *##* *##* *##*   *##*     *##*   ###*     ###   *##*    *###    *
    #############   *#########*   ###    ###   *##*     *##*          *##* *##* **#* *##*   *##*     *##*   ###*     ###   *##*    *###    *
    ###       ###   *#########*   ###    ###   ###       ##*           *##*##*   *#**##*    ###       ##*   ###*     ###   *##      ###    *
    ###       ###   *##*          ###    ###   *##*     *##*           *##*##*   *##*##*    *##*     *##*   ###      ###   *##*    *###    *
    ###       ###   *##*          ###    ###   *##*     *##*           *#####*   *#####*    *##*     *##*   ###      ###   *##*    *###    *
    ###       ###   *##*****##*   ###    ###   *###** **###*           **###**    *###**    *###** **###*   ###      ###   *##******###     
    ###       ###   **########*   ###    ###    *#########*             *###*     *###*      *#########*    ###      ###   **##########    #
    ###       ###    **######*    ###    ###    **#######**             *###*     *###*      **#######**    ###      ###    **#####*###    #
    ###       ###     *******     ###    ###      *******               *###*     *###*        *******      ###      ###     *******###    #

in ITALIC

      *#*        *#*                *#*   *#*                      *#*       *##*      **#*                      *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*      *###*      *#*                       *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*      *###*     **#*                       *#*          *#*  *#*      
      *#*        *#*                *#*   *#*                      *#*     *#**#*     *#*                        *#*          *#*  *#*      
     ****       *#*    *******     ****  ****   *******            *#*     *#**#*    ****   *******    *#* **#* ****   ********#*  *#*      
     *#*        *#*   **######*    *#*   *#*   **######*           *#*    *#***#*    *#*   **######*   *#**###* *#*   **####**#**  **       
     *#*        *#*  **#******#*   *#*   *#*  **#******#*          *#*   **#* *#*   ****  **#******#*  *#****   *#*  *##** **##*  *#*       
     *############* **#*     *#*   *#*   *#* **#*     *#*          *#*   *#*  *#*   *#*  **#*     *#*  *#**     *#* **#*    **#*  *#*       
    **###########** *#*       #*  ****  **** *#*      *#*          *#*  **#*  *#*  ****  *#*      *#* **#*     **** *#*      *#*  *#*       
    *#*        *#*  *##########*  *#*   *#*  *#*       #*          *#*  *#*   *#*  *#*   *#*       #* *#**     *#*  *#*      *#*  **        
    *#*        *#*  *##########*  *#*   *#*  *#*      *#*          *#* ****   *#* *#**   *#*      *#* *#*      *#*  *#*      *#*  **        
    *#*        *#*  *#            *#*   *#*  *#       *#*          *#* *#*    *#* *#*    *#       *#* *#*      *#*  *#      ***   **        
    *#*        *#*  *#*           *#*   *#*  *#*     **#*          *#**#**    *#**#**    *#*     **#* *#*      *#*  *#*     *#*   **        
   *#*        *#*   *#*     **** *#*   *#*   *#*     *#**          *#**#*     *#**#*     *#*     *#***#*      *#*   *#*    **#*             
   *#*        *#*   *#*******#*  *#*   *#*   *#*** **##*           *###**     *###**     *#*** **##* *#*      *#*   *#******##*             
   *#*        *#*    *######**   *#*   *#*    *######**            *###*      *###*       *######**  *#*      *#*   **#####**#*  *#*        
   *#*        *#*     *******    *#*   *#*     *******              ##*       *##*         *******   *#*      *#*    ********#*  *#*        

Change the Font to SERIF BOLD

   #######   #######             ####   ####                   *######* *######*   ####*                       ####         ####    ***     
    *###*     *###*              *###   *###                    **##**   **##**    ****                        *###         *###    *#*     
     ###       ###                ###    ###                     *##**    **##*     **                          ###          ###    *#*     
     ###       ###                ###    ###                      *##*     *##*    ***                          ###          ###    *#*     
     ###       ###                ###    ###                      *##*     **#**   **                           ###          ###    *#*     
     ###       ###      ******    ###    ###     ******           **##*   ***##*   **     ******    #### ****   ###     *****###    *#*     
     ###       ###     *#***#**   ###    ###    *#****#*           *##*   ***##*   **    *#****#*   *###*###*   ###    *#****###    *#*     
     #############    *##* *##*   ###    ###   *##*  *##*          *##*   ****#** **    *##*  *##*   ###*****   ###   *##*  *###    *#*     
     ###       ###    *##   ##*   ###    ###   *##*  *##*           *##* **  *##* **    *##*  *##*   ###*       ###   *##*   ###    *#*     
     ###       ###    *#######*   ###    ###   *##    ##*           *##* **  *##* **    *##    ##*   ###*       ###   *##    ###    *#*     
     ###       ###    *##*        ###    ###   *##    ##*           *##***   **##**     *##    ##*   ###        ###   *##    ###    *#      
     ###       ###    *##*        ###    ###   *##    ##*            *##**    *##**     *##    ##*   ###        ###   *##    ###            
     ###       ###    *##*   **   ###    ###   *##*  *##*            *##**    *###*     *##*  *##*   ###        ###   *##*   ###            
     ###       ###    *##** ***   ###    ###   *##*  *##*            **#*      *#*      *##*  *##*   ###        ###   *##*  *###*   ***     
    *###*     *###*    *#####*   *###*  *###*   *#****#*              *#*      *#*       *#****#*   *###*      *###*  **#****####   *#*     
   #######   #######   ******    #####  #####    ******               ***      *#*        ******    #####      #####   **********   ***     
like image 70
Peter Lawrey Avatar answered Oct 01 '22 11:10

Peter Lawrey


This problem is solved in four steps.

  1. Create an ascii art representation (as a 2d rectangular array) for each letter you intend to support. Perhaps these are loaded from a resource file.
  2. Parse the input string into character tokens and acquire the art for those characters.
  3. Make a rectangular array that is as tall as the tallest token, as wide as the sum of each tokens' width (plus space between characters if not included in the images themselves).
  4. Copy each one into the giant array at the correct location.
like image 27
corsiKa Avatar answered Oct 01 '22 13:10

corsiKa