-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixel_Mapper.pde
71 lines (59 loc) · 2.15 KB
/
Pixel_Mapper.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
import java.util.*;
PImage base;
PImage transfer;
int loc;
//Image mode = 0, Pallete mode = 1
//int mode = 0;
//RGB values for the two images
float []baseRGB = new float[3];
float []transferRGB = new float[3];
float []comboRGB = new float[3];
//Averaged RGB values
//Set to very large values initially so there is always something smaller
float rDist;
float gDist;
float bDist;
void setup() {
size(400, 400);
background(0);
base = loadImage("Insert Image 1 Here");
transfer = loadImage("Insert Image 2 Here");
transfer.resize(width, height);
base.resize(width, height); //<>//
}
void draw() {
loadPixels();
base.loadPixels();
transfer.loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
loc = x+y*width;
//RGB values for base image
baseRGB[0] = red(base.pixels[loc]);
baseRGB[1] = green(base.pixels[loc]);
baseRGB[2] = blue(base.pixels[loc]);
rDist = 9999;
gDist = 9999;
bDist = 9999;
//Loop through all the pixels in transfer and find the one that most closely matches the one in the base picture
for (int i = 0; i < transfer.pixels.length; i++) {
//RGB values for transfer image
transferRGB[0] = red(transfer.pixels[i]);
transferRGB[1] = green(transfer.pixels[i]);
transferRGB[2] = blue(transfer.pixels[i]);
//If the rgb values of one pixel are closer to that of the original, update the smallest distance and the corresponding rgb values
if (dist(transferRGB[0], 0, baseRGB[0], 0) < rDist && dist(transferRGB[1], 0, baseRGB[1], 0) < gDist && dist(transferRGB[2], 0, baseRGB[2], 0) < bDist) {
rDist = dist(transferRGB[0], 0, baseRGB[0], 0);
gDist = dist(transferRGB[1], 0, baseRGB[1], 0);
bDist = dist(transferRGB[2], 0, baseRGB[2], 0);
comboRGB[0] = transferRGB[0];
comboRGB[1] = transferRGB[1];
comboRGB[2] = transferRGB[2];
}
}
pixels[loc] = color(comboRGB[0], comboRGB[1], comboRGB[2]);
}
}
updatePixels();
save("product.jpg");
}