Sorting out pixels by colour
in
Programming Questions
•
6 months ago
Dear Processers,
I am very new to Processing and coding in general. I am studying art in the Netherlands and started learning about the basics of processing in the past few weeks to include it into my art practice. So please bear with me.
What I would like to achieve is to make a programme that would take any image and sort all the pixels by color according to its value on the gradient, from white to black or the other way around.
Here is what I had in mind on how the programme could function: it takes the "whitest" pixel out of the image and stores its RGB value somewhere. There would be one empty pixel space anywhere on the image where the whitest pixel was. All the other pixels would be pushed "to the right" or +1. There would be one empty pixel space on position 1 (top-left). So you could place the stored whitest pixel on empty position 1. And so on and so on. This way you could see the process of pixel displacement.
I had in mind to sort them by hue but I figured that the black pixels and white pixels would be left out.
I had an attempt at coding something but I don't get to the result I want. Maybe my project is a bit too ambitious for a beginner...!
Looking forward to your precious help
- PImage source;
- PImage destination;
- void setup() {
- source = loadImage("F1000016.JPG");
- size(source.width, source.height);
- destination = createImage(source.width, source.height, RGB);
- }
- void draw() {
- color c;
- source.loadPixels();
- destination.loadPixels();
- for (int y = 0; y < source.height-1; y++) {
- for (int x = 0; x < source.width-1; x++) {
- int loc = x + y*source.width;
- int rightLoc = (x+1) + y*source.width;
- if (hue(source.pixels[loc]) > hue(source.pixels[rightLoc])) {
- c = source.pixels[loc];
- destination.pixels[loc] = source.pixels[rightLoc];
- destination.pixels[rightLoc] = c;
- }
- }
- }
- destination.updatePixels();
- image(destination, 0, 0);
- }
1