在屏幕上定位颜色位置?
Locate color position on screen?
我想用 pyautogui 单击屏幕上的特定颜色,但为此我需要它的位置,但我找不到有关该主题的任何有用信息。我正在尝试制作一个 Piano Tiles 自动点击器,为此我考虑过识别瓷砖的颜色并点击它。
你可以通过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
考虑制作较小区域的屏幕截图以更快地识别像素。
pyautogui.screenshot(region=(0,0, 300, 400))
参数是要捕获的区域的左、上、宽、高四元组。您甚至可以只抓取每个图块的一个像素以使其更好地工作。我不认为制作整个屏幕的屏幕截图是个好主意,尤其是当图块运行速度很快时。
我会怎么做:
- 使用
pyautogui.position()
获取出现方块的每个区域的一个像素的坐标(假设方块的颜色是纯色并且在游戏过程中不会改变)
- 使用
getpixel()
获取瓦片像素的RGB值
- 如果第 1 步中的坐标像素与第 2 步中获得的 RGB 值相同,则在循环中检查。
- 如果是,请致电
pyautogui.click()
这是我的第一个post! :) 我遇到了同样的问题,但我找到了解决方案。我的代码可能不遵循任何编程标准,但它正在运行哈哈哈!我 2 个月前在 Python 开始编程(20 年前的一些经验 (QBasic/C/Java),但从来没有任何专业知识)。请告诉我是否适合您,以及是否有任何我可以改进的地方。我希望我能在这方面帮助别人 post,因为这个网站在过去 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)
这是另一个计算区域中有多少像素的版本:
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)
我想用 pyautogui 单击屏幕上的特定颜色,但为此我需要它的位置,但我找不到有关该主题的任何有用信息。我正在尝试制作一个 Piano Tiles 自动点击器,为此我考虑过识别瓷砖的颜色并点击它。
你可以通过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
考虑制作较小区域的屏幕截图以更快地识别像素。
pyautogui.screenshot(region=(0,0, 300, 400))
参数是要捕获的区域的左、上、宽、高四元组。您甚至可以只抓取每个图块的一个像素以使其更好地工作。我不认为制作整个屏幕的屏幕截图是个好主意,尤其是当图块运行速度很快时。
我会怎么做:
- 使用
pyautogui.position()
获取出现方块的每个区域的一个像素的坐标(假设方块的颜色是纯色并且在游戏过程中不会改变) - 使用
getpixel()
获取瓦片像素的RGB值 - 如果第 1 步中的坐标像素与第 2 步中获得的 RGB 值相同,则在循环中检查。
- 如果是,请致电
pyautogui.click()
这是我的第一个post! :) 我遇到了同样的问题,但我找到了解决方案。我的代码可能不遵循任何编程标准,但它正在运行哈哈哈!我 2 个月前在 Python 开始编程(20 年前的一些经验 (QBasic/C/Java),但从来没有任何专业知识)。请告诉我是否适合您,以及是否有任何我可以改进的地方。我希望我能在这方面帮助别人 post,因为这个网站在过去 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)
这是另一个计算区域中有多少像素的版本:
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)