5

I am trying to take a screen shot, check the screen shot for a certain color, if the color is found then click on it.

The problem I'm having is that the RGB values of the color has to be exact.

I was wondering if it was possible to convert the image to an image with very few colors.

I'm sorry for the clutter. I am not properly trained. I am just obsessed with coding now.

Thank you for taking the time to read this.

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

How do I use "s.convert("P", palette=Image.ADAPTIVE, colors=5)" correctly so that I limit the range of colors to something like (0, 255, 0)?

2
  • Could you be more precise where is a problem and your question? Commented Apr 27, 2014 at 23:44
  • How do I use "s.convert("P", palette=Image.ADAPTIVE, colors=5)" correctly so that I limit the range of colors to something like (0, 255, 0)? Commented Apr 27, 2014 at 23:54

1 Answer 1

1

Instead of simplifying your color why don't you give range of RGB values when trying to find what color it is?

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

This would work around changing the RGB values of all the pixels.

Sign up to request clarification or add additional context in comments.

12 Comments

I know this is not exactly what you might want, but it is a work-around
Thank you. That is pretty much what I was looking for. I can remove the s.convert line now right?
"if s.getpixel(xy)== (range(0, 255), range(0, 255), range(0, 255)): break" That should make it break on any color right? Bah, this is challenging. =(
You can remove the s.convert line, and yes, it would make it break on every possible color.
For some reason it's not breaking. The code just runs... =(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.