【问题标题】:Is RGB taken from ImageGrab.grab().load() is in array or stringRGB 取自 ImageGrab.grab().load() 是在数组还是字符串中
【发布时间】:2019-10-31 07:35:50
【问题描述】:

我正在用 python 制作一个机器人。我想将特定像素的颜色与另一种颜色 (83, 83, 83) 进行比较。

我尝试与带有单引号和双引号的字符串进行比较。它不起作用,所以我认为它可能是一个数组。

这是我的机器人代码

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()

它只是没有用,也没有按空格。我也导入了所有依赖项。请帮忙告诉它是不是一个数组,如果是,比如何比较。(注意:休息时间颜色为(247、247、247))

【问题讨论】:

标签: python arrays python-3.x image pixel


【解决方案1】:

请记住,您没有说明 pressspace() 中的“py”是什么以及对您的代码 sn-p 的作用。

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.

就我而言,它是<class 'tuple'>

【讨论】:

  • 如果是元组,为什么要和字符串比较?
  • 看到 OP 做到了...确实解决了这个问题 ;-)
  • 只是一个想法:添加了 c1,c2,c3 以向您表明,如果您想这样做,实际上可以从那里检查每个单独的像素值。没有包含实际的选择代码,但你得到了图片;p
  • 对不起,我现在知道你回答的全部意思了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多