Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
For Code and Coffee. Can take any ascii art text file, and create a huge image of random colors representing that Ascii art
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
public class Convert {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
//Welcome The User!
System.out.println("Hello! Welcome to the ASCII Art to art converter! Select a text file!");
System.out.println();
//Our two color values
//Color black = new Color(255, 255, 255, 255);
Color trans = new Color(255, 255, 255, 0);
//Chose the input file
// Open our file chooser
File selectedFile = null;
File outputfile = null;
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
// Get thefile
selectedFile = chooser.getSelectedFile();
//Output the file
outputfile = new File(selectedFile.getParentFile().getAbsolutePath() + "/image.jpg");
outputfile.createNewFile();
}
//Create a scanner for reading the art dimensions
Scanner dimensions = new Scanner(selectedFile);
//Get the image dimensions
int imageX = 0;
int imageY = 0;
while(dimensions.hasNextLine())
{
//Increase Y
imageY++;
//Find max X
String line = dimensions.nextLine();
if(imageX < line.length()) imageX = line.length();
}
//Create the image
BufferedImage image = new BufferedImage(imageX, imageY, BufferedImage.TYPE_INT_ARGB);
//close the scannre and create a new to start drawing
dimensions.close();
Scanner user = new Scanner(selectedFile);
//Loop through the lines
for(int y = 0; user.hasNextLine(); ++y)
{
//get the line
String ascii = user.nextLine();
//Now loop through all of the characters
for(int x = 0; x < ascii.length(); ++x)
{
//check the character, white space is transparent, character is black
if(ascii.charAt(x) == ' ') {
image.setRGB(x, y, trans.getRGB());
}
else {
image.setRGB(x, y, getRandomColor());
}
}
}
//Resize our image to something bigger
double scaleAmount = 40.0;
int newWidth = new Double(image.getWidth() * scaleAmount).intValue();
int newHeight = new Double(image.getHeight() * scaleAmount).intValue();
BufferedImage after = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.scale(scaleAmount, scaleAmount);
AffineTransformOp scaleOp =
new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
after = scaleOp.filter(image, after);
//close the scanner, write image
user.close();
ImageIO.write(after, "png", outputfile);
//Welcome The User!
System.out.println();
System.out.println("Image saved! Have a nice day!");
}
//Function to return a random color
public static int getRandomColor() {
//Create a random
Random rand = new Random();
//Create a random color
Color random = new Color(rand.nextInt(255) + 1, rand.nextInt(255) + 1, rand.nextInt(255) + 1, 255);
//return is
return random.getRGB();
}
}
@torch2424
Copy link
Author

torch2424 commented Nov 14, 2015

Examples:

image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment