【问题标题】:Locate color position on screen?在屏幕上定位颜色位置?
【发布时间】:2021-12-14 18:16:15
【问题描述】:

我想使用 pyautogui 在屏幕上单击特定颜色,但为此我需要它的位置,但我找不到有关该主题的任何有用信息。我正在尝试制作钢琴瓷砖自动点击器,为此我考虑过识别瓷砖的颜色并点击它。

【问题讨论】:

    标签: python python-imaging-library cv2 pyautogui


    【解决方案1】:

    你可以用pyautogui找到颜色位置:

    import pyautogui
    
    color = (255, 255, 255)
    
    s = pyautogui.screenshot()
    for x in range(s.width):
        for y in range(s.height):
            if s.getpixel((x, y)) == color:
                pyautogui.click(x, y)  # do something here
    

    【讨论】:

      【解决方案2】:

      考虑制作更小区域的屏幕截图以更快地识别像素。

      pyautogui.screenshot(region=(0,0, 300, 400))
      

      参数是要捕获的区域的左、上、宽和高的四整数元组。您甚至可以只抓取每个图块的一个像素以使其更好地工作。我不认为制作整个屏幕的屏幕截图是一个好主意,尤其是当瓷砖快速移动时。

      我会怎么做:

      1. 使用pyautogui.position() 获取出现图块的每个区域的一个像素的坐标(假设图块的颜色是纯色并且在游戏过程中没有变化)
      2. 使用getpixel()获取瓦片像素的RGB值
      3. 如果具有步骤 1 中坐标的像素具有与步骤 2 中获得的相同的 RGB 值,则在循环中检查。
      4. 如果是,请致电pyautogui.click()

      【讨论】:

        【解决方案3】:

        这是我的第一篇文章! :) 我有同样的问题,但我找到了解决方案。我的代码可能没有遵循任何编程标准,但它正在工作哈哈哈!我在 2 个月前开始使用 Python 编程(20 年前有一些经验(QBasic/C/Java),但从来没有任何专业知识)。请告诉我是否对您有用,以及是否有任何可以改进的地方。我希望我能在这篇文章中帮助别人,因为这个网站在过去 2 个月里帮助了我很多!

        def checkForRGBValues(start=(0,0), end=(50,50), R=255, G=255, B=255):
        
            x = int(end[0]-start[0] + 1)
            y = int(end[1]-start[1] + 1)
            # print(x)
            # print(y)
            for x in range(start[0], end[0] + 1, 1):
                for y in range(start[1], end[1] + 1, 1):
                    print(str(x) + "/" + str(y))
        
                    if pyautogui.pixel(x, y)[0] == R and pyautogui.pixel(x, y)[1] == G and pyautogui.pixel(x, y)[2] == B:
                        print("Color found")
                        with open("color_found.txt", "a") as file_found:
                            file_found.write(str(x) + "/" + str(y))
                            file_found.write("\n")
                    else:
        
                        with open("color_not_found.txt", "a") as file:
                            file.write(str(x) + "/" + str(y))
                            file.write("\n")
        
                    y = y + 1
                x = x + 1
        
        
        
        checkForRGBValues((150,200), (200,250), R=255, G=0, B=0) #if nothing (0,0), (50,50)
        

        【讨论】:

        • 我刚刚注意到代码部分没有两行,对此我很抱歉!
        【解决方案4】:

        这是另一个计算区域中有多少像素的版本:

        import pyautogui
        
        def checkForRGBValues(start=(0,0), end=(50,50), R=255, G=255, B=255): #start/end/r/g/b value, I put some standard values for testing
        
            x = int(end[0]-start[0] + 1) #calculates x value between start and end
            y = int(end[1]-start[1] + 1)  #calculates y value between start and end
            how_many_pixels_found = 0 #nothing found yet 
            for x in range(start[0], end[0] + 1, 1): #loops through x value
                for y in range(start[1], end[1] + 1, 1): #loops through y value
                    if pyautogui.pixel(x, y)[0] == R and pyautogui.pixel(x, y)[1] == G and pyautogui.pixel(x, y)[2] == B: #checks if the wanted RGB value is in the region
                        how_many_pixels_found = how_many_pixels_found + 1 #adds one to the result
        
                    y = y + 1
                x = x + 1
            return how_many_pixels_found #returns the total value of pixels with the wanted color in the region.
        
        x = checkForRGBValues((150,200), (200,250), R=60, G=63, B=65)
        print(x)
        

        【讨论】:

        • 请解释你的代码是做什么的以及它是怎么做的。
        • 对不起!我刚刚添加了 cmets!我应该在发帖之前考虑到这一点:-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多