Author Topic: [Java Code]Image To ASCII Art Generator  (Read 5160 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
[Java Code]Image To ASCII Art Generator
« on: July 14, 2013, 08:28:12 pm »
The following piece of code converts an image to an ascii art.


How to Use ?

This code doesn't takes command line input so you need to pass the image name with extension in the main function.



example :-


Code: [Select]
obj.convertToAscii("Your_Image_Name.jpg");


When you run the code a text file "asciiart.txt" is created which has the Ascii Art of the image you gave as input. For better vision of the converted image set the font size to 2 or 1.


Code: [Select]
import java.awt.Color;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;


class Img2Ascii {


    BufferedImage img;
    double pixval;
    PrintWriter prntwrt;
    FileWriter filewrt;


    public Img2Ascii() {
        try {
            prntwrt = new PrintWriter(filewrt = new FileWriter("asciiart.txt",true));
        } catch (IOException ex) {
        }
    }
   
    public void convertToAscii(String imgname) {
        try {
            img = ImageIO.read(new File(imgname));
        } catch (IOException e) {
        }


        for (int i = 0; i < img.getHeight(); i++)
        {
            for (int j = 0; j < img.getWidth(); j++)
            {
                Color pixcol = new Color(img.getRGB(j, i));
                pixval = (((pixcol.getRed() * 0.30) + (pixcol.getBlue() * 0.59) + (pixcol.getGreen() * 0.11)));
                print(strChar(pixval));
            }
            try {
                prntwrt.println("");
                prntwrt.flush();
                filewrt.flush();
            } catch (Exception ex) {
            }
        }
    }


    public String strChar(double g)
    {
        String str = " ";
        if (g >= 240) {
            str = " ";
        } else if (g >= 210) {
            str = ".";
        } else if (g >= 190) {
            str = "*";
        } else if (g >= 170) {
            str = "+";
        } else if (g >= 120) {
            str = "^";
        } else if (g >= 110) {
            str = "&";
        } else if (g >= 80) {
            str = "8";
        } else if (g >= 60) {
            str = "#";
        } else {
            str = "@";
        }
        return str;
    }


    public void print(String str)
    {
        try {
            prntwrt.print(str);
            prntwrt.flush();
            filewrt.flush();
        } catch (Exception ex) {
        }
    }
   
    public static void main(String[] args) {
    Img2Ascii obj=new Img2Ascii();
    obj.convertToAscii("hcdev.jpg");
    }
}


Sample Run :-


The image I used is :-


1. Evil zone logo
2. Shinchan





The output is :-





"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins

Offline ZTP0

  • /dev/null
  • *
  • Posts: 13
  • Cookies: 2
    • View Profile
Re: [Java Code]Image To ASCII Art Generator
« Reply #1 on: August 11, 2013, 02:48:40 pm »
This is so funny! Thank you! I tried it with some random images and it worked nicely, is there any specific reason behind the choice of the thresholds and the symbols you used or did you just try and try until you got it right?


I agree that taking the argument from command line instead of forcing you to call your image "hcdev.jpg" would be definitely better.