Yesterday, I wanted to experiment a little bit with ascii art and was searching for a nice simple piece of software that converts image in ascii art console output. After a while I decided to write some simple java class that do the job (not because there were no such nice programs, but I was tired of searching). It works simple, outputs the image in the console and in a txt file, takes as input argument an image file, but it is not (still) suitable for large images (> 50px each side). Works good with pictograms. And it is not quite ascii, just sign art :) If I find some free hours in the next week, I'm gonna improve it to do more and better job, but for now that's not the case. And here it is:
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Visualizer {
public Visualizer(File file)
{
try
{
// the line that reads the image file
BufferedImage image = ImageIO.read(file);
int height = image.getHeight();
int width = image.getWidth();
System.out.println("Picture width: "+width);
System.out.println("Picture height: "+height);
int i, j;
String output = "";
for (i=0; i < height; i++)
{
for (j=0; j < width; j++)
{
int pixel = image.getRGB(j, i);
int grayscale = convertToGrayscale(pixel);
//writes the string for the txt file
output = output+" "+getASCIIforGrayscale(grayscale);
//prints the image in the console
System.out.print(" "+getASCIIforGrayscale(grayscale));
}
output = output+"\n";
System.out.print("\n");
}
writeToFile(output);
}
catch (IOException e)
{
// log the exception
// re-throw if desired
}
}
public int convertToGrayscale(int pixel)
{
//int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
int grayValue = (red + green + blue) / 3;
return grayValue;
}
public String getASCIIforGrayscale(int grayValue){
String ascii = ".";
ascii = lookupTableGray(grayValue);
return ascii;
}
public void printPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
}
public String lookupTableGray(int gray){
if (-1 < gray && gray < 40)
{
return "@";
}
else if (40 < gray && gray < 60)
{
return "#";
}
else if (60 < gray && gray < 80)
{
return "%";
}
else if (80 < gray && gray < 100)
{
return "0";
}
else if (100 < gray && gray < 160)
{
return "*";
}
else if (160 < gray && gray < 210)
{
return "o";
}
else if (210 < gray && gray < 256)
{
return ".";
}else
{
return ".";
}
}
public String lookupTableBinary(int gray){
if (-1 < gray && gray < 170)
{
return "@";
}
else if (170 < gray && gray < 256)
{
return ".";
}
else
{
return "_";
}
}
public void writeToFile(String output) throws IOException
{
BufferedWriter out = new BufferedWriter(new FileWriter("asciitest.txt"));
out.write(output);
out.close();
}
public static void main(String[] args) {
File file = new File(args[0]);
new Visualizer(file);
}
}
No comments:
Post a Comment