Processing/JS

Ejemplos 2

ASCII image

PFont dFont;
PImage photo;
boolean colored = false;
int scale = 1; // 2 = 2x as big as input
int fSz = 10*scale, vwidth, vheight;
void setup() {
dFont = loadFont("ArialMT-12.vlw");
photo = loadImage("isl.JPG");
size((int)photo.width*scale, (int)photo.height*scale);
vwidth = photo.width; vheight = photo.height;
textFont(dFont,fSz);
background(255);
drawBendyLetters(photo, 10);
save("letterPhoto.jpg");
}
void draw() {
}

void drawLetters(PImage photo, int spacing)
{
for(int j = 0; j < photo.height; j+=spacing) {
for(int i = 0; i < photo.width; i+=spacing) {
PImage segment = photo.get(i, j, spacing, spacing);
color averageColor = getAreaColor(segment);
if(!colored) fill(0);
else fill(averageColor);
// Draw text
String letter = determineLetter((int)brightness(averageColor), false);
text(letter, i*scale, j*scale);
}
}
}

void drawBendyLetters(PImage photo, int spacing) {
// Set up noise and pre positioning variables
int dmod = 100; // displacement modifier
noiseSeed(1227865);
float noiseOffset = 0.003, angle;
int prexpo, preypo;
int xpo = 1, ypo = 1;
// Loop to create new grid
for(int x=0; x<width; x+=spacing) {
for(int y=0; y<height; y+=spacing) {
// Calculate noisey grid
float noise = noise(x*noiseOffset, y*noiseOffset);
float noiseX = (noise(x*noiseOffset)+noise)/2;
float noiseY = (noise(y*noiseOffset)+noise)/2;
prexpo = xpo; preypo = ypo; // store previous position
xpo = (int)(x+(noiseX*dmod)-dmod/2);
ypo = (int)(y+(noiseY*dmod)-dmod/2);
angle = atan((float)(xpo-prexpo)/(float)(ypo-preypo));
// Start individual letter transformation
pushMatrix();
translate(xpo*scale, ypo*scale);
rotate(angle);
// Decide on letter to use
// Cap index values
if(xpo < 0) xpo = 0; else if(xpo > photo.width-1) xpo = photo.width-1;
if(ypo < 0) ypo = 0; else if(ypo > photo.height-1) ypo = photo.height-1;
PImage segment = photo.get(xpo, ypo, spacing, spacing);
color averageColor = getAreaColor(segment);
if(!colored) fill(0);
else fill(averageColor);
// Draw letter
String letter = determineLetter((int)brightness(averageColor), false);
text(letter, -fSz/2, -fSz/2);
popMatrix();
}
}
}String determineLetter(int b, boolean inv) {
final String[] barr = {
// This array is ordered by characters in density, provided by BSR
".","'","`",",","^",":","\"",";","~","-","_","+","<",">","i","!","l","I","?","/","\\","|","(",")","1","{","}","[","]","r","c","v","u","n","x","z","j","f","t","L","C","J","U","Y","X","Z","O","0","Q","o","a","h","k","b","d","p","q","w","m","*","W","M","B","8","&","%","$","#","@",
};
float bdiv = 255/(barr.length);
int bres;
if(inv) {
bres = (int)(b/bdiv);
} else {
bres = (barr.length-1)-(int)(b/bdiv);
}
if(bres >= barr.length) bres = barr.length-1;
if(bres < 0) bres = 0;
return barr[bres];
}

int getAreaBrightness(color [] src, int x, int y, int sz)
{
int bval = 0, count = 1;
int xw = x+(sz*(vwidth/vheight)), yh = y+sz;

for(int i = x; i < xw; i++)
{
for(int j = y; j < yh; j++)
{
bval += brightness(src[j*vwidth+i]);
count++;
}
}
return (int)(bval/count);
}

color getAreaColor(PImage area)
{
int red = 0, green = 0, blue = 0;
int size = area.width*area.height;

// Select values from the specified grid (x, y coord, length)
for(int i = 0; i < area.width; i++)
{
for(int j = 0; j < area.height; j++)
{
color check = area.get(i, j);
red += red(check);
green += green(check);
blue += blue(check);
}
}
color ret = color(red/size, green/size, blue/size);
return ret;
}

void keyPressed() {
if(key == 'c' || key == 'C')
{
colored = !colored;
}
}

C