-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoCyic.pde
107 lines (88 loc) · 2.14 KB
/
moCyic.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
PImage mainImage;
PImage im;
PImage[] allImages;
int r;
int g;
int b;
int numpix;
color[] avgc;
int ww = 9600;
int hh = 8710;
int w;
int h;
int scale = 8;
void setup()
{
size(9600, 8710);
// size(600, 800); //sloth1
mainImage = loadImage("test1.png");
File[] files = listFiles(sketchPath("./images"));
allImages = new PImage[files.length/4];
avgc = new color[files.length/4];
for(int i = 0; i < allImages.length; i++)
{
allImages[i] = loadImage(files[i].toString());
avgc[i] = extractColorFromImage(allImages[i]);
print((double) i/allImages.length*100 + "\n");
}
w = mainImage.width/scale;
h = mainImage.height/scale;
im = createImage(w, h, RGB);
im.copy(mainImage, 0, 0, mainImage.width, mainImage.height, 0, 0, w, h);
}
void draw()
{
im.loadPixels();
for(int x = 0; x < w; x++)
{
for(int y = 0; y < h; y++)
{
int index = x + y * w;
color c = im.pixels[index];
int imin = 0;
float diffmin = 999999;
float diff = 0;
for(int i = 0; i < allImages.length; i++)
{
diff = abs(c - avgc[i]);
//(red(c)-red(avgc[i])) + (blue(c)-blue(avgc[i])) + (green(c)-green(avgc[i])));
if(diff < diffmin)
{
diffmin = diff;
imin = i;
}
}
image(allImages[imin],x*100, y*100, 100, 100);
//fill(c);
//noStroke();
//rect(x*scale, y*scale, scale, scale);
}
}
saveFrame();
//image(im, 0, 0);
noLoop();
}
color extractColorFromImage(PImage img) {
img.loadPixels();
int r = 0, g = 0, b = 0;
for (int i=0; i<img.pixels.length; i++) {
color c = img.pixels[i];
r += c>>16&0xFF;
g += c>>8&0xFF;
b += c&0xFF;
}
r /= img.pixels.length;
g /= img.pixels.length;
b /= img.pixels.length;
return color(r, g, b);
}
File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
}