我的程序试图在屏幕上找到特定的颜色,但是 returns 一个元组错误 [Python3]
My program tries to find a specific color on the screen but returns a tuple error [Python3]
我正在尝试使用 python 完成 this 游戏,我是初学者,谁能解释一下为什么我的代码无法运行?
这是部分代码:
box = (x_pad+1, y_pad+1, x_pad+731, y_pad+550)
im = ImageGrab.grab(bbox=box)
im.save('/Users/CENSORED/full_snap__.png')
colour = (58, 15, 8)
img = Image.open('/Users/CENSORED/full_snap__.png')
rgb_img = img.convert('RGB')
for x in range(rgb_img.size()[0]):
for y in range(rgb_img.size()[1]):
r, g, b = rgb_img.getpixel((x, y))
if (r,g,b) == colour:
print('found image at {x}, {y}')
pyautogui.click(x,y)
time.sleep(.1)
这是错误:
File "/Users/CENSORED/Documents/Testing/gamecrusher.py", line 32, in <module>
for x in range(rgb_img.size()[0]):
TypeError: 'tuple' object is not callable
问题是rgb_img.size
是一个元组,不是一个可调用的方法,所以你不要在它后面加上括号。你想要:
for x in range(rgb_img.size[0]):
y
.
也是如此
我正在尝试使用 python 完成 this 游戏,我是初学者,谁能解释一下为什么我的代码无法运行?
这是部分代码:
box = (x_pad+1, y_pad+1, x_pad+731, y_pad+550)
im = ImageGrab.grab(bbox=box)
im.save('/Users/CENSORED/full_snap__.png')
colour = (58, 15, 8)
img = Image.open('/Users/CENSORED/full_snap__.png')
rgb_img = img.convert('RGB')
for x in range(rgb_img.size()[0]):
for y in range(rgb_img.size()[1]):
r, g, b = rgb_img.getpixel((x, y))
if (r,g,b) == colour:
print('found image at {x}, {y}')
pyautogui.click(x,y)
time.sleep(.1)
这是错误:
File "/Users/CENSORED/Documents/Testing/gamecrusher.py", line 32, in <module>
for x in range(rgb_img.size()[0]):
TypeError: 'tuple' object is not callable
问题是rgb_img.size
是一个元组,不是一个可调用的方法,所以你不要在它后面加上括号。你想要:
for x in range(rgb_img.size[0]):
y
.