Skip to content

Instantly share code, notes, and snippets.

@mariuswatz
Last active August 29, 2015 14:04
Show Gist options
  • Save mariuswatz/916f9ac592d98422049b to your computer and use it in GitHub Desktop.
Save mariuswatz/916f9ac592d98422049b to your computer and use it in GitHub Desktop.
Extract dominant colors from a set of Emoji images, export to CSV
import toxi.color.Histogram;
import java.util.Iterator;
/**
* EmojiColorsCSV.pde - Marius Watz, 2014
* http://workshop.evolutionzone.com
*
* Download the file "Emoji.zip" from this URL:
* https://github.com/mariuswatz/teaching/blob/master/EmojiColorsCSV/Emoji.zip
*
* The archive must be unzipped inside the sketch folder before running, so that
* the images end up in a subfolder "Emoji".
*
* ---------------------------------------------
*
* Requires Toxiclibs: http://toxiclibs.org/
*
* Based on ImageColors.pde from Toxiclibs.
* Copyright (c) 2010 Karsten Schmidt
*
* Creating image based color palettes and color decimation through means
* of using a histogram.
*
* This variant shows how to extract dominant color from a set of emoji and
* save a list of those colors to a CSV file.
*/
import toxi.color.*;
import toxi.math.*;
// color tolerance
float tolerance=0.25;
// number of colors to save
int numColors=12;
void setup() {
size(520, 620);
load();
export("EmojiColors.csv");
}
void draw() {
background(255);
int index=0;
int offset=(int)map(mouseX, 0,width-1, 0,images.size()-50);
for(int i=0; i<100; i++) {
EmojiImg tmp=images.get((offset+i)%images.size());
tmp.draw(10+(i%10)*50,10+(i/10)*60, 40);
}
}
class EmojiImg {
PImage img;
ArrayList<Integer> col;
String code;
EmojiImg(String filename) {
code="u"+filename.substring(0, filename.length()-4);
img=loadImage(path+filename);
img.loadPixels();
Histogram hist=Histogram.newFromARGBArray(
img.pixels, img.pixels.length, tolerance, true);
col=new ArrayList<Integer>();
for (HistEntry e : hist.getEntries ()) {
col.add(e.getColor().toARGB());
}
}
void draw(int x,int y,int W) {
pushMatrix();
translate(x,y);
stroke(200);
fill(240);
rect(-5,-5,W+10,W+15);
image(img, 0,0,W,W);
int W2=W/numColors;
noStroke();
for(int i=0; i<min(numColors,col.size()); i++) {
fill(col.get(i));
rect(i*W2,W+5, W2,5);
}
popMatrix();
}
}
String path="C:/Users/marius/Dropbox/03 Code/StevenApp/data/Emoji/";
String files[];
ArrayList<EmojiImg> images;
// Loads all files from a given path and parses them into EmojiImg data
public void load() {
path=sketchPath+"/Emoji/";
files=new File(path).list();
int cnt=0;
images=new ArrayList<EmojiImg>();
for(String f : files) {
images.add(new EmojiImg(f));
if(cnt%50==0) println(cnt+"/"+files.length+" loaded.");
cnt++;
}
}
// Exports data from the ArrayList<EmojiImg> to a CSV tabular data format.
// numColors are saved as 6-digit hex strings, prefixed with "#" to avoid them
// being parsed as numbers in Excel or similar.
public void export(String csvFilename) {
Table tab=new Table();
tab.addColumn("emojicode", Table.STRING);
for(int i=0; i<numColors; i++) {
tab.addColumn("col"+i, Table.STRING);
}
for(EmojiImg tmp : images) {
TableRow row=tab.addRow();
row.setString(0,tmp.code);
for(int i=0; i<numColors; i++) {
int c=0xFFFFFFFF; // white will be used if not enough numColors
if(i<tmp.col.size()) c=tmp.col.get(i);
row.setString(i+1, "#"+toHex(c));
}
}
println("Saved '"+csvFilename+"' - "+images.size()+" entries.");
saveTable(tab, csvFilename);
}
String toHex(int col) {
String s="", tmp;
int a=(col >> 24) & 0xff;
// if(a<255) s+=strPad(Integer.toHexString(a),2,'0');
s+=strPad(Integer.toHexString((col>>16)&0xff), 2, '0');
s+=strPad(Integer.toHexString((col>>8)&0xff), 2, '0');
s+=strPad(Integer.toHexString((col)&0xff), 2, '0');
s=s.toUpperCase();
return s;
}
public String strPad(String s, int len, char c) {
len-=s.length();
while (len-->0) s+=c;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment