Image To ASCII Art Generator 05-17-2013, 07:36 PM
#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 :-
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.
Sample Run :-
The image I used is :-
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)
The output is (See the text in notepad)
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:
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]](http://i.imgur.com/2YpkRjy.png)
The output is (See the text in notepad)
Spoiler: Open the Spoiler
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]](http://fat.gfycat.com/OilyCostlyEwe.gif)