【问题标题】:Python : Detect Color Then Click ColorPython:检测颜色然后单击颜色
【发布时间】:2014-04-27 23:11:48
【问题描述】:

我正在尝试截屏,检查屏幕截图是否有某种颜色,如果找到该颜色,请单击它。

我遇到的问题是颜色的 RGB 值必须准确。

我想知道是否可以将图像转换为颜色很少的图像。

我很抱歉造成混乱。我没有受过适当的培训。我现在只是痴迷于编码。

感谢您抽出宝贵时间阅读本文。

import os, sys
import Image, ImageGrab, ImageOps
import time, random
from random import randrange
import win32api, win32con
from numpy import *


# Globals
# ------------------

x_pad = 0
y_pad = 0


# Screen Grab Function
def screenGrab():
    b1 = (x_pad + 1,y_pad+1,x_pad+1921,y_pad+1081)
    im = ImageGrab.grab()
    ##im.save(os.getcwd() + '\\Snap__' + str(int(time.time())) +'.png', 'PNG')
    return im

## Grab Mouse Position
## MousePos = win32api.GetCursorPos()
## print MousePos

## Type in shell to grab RGB color
## im = screenGrab()
## im.getpixel(MousePos)

## Check Mouse Position for Black
## s = screenGrab()
## if s.getpixel(MousePos) <= (96, 96, 96):
    ##print "I see black."   

# Main 

x = 920
y = 465

# Color Check Then Stop/Click Loop

while True:

    s = screenGrab()
    s.convert("P", palette=Image.ADAPTIVE, colors=5)
    x = x + 10
    xy = (x, y)
    if s.getpixel(xy)== (255, 255, 255):
        break
    else:
        win32api.SetCursorPos((x, y))
        print x
        print y

        if x == 1250:
            x = 700
            y = y + 10
            if y == 985:
                break

如何正确使用 "s.convert("P", palette=Image.ADAPTIVE, colors=5)" 以便将颜色范围限制为 (0, 255, 0)?

【问题讨论】:

  • 您能否更准确地说出问题在哪里以及您的问题?
  • 如何正确使用 "s.convert("P", palette=Image.ADAPTIVE, colors=5)" 以便将颜色范围限制为 (0, 255, 0 )?

标签: python colors


【解决方案1】:

除了简化你的颜色之外,为什么在试图找出它是什么颜色时不给出 RGB 值的范围?

(range(200,220), range(200,220), range(200,220))

这可以改变所有像素的 RGB 值。

【讨论】:

  • 我知道这可能不是您想要的,但它是一种解决方法
  • 谢谢。这几乎就是我想要的。我现在可以删除 s.convert 行了吗?
  • "if s.getpixel(xy)== (range(0, 255), range(0, 255), range(0, 255)): break" 这应该让它在任何颜色对吗?呸,这很有挑战性。 =(
  • 您可以删除 s.convert 行,是的,它会破坏所有可能的颜色。
  • 由于某种原因它没有坏掉。代码只是运行... =(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2011-11-22
  • 2014-05-13
相关资源
最近更新 更多