pythoncolor-detection

Can I search for black pixels in a photo and find out where they are in the photo using python?


I want to search for black pixels in a screenshot I took using pyautogui and I want to find the x and y location of those pixels using python so that I can move the mouse to the black pixels locations using pynput. I tried using imageio but I could not find a command that would do what I want. I asked this a few hours Ago but it was closed so I made the necessary edits to it.


Solution

  • Here is one way in Python/OpenCV/Numpy using np.argwhere on a thresholded image that isolates the black spots.

    Input (4 black clusters near the 4 corners):

    enter image description here

    import cv2
    import numpy as np
    
    # read input
    img = cv2.imread("lena_black_spots.png")
    
    low = (0,0,0)
    high = (0,0,0)
    
    mask = cv2.inRange(img, low, high)
    mask = 255 - mask
    
    # find black coordinates
    coords = np.argwhere(mask==0)
    for p in coords:
        pt = (p[0],p[1])
        print (pt)
    
    # save output
    cv2.imwrite('lena_black_spots_mask.png', mask)
    
    cv2.imshow('img', img)
    cv2.imshow('mask', mask)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    


    Mask:

    enter image description here

    Coordinates:

    (18, 218)
    (18, 219)
    (19, 218)
    (19, 219)
    (20, 218)
    (20, 219)
    (38, 21)
    (38, 22)
    (39, 21)
    (39, 22)
    (173, 244)
    (173, 245)
    (174, 244)
    (174, 245)
    (194, 23)
    (194, 24)
    (195, 23)
    (195, 24)