Skip to content

Instantly share code, notes, and snippets.

@volfegan
Last active January 27, 2024 13:57
Show Gist options
  • Save volfegan/3c713dd076d0c71f29d2a369061c6d07 to your computer and use it in GitHub Desktop.
Save volfegan/3c713dd076d0c71f29d2a369061c6d07 to your computer and use it in GitHub Desktop.
Maps an image into different ASCII characters gradients that sometimes form a flow
//References
//Font: http://laemeur.sdf.org/fonts/
//http://paulbourke.net/dataformats/asciiart/
//https://youtu.be/AGR3sfOq2qc?t=5911
//Related
//https://github.com/odditica/ProcessingStuff/blob/master/ItsTerminal/ItsTerminal.pde
//https://github.com/JaceyPenny/ASCIIToImage/blob/master/Main.java
//ASCII gradient references
//"@MBHENR#KWXDFPQASUZbdehx*8Gm&04LOVYkpq5Tagns69owz$CIu23Jcfry%1v7l+it[] {}?j|()=~!-/<>\\\"^_';,:`. "
//"$@B%8&WMZO0QLCJUYXzcvunxrjft#*/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
//"@%#*+=-:. "
String[] asciiGradient = {
"MG@%8#0n?y1!li*<)+=~-;:,'..` ",
"$@%8&0#qynf?!i{])*+=~/-;:,'. ",
"G@%8O#m!1otfi*+\\/=<>~-;:',. ",
"N@H%#mn?xci*+)(=^<~-;:,.''` ",
"M@Q%0#x7t{*+|(=<\"/~-;:,..'` ",
"MND@%8$7x1tl?i*+=>/~-^;:_,'. "
};
float charSize = 16;
int variant = 0;//index for the ascii gradient array
PImage img;
PFont fontDOS;
String filename;
//there is no file validation, so any non-img selected will crash the program
void fileSelected(File selection) {
if (selection == null) {
println("No image file selected.");
exit();
} else {
String filepath = selection.getAbsolutePath();
filename = selection.getName();
int pos = filename.lastIndexOf(".");
if (pos != -1) filename = filename.substring(0, pos); //remove extension
println("File selected " + filepath);
// load file here
img = loadImage(filepath);
}
}
void interrupt() {
while (img==null) delay(200);
}
void settings() {
selectInput("Select an image file to process:", "fileSelected");
interrupt(); //interrupt process until img is selected
//for testing
//img = loadImage("cat.jpg");
width = img.width;
height = img.height;
//the canvas window size will be according to the img size
//but if the img is bigger than the screen display, it will be resized it to 80% of display
if (width > displayWidth) {
float resizer = width / (displayWidth * 0.8);
width = (int)((float)displayWidth * 0.8);
height = (int)((float)height / resizer);
img.resize(width, height);
}
if (height > displayHeight) {
float resizer = height / (displayHeight * 0.8);
height = (int)((float)displayHeight * 0.8);
width = (int)((float)width / resizer);
img.resize(width, height);
}
size(width, height, P2D);
println("w="+img.width+", h="+img.height);
}
void setup() {
fontDOS = createFont("MorePerfectDOSVGA.ttf", charSize);
textFont(fontDOS);
//PFont font = createFont("Ubuntu Mono", charSize);
//textFont(font);
noStroke();
fill(0, 255, 0);
textSize(charSize);
image(img, 0, 0);
}
void draw() {
clear();
//change index for ASCII gradient variants
if (frameCount%10==0) {
variant++;
variant%=asciiGradient.length;
}
img.loadPixels();
for (int x=0; x < width; x+=charSize) {
for (int y=0; y < height; y+=charSize) {
int index = x+y*width;
int pixel = img.pixels[index];
float lux = brightness(pixel);
if (lux > 150) fill(0, lux, 0);
else fill(0, 150, 0);
//fill(color(pixel));//colour fonts
int charIndex = (int)map(lux, 0, 255, asciiGradient[variant].length()-1, 0);
char ascii = asciiGradient[variant].charAt(charIndex);
text(ascii, x, y);
}
}
//show image with some minor transparency
tint(255, 15);
image(img, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment