问题
I am making a bot in python. I want to compare the colors of a particular pixel with another color which is (83, 83, 83).
I tried comparing with a string with single and double quotes. It didn't work so I thought that it could be an array.
This is my code of the bot
import pyautogui as py
from PIL import ImageGrab
def pressspace():
py.keyDown('space')
py.keyUp('space')
def jump():
px=ImageGrab.grab().load()
color=px[207,445]
if color=='(83, 83, 83)':
pressspace()
while True:
jump()
It just didn't work and didn't press the space. I have imported all dependencies also. Please Help and tell that is it an array and if yes, than how to compare.(Note: rest time the color is (247, 247, 247))
回答1:
Keep in mind you didn't state what 'py' in pressspace() is and does for your code snippet.
import sys, time
from PIL import ImageGrab
def pressspace():
py.keyDown('space')
py.keyUp('space')
def jump():
px=ImageGrab.grab().load()
color=px[207,445]
c1, c2, c3 = color # just a thought: if included you can compare and print each
# of them to see if they fit a certain value of your liking.
if color==(83, 83, 83):
print ('1 - type: ', type(color))
else:
print ('2 - type: ', type(color))
print (color) # just to print always the color
time.sleep(1) # pause it for one second to prevent SPAM in the output.
# pressspace()
while True:
jump()
sys.stdout.flush() # forces to print directly the result from within an editor if used.
In my case its an <class 'tuple'>
来源:https://stackoverflow.com/questions/56635871/is-rgb-taken-from-imagegrab-grab-load-is-in-array-or-string