Skip to content
disfated edited this page Apr 16, 2011 · 1 revision

Screen abstraction layer.

Common

  • Most methods accept input arguments in several formats:

    • points can be passed as do(x, y) or do(point), where point is a tuple / list of x and y.

    • boxes can be passed as do(left, top, right, bottom) or do(from_point, to_point) or do(rect), where rect is a tuple / list of left, top, right and bottom.

  • Images returned by methods are instances of Image class.

Supported platforms:

  • Windows - all methods (need tests) (requires: PIL, ctypes);

  • Unix - all methods (requires: gtk, PIL, Xlib);

  • Mac - not supported;

Examples

from libs import screen

# dimensions:
print screen.size       # > (1440, 900)
print screen.width      # > 1440
print screen.height     # > 900
print screen.center     # > (720, 450)

# retrieving data from screen:
print screen.pixel(100, 100)      # > (150, 191, 226)
c = screen.center
print screen.pixel(c)             # > (35, 35, 35)
sshot = screen.shot()             # capture entire screen
sshot.show()                      # show it
sshot.save('blabla.png')          # save it
print dir(sshot)                  # show what else it can do
p1 = (c[0] - 100, c[1] - 100)     # upper left point > (620, 350)
p2 = (c[0] + 100, c[1] + 100)     # lower bottom point > (820, 550)
sshot = screen.shot(p1, p2)       # capture 200×200 area
box = p1 + p2                     # > (620, 350, 820, 550)
sshot = screen.shot(box)          # capture 200×200 area again
# Image API: http://www.pythonware.com/library/pil/handbook/image.htm

# searching on the screen:
color = (150, 191, 226)               # we got it earlier in (100, 100)
print screen.find(color)              # > (100, 100) - yep, it's still there
print screen.locate(color)            # > [(100, 100), (223, 100), (1084, 869)]
print screen.locate(color, p1)        # > [(1084, 869)]
print screen.locate(color, p1, p2)    # > []
print screen.find(color, box)         # > None

# testing:
print screen.test(color, 100, 100)    # > True
print screen.test(color, [223, 100])  # > True
print screen.test(color, c)           # > False
print screen.test(sshot, p1)          # > True
print screen.test(sshot, p2)          # > False

API

  • screen.size
    Get current screen size in pixels.
    Returns (width, height) tuple of 2 integers.
    Read-only property.

  • screen.width
    Get current screen width in pixels.
    Returns integer.
    Read-only property.

  • screen.height
    Get current screen height in pixels.
    Returns integer.
    Read-only property.

  • screen.center
    Get current screen center point.
    Returns (x, y) a tuple of 2 integers.
    Read-only property.

  • screen.pixel(x, y)
    screen.pixel(point)
    Get the color of given (x, y)-pixel (or point) on the screen.
    Returns (r, g, b) tuple of 3 integers.

  • screen.shot([left = 0, top = 0, right = screen.width, bottom = screen.height])
    screen.shot([from_point, to_point])
    screen.shot([box])
    Get screenshot of the screen area.
    Returns Image object.

  • screen.find(object, [left = 0, top = 0, right = screen.width, bottom = screen.height])
    screen.find(object, [from_point, to_point])
    screen.find(object, [box])
    If object is (r,g,b)-color - searches for the pixel of that color on the screen.
    If object is Image - searches for that image on the screen.
    Stops on first match.
    Returns position of found object as a tuple (x, y) of 2 integers if found or None if search fails.

  • screen.locate(object, [left = 0, top = 0, right = screen.width, bottom = screen.height])
    screen.locate(object, [from_point, to_point])
    screen.locate(object, [box])
    If object is (r,g,b)-color - searches for the pixels of that color on the screen.
    If object is Image - searches for that image on the screen.
    Tries to find all occurences.
    Returns positions of all found objects in a list of (x, y) tuples for each occurrence, empty if search fails.

  • screen.test(object, x, y)
    screen.test(object, point)
    Checks if a given object is found in (x, y)-positon on the screen.
    object can be (r,g,b)-color or Image.
    Returns True if it is, False otherwise.