p5py / p5

p5 is a Python package based on the core ideas of Processing.
https://p5.readthedocs.io
GNU General Public License v3.0
728 stars 120 forks source link

Using get() to obtain colour value of a pixel in image or sketch #338

Open ArayB opened 2 years ago

ArayB commented 2 years ago

In Processing you can use the get() method on an image, or the sketch to obtain the colour value of a particular pixel. This also works in p5js. In p5py you can access the colour value of a pixel in an image by indexing into it as an array e.g: color = image_name[10,10] but I can't see how you do this on the sketch to obtain the colour of a pixel.

It would be great if there was a get() method that could be called which would return the colour value of a pixel in the sketch. This would also return the colour value of a shape that was present in the sketch at that coordinate.

I've tried to create the same behaviour using the load_pixels() method but it doesn't appear to work as I would expect. If there is no image present in the sketch it just returns the background colour. It ignores any shapes drawn in the sketch and also clears them from the sketch.

As a very simple example of what I mean in Processing and p5js (in a draw() function) the following works:

background(220);
c = get(50, 50)

fill(255,255,255)
rect(0,0, 300, 300)

c2 = get(80, 80)
fill(c)
rect(10, 10, 50, 50)

fill(c2)
rect(15, 15, 30, 30)

Simply retrieving that background colour and then the white of a rectangle and drawing another rectangle with the appropriate colour. I can't see how to do this in p5py.

I can see the _get_pixel method in the image class so presumably adding a get() method that forwards on to that would be fairly straightforward. I don't know how it would work for the whole sketch though.

github-actions[bot] commented 2 years ago

Thank you for submitting your first issue to p5py