-
Notifications
You must be signed in to change notification settings - Fork 41
/
PosterizeAndDownsampleImage.pde
67 lines (52 loc) · 1.61 KB
/
PosterizeAndDownsampleImage.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
/*
POSTERIZE and DOWNSAMPLE IMAGE
Jeff Thompson
February 2012
Divides the image's color into discreet steps using the pixel's brightness.
www.jeffreythompson.org
*/
String filename = "StarryNight.jpg";
PImage p;
void setup() {
// load image and set size of screen to same
p = loadImage(filename); // load
size(700,477);
colorMode(HSB, 255); // allows us to access the brightness of a color
// draw image to screen and access it's pixel values
image(p, 0, 0);
pixelateImage(80); // argument is resulting pixel size
posterizeImage(20); // argument is the step size for posterization
// save("PixelatedAndPosterized_" + filename);
}
void pixelateImage(int pxSize) {
// use ratio of height/width...
float ratio;
if (width < height) {
ratio = (float) height/width;
}
else {
ratio =(float) width/height;
}
println("Ratio: " + ratio);
// ... to set pixel height
int pxH = int(pxSize * ratio);
noStroke();
for (int x=0; x<width; x+=pxSize) {
for (int y=0; y<height; y+=pxH) {
fill(p.get(x, y));
rect(x, y, pxSize, pxH);
}
}
}
void posterizeImage(int rangeSize) {
// the built-in filter(POSTERIZE) works ok, but this is a bit more tweakable...
// iterate through the pixels one by one and posterize
loadPixels();
for (int i=0; i<pixels.length; i++) {
// divide the brightness by the range size (gets 0-rangeSize), then
// multiply by the rangeSize to step by that value; set the pixel!
int bright = int(brightness(pixels[i])/rangeSize) * rangeSize;
pixels[i] = color(bright);
}
updatePixels();
}