Skip to content

Instantly share code, notes, and snippets.

@antic-ml
Created September 11, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antic-ml/63af32781cbf5ef31feeb5eabc1dab8e to your computer and use it in GitHub Desktop.
Save antic-ml/63af32781cbf5ef31feeb5eabc1dab8e to your computer and use it in GitHub Desktop.
Creates a "binary digit mosaic" of a JPG file
/**
* Load a JPG image and convert it into a "binary digit mosaic"
*
* Author: Mario Gianota, September 2020
*/
PImage img; // Declare variable of type PImage
boolean once = false;
PFont myFont;
int x;
int y;
String one = "1";
String zero = "0";
boolean toggle = false;
void setup() {
size(1024, 768);
// The image file must be in the data folder of the current sketch
// to load successfully
img = loadImage("me4-64x80.jpg"); // Load the image into the program
// Grab the pixels
loadPixels();
myFont = createFont("Lucida Sans", 12);
textFont(myFont);
textAlign(CENTER, CENTER);
}
void draw() {
if( once )
return;
background(0);
for(int i=0; i<64; i++) {
for(int j=0; j<80; j++) {
color c = img.get(j, i);
fill(c);
text( toggle == true ? one : zero, x, y);
toggle = ! toggle;
x+= 8;
}
x = 0;
y += 10;
}
y=0;
saveFrame("me4-binary-mosaic.jpg");
once = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment