I am currently creating a PianoTiles AI, that has to locate all the black pixels from an ImageGrab. I have got all the positions of the Image Grab however I need to find out if there are black pixels in there and if so where they are so my AI can click them. Bellow I have put a snip-it of my code.
I have already had a look around the web but cant find anything. I think that the code goes something like this.
from PIL import ImageGrab, ImageOps
class Coordinates:
lines = [
(520, 300, 525, 760),
(630, 300, 635, 760),
(740, 300, 745, 760),
(850, 300, 855, 760)]
restartcheck = (660, 590, 725, 645)
restartbtn = (695, 615)
blackpixelpositions = []
def findtiles():
for line in Coordinates.lines:
i = ImageGrab.grab(line)
for pixel in i.getdata():
#if pixel is black
# x, y = pixel position
blackpixelpositions.append((x,y))
All I need is the above code to work and give me the black pixel positions.
You should try and avoid looping over images and using functions such as
getpixel()to access each pixel as it is really slow - especially for large images if you are grabbing modern 4-5k screens.It is generally better to convert your PIL image to a Numpy array and then use vectorised Numpy routines to process your images. So, in concrete terms, let's say you get a PIL image either by screen-grabbing or opening a file:
you can then make a Numpy array from the image like this:
and search for black pixels like this:
which will give you an array of
xcoordinates and an array ofycoordinates of the black pixels, e.g. you could do: