Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Image To ASCII Art Generator filter_list
Author
Message
Image To ASCII Art Generator #1
Deque had conducted a contest on text to ascii art converter. I had made a program there in C++ for Image to Ascii Art. But my code had limitations as it was unable to manipulate the small size images and got the pixels messed up. Today I made a program in Java and it looks cool.

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:
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:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.ImageIO;

public class Img2Ascii {

    private BufferedImage img;
    private double pixval;
    private PrintWriter prntwrt;
    private 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.png");
    }
}

Sample Run :-

The image I used is :-


[Image: 2YpkRjy.png]

The output is (See the text in notepad)


Spoiler: Open the Spoiler
[Image: i0J2jdp.png]



To be true I didn't wanted to say this but I think soon I will fall in LOVE with Java and I hope that my present girlfriend VB.NET will have no problem :tongue:
[Image: OilyCostlyEwe.gif]

Reply

Image To ASCII Art Generator #2
Deque had conducted a contest on text to ascii art converter. I had made a program there in C++ for Image to Ascii Art. But my code had limitations as it was unable to manipulate the small size images and got the pixels messed up. Today I made a program in Java and it looks cool.

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:
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:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.imageio.ImageIO;

public class Img2Ascii {

    private BufferedImage img;
    private double pixval;
    private PrintWriter prntwrt;
    private 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.png");
    }
}

Sample Run :-

The image I used is :-


[Image: 2YpkRjy.png]

The output is (See the text in notepad)


Spoiler: Open the Spoiler
[Image: i0J2jdp.png]



To be true I didn't wanted to say this but I think soon I will fall in LOVE with Java and I hope that my present girlfriend VB.NET will have no problem :tongue:
[Image: OilyCostlyEwe.gif]

Reply

RE: [Java Code]Image To ASCII Art Generator #3
Great tool. Well done. And so simple actually. Thanks for the share.

You really fall in love with Java? Why that?
I read up a lot on C# and from the language perspective (not the implementation or anything, only seeing the language with its syntax, features and so on) it is better than Java imho (not surprisingly though, it is younger and Java had to maintain backward compatibility)
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [Java Code]Image To ASCII Art Generator #4
Great tool. Well done. And so simple actually. Thanks for the share.

You really fall in love with Java? Why that?
I read up a lot on C# and from the language perspective (not the implementation or anything, only seeing the language with its syntax, features and so on) it is better than Java imho (not surprisingly though, it is younger and Java had to maintain backward compatibility)
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [Java Code]Image To ASCII Art Generator #5
(05-17-2013, 08:32 PM)Deque Wrote: You really fall in love with Java? Why that?

:nono: Can't tell you that Mam :nono:

Quote:I read up a lot on C# and from the language perspective (not the implementation or anything, only seeing the language with its syntax, features and so on) it is better than Java imho (not surprisingly though, it is younger and Java had to maintain backward compatibility)

My teacher said that if there is a pure form of OOP more than Java then it is C#, but he is a Java Programmer too and he is the one who taught me that coding is important and optimization is not so. Only if I hadn't listen to him then I would probably have been a better coder today Confusedad:
[Image: OilyCostlyEwe.gif]

Reply

RE: [Java Code]Image To ASCII Art Generator #6
(05-17-2013, 08:32 PM)Deque Wrote: You really fall in love with Java? Why that?

:nono: Can't tell you that Mam :nono:

Quote:I read up a lot on C# and from the language perspective (not the implementation or anything, only seeing the language with its syntax, features and so on) it is better than Java imho (not surprisingly though, it is younger and Java had to maintain backward compatibility)

My teacher said that if there is a pure form of OOP more than Java then it is C#, but he is a Java Programmer too and he is the one who taught me that coding is important and optimization is not so. Only if I hadn't listen to him then I would probably have been a better coder today Confusedad:
[Image: OilyCostlyEwe.gif]

Reply

RE: [Java Code]Image To ASCII Art Generator #7
Thread updated and image links corrected
[Image: OilyCostlyEwe.gif]

Reply

RE: [Java Code]Image To ASCII Art Generator #8
Thread updated and image links corrected
[Image: OilyCostlyEwe.gif]

Reply







Users browsing this thread: 1 Guest(s)