Last active
September 28, 2015 03:38
-
-
Save aeurielesn/1378443 to your computer and use it in GitHub Desktop.
spmacdonald's dice portrait
This file contains hidden or 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
# http://web.archive.org/web/20091015013516/http://www.elusivesnark.com/2008/11/carolines-dice-portrait.html | |
PImage img; | |
PImage cropped_img; | |
PImage patch; | |
int _width = 640; | |
int _height = 1024; | |
int patch_w = 16; | |
int patch_h = 16; | |
int pip_size = 4; | |
void setup() { | |
size(_width, _height); | |
img = loadImage("Caroline.jpg"); | |
img.resize(1536, 1024); | |
cropped_img = img.get(250, 0, _width, _height); | |
} | |
// Draw quantized image | |
void draw() { | |
// Array to hold the gray values of a patch | |
int[] x = new int[patch_w * patch_h]; | |
// Quantize the image | |
for (int i=0; i < (_width/patch_w); ++i) { | |
for (int j=0; j < (_height/patch_h); ++j) { | |
patch = cropped_img.get(i*patch_w, | |
j*patch_h, | |
patch_w, | |
patch_h); | |
patch.loadPixels(); | |
for (int k=0; k < patch.pixels.length; ++k) { | |
x[k] = rgb2gray(patch.pixels[k]); | |
} | |
int dice_num = six_step_gray(mean(x)); | |
if (dice_num == 1) draw_one(i, j); | |
if (dice_num == 2) draw_two(i, j); | |
if (dice_num == 3) draw_three(i, j); | |
if (dice_num == 4) draw_four(i, j); | |
if (dice_num == 5) draw_five(i, j); | |
if (dice_num == 6) draw_six(i, j); | |
} | |
} | |
} | |
void draw_one(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+8, j*patch_h+8, pip_size, pip_size); | |
} | |
void draw_two(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+3, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+13, pip_size, pip_size); | |
} | |
void draw_three(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+3, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+8, j*patch_h+8, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+13, pip_size, pip_size); | |
} | |
void draw_four(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+3, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+3, j*patch_h+13, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+13, pip_size, pip_size); | |
} | |
void draw_five(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+3, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+3, j*patch_h+13, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+13, pip_size, pip_size); | |
ellipse(i*patch_w+8, j*patch_h+8, pip_size, pip_size); | |
} | |
void draw_six(int i, int j) { | |
fill(0); | |
rect(i*patch_w, j*patch_h, 16, 16); | |
fill(255); | |
ellipse(i*patch_w+3, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+3, j*patch_h+8, pip_size, pip_size); | |
ellipse(i*patch_w+3, j*patch_h+13, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+3, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+8, pip_size, pip_size); | |
ellipse(i*patch_w+13, j*patch_h+13, pip_size, pip_size); | |
} | |
// Impose 6 step gray palette | |
int six_step_gray(int x) { | |
if (0 <= x && x <= 41) | |
return 1; | |
if (41 < x && x <= 83) | |
return 2; | |
if (83 < x && x <= 124) | |
return 3; | |
if (124 < x && x <= 165) | |
return 4; | |
if (165 < x && x <= 206) | |
return 5; | |
if (x < 206 && x <= 247) | |
return 6; | |
else | |
return 6; | |
} | |
// Blend rgb channels | |
int rgb2gray(int argb) { | |
int _alpha = (argb >> 24) & 0xFF; | |
int _red = (argb >> 16) & 0xFF; | |
int _green = (argb >> 8 ) & 0xFF; | |
int _blue = (argb) & 0xFF; | |
return int(0.3*_red + 0.59*_green + 0.11*_blue); | |
} | |
// Calculates the average of an integer array | |
int mean(int[] x) { | |
float m = 0; | |
for (int i=0; i < x.length; ++i) { | |
m += x[i]; | |
} | |
m = m/x.length; | |
return int(m); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment