Created
September 26, 2013 14:29
converter gif to ascii art: http://ww3.sinaimg.cn/large/922b5b03jw1e909p4aicpg20nc0dw1kx.gif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gifAnimation.*; | |
PFont font; | |
GifMaker gifExport; | |
PImage[] frames; | |
PImage img; | |
void setup() { | |
gifExport = new GifMaker(this, "export.gif"); | |
gifExport.setRepeat(0); | |
frames = Gif.getPImages(this, "20131261156469027_asqql.com.gif"); | |
// frames = new PImage[1]; | |
// frames[0] = loadImage("image_weibo.jpeg"); | |
img = frames[0]; | |
size(img.width*5, img.height*5); | |
font = loadFont("Arial-Black-12.vlw"); | |
textFont(font, 12); | |
fill(0); | |
createFontGrayscaleTable(); | |
// frameRate(2); | |
//noLoop(); | |
} | |
void draw() { | |
if (frameCount<=frames.length) { | |
background(255); | |
img = frames[frameCount-1]; | |
praseImage(); | |
gifExport.addFrame(); | |
gifExport.setDelay(100); | |
} | |
else { | |
noLoop(); | |
gifExport.finish(); | |
} | |
} | |
float[] fontGrayscale; | |
char[] fontColor; | |
int chars; | |
PGraphics character; | |
void createFontGrayscaleTable() { | |
chars = '~'-' '+1; | |
fontGrayscale = new float[chars]; | |
fontColor = new char[chars]; | |
character = createGraphics(12, 12); | |
for (int i=' '; i<='~'; i++) { | |
character.beginDraw(); | |
character.background(255); | |
character.fill(0); | |
character.textAlign(CENTER, BOTTOM); | |
character.text((char)i, 6, 12); | |
int count = 0; | |
for (int x=0; x<12; x++) { | |
for (int y=0; y<12; y++) { | |
count += 255-brightness(character.get(x, y)); | |
} | |
} | |
character.endDraw(); | |
fontGrayscale[i-' '] = count/(12*12.0*255); | |
fontColor[i-' '] = (char)i; | |
} | |
for (int i=0; i<chars-1; i++) { | |
for (int j=i; j<chars; j++) { | |
if (fontGrayscale[j]<fontGrayscale[i]) { | |
float tmpg = fontGrayscale[j]; | |
fontGrayscale[j] = fontGrayscale[i]; | |
fontGrayscale[i] = tmpg; | |
char tmpc = fontColor[j]; | |
fontColor[j] = fontColor[i]; | |
fontColor[i] = tmpc; | |
} | |
} | |
} | |
for (int i=0; i<chars; i++) | |
println("char:"+fontColor[i]+" grayscale:"+fontGrayscale[i]); | |
} | |
void praseImage() { | |
textAlign(CENTER, BOTTOM); | |
for (int i=0; i<img.width; i+=2) { | |
for (int j=0; j<img.height; j+=2) { | |
// float grayscale = 255 - brightness(img.get(i,j)); | |
float grayscale = 0; | |
color cr = color(0,0,0); | |
for (int x=0; x<2; x++) { | |
for (int y=0; y<2; y++) { | |
grayscale += 255-brightness(img.get(i+x, j+y)); | |
cr = img.get(i,j); | |
} | |
} | |
grayscale /= 2*2; | |
fill(cr); | |
int fc = (int)(grayscale/256*chars); | |
char c = fontColor[fc]; | |
text(c, i*5+5, j*5+10); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment