Image to characters
these are the characters used" -~=+*^<a#$%0ß@"
- = + =
* # # # # # ~
^ # * - ~ < # ~
~ # + = * - a <
^ a + # a # + #
a ^ a ^ - # + #
= * * - < ^ a ^ a <
- a # a # # + ^ # + # ^ * a # ~
< # ~ ^ # - ~ # + + a # < ~
~ # = ^ = # + # # +
+ # + a ~ # + - # # # # ^ +
= # = # ^ a # = # # # < a # # =
- # + * a < - a # + - < # =
^ # = - < # ~ + < ^ ~ < #
^ # a ^ < # # ~ ^ # < a # - ~ # ~
~ ^ < < + ~ # = # * # +
+ # * ^ # = - # =
= # - - < * + # -
# < - = # ^
~ # # a a # <
+ ^ * ~
Code
PImage source;
int s = 30,w,h;
char chr[] = (" -~=+*^<a#$%0ß@").toCharArray(); //char chr[] = (" °.-~=+*^>a#$%0ß@").toCharArray();
boolean invert = false;
void setup() {
size(640, 600);
w = width/s;
h = height/s;
source = loadImage("data/source.png");
textSize(s);
}
void draw() {
background(255);
image(source,0,0);
noStroke();
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
float val = getAvg(get(i*s,j*s,s,s));
fill(val);
rect(i*s, j*s, s, s);
char replace = (( (invert)? (chr[floor(map(val,255,0,0,chr.length-0.01))]) : (chr[floor(map(val,0,255,0,chr.length-0.01))])));
print(replace+" ");
}
println();
}
noLoop();
}
float getValue(char input) {
PGraphics temp = createGraphics(s, s);
temp.beginDraw();
temp.background(0);
temp.textSize(s);
temp.textAlign(3, 3);
temp.fill(0);
temp.text(input, s*0.5, s*0.5);
temp.endDraw();
return getAvg(temp);
}
float getAvg(PImage input) {
input.filter(GRAY);
float sum = 0, imgW = input.width, imgH = input.height;
for (int i = 0; i < imgW; i++) for (int j = 0; j < imgH; j++) sum += red(input.get(i, j));
float avg = sum/(imgW*imgH);
return (avg);
}
Instructions - create a “source.png” inside of the sketches data folder to use.