Colt/Code

Programming | Music | Art

Pixel Sorting Experiments Part 2

After my initial American Flag experiments, I decided to take on another iconic image: Van Gogh’s Starry Night. Here’s the image I started with:

I ended up sorting the pixels a bunch of different ways, but here are some of my favorites:

Sorted By Brightness

Though I was happy with this result, I decided to play around with smoothing the results out a bit. Here’s the result:

Sorted By Brightness With Smoothing

Here’s the code I used to do the smoothing:

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
color col = color(0,0,0);
float ar = 0;
float ag = 0;
float ab = 0;
  int counter = 0;
  for (int i = 0; i<sorted_colors.size(); i++){
    if(counter == width){
      ar = ar/width;
      ag = ag/width;
      ab = ab/width;
      counter = 0;
      int k = i - width;
      for(int j = k; j<i; j++){
        pixels[j] = color(ar,ag,ab);
      }
    }
    if(counter==0){
    ar = 0;
    ag = 0;
    ab = 0;
  }
    counter += 1;
    ar += sorted_colors.get(i).r;
    ag += sorted_colors.get(i).g;
    ab += sorted_colors.get(i).b;
  }

Here are a few more of my favorites:

Sorted By Hue

Sorted By Hue With Smoothing

Sorted By Green With Smoothing

Here’s the complete code:

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
class Col {
  float r;
  float g;
  float b;

   Col(float r_, float g_, float b_) {
     r = r_;
     g = g_;
     b = b_;
  }
}

size(900,476);
PImage img;
int max = (height -1) +(width-1) * width;
//int[] colors = new int[width*height];
color[] colors = new color[width*height];
ArrayList<Col> sorted_colors = new ArrayList<Col>();

  img = loadImage("starry.jpg");
  loadPixels();

  // We must also call loadPixels() on the PImage since we are going to read its pixels.
  img.loadPixels();
  for (int y = 0; y < height; y++ ) {
    for (int x = 0; x < width; x++ ) {
      int loc = x + y*width;
      // The functions red(), green(), and blue() pull out the three color components from a pixel.
      float r = red(img.pixels [loc]);
      float g = green(img.pixels[loc]);
      float b = blue(img.pixels[loc]);

      colors[loc] = color(r,g,b);

    }
  }


    for(int j = 0; j<colors.length; j++){
      float r = red(colors[j]);
      float h = int(saturation(colors[j]));
      float g = green(colors[j]);
      float b = blue(colors[j]);
        sorted_colors.add(new Col(r,g,b));
    }

color col = color(0,0,0);
float ar = 0;
float ag = 0;
float ab = 0;
  int counter = 0;
  for (int i = 0; i<sorted_colors.size(); i++){
    if(counter == width){
      ar = ar/width;
      ag = ag/width;
      ab = ab/width;
      counter = 0;
      int k = i - width;
      for(int j = k; j<i; j++){
        pixels[j] = color(ar,ag,ab);
      }
    }
    if(counter==0){
    ar = 0;
    ag = 0;
    ab = 0;
  }
    counter += 1;
    ar += sorted_colors.get(i).r;
    ag += sorted_colors.get(i).g;
    ab += sorted_colors.get(i).b;
  }

      updatePixels();