示例#1
0
文件: imgutil.py 项目: ponty/discogui
def focus_wnd():
    '''
    move mouse over window to get focus
    '''
    rct_wnd = getbbox(pyscreenshot.grab())
    if not rct_wnd:
        raise EmptyScreenException('Empty screen!')

    imglog.set_crop_rect(rct_wnd)

    # init window focus
    mouse = PyMouse()
    mouse.move(*rct_wnd.topleft)

    img_orig = pyscreenshot.grab()
    imglog.img_log(img_orig, 'img_orig')
    return img_orig
示例#2
0
def tab_rectangles():
    '''
    Return rectangles found by sending TAB button events.

    Does not work if other parts of screen are changing (e.g. blinking cursor)

    :rtype: rectangles list
    '''
    ls = []

    img_orig = focus_wnd()

    im1 = img_orig

    while 1:
        send_key('\t')
        im2 = pyscreenshot.grab()

        img_log(im1, 'im1')
        img_log(im2, 'im2')

        boxes = tab_rect_pair(im1, im2)
        if not boxes:
            return []
        if len(ls):
            if len(boxes) == 2:
                # TODO: implement almost_equal()
                assert boxes[0] == ls[-1]
            if boxes[-1] in ls:
                break
            ls += [boxes[-1]]
        else:
            # first
            ls += boxes
        im1 = im2

    img_log_rects(img_orig, ls, 'img_orig')
    log.debug('rectangles found:%s', ls)
    return ls
示例#3
0
def tab_rect_pair(img_orig, im_next):
    '''
    '''
    img_diff = ImageChops.difference(img_orig, im_next)
    img_log(img_diff, 'img_diff')

    # can be dotted -> filter + enhance color
    img_diff_filtered = img_diff.filter(ImageFilter.MaxFilter(5))
    img_diff_filtered = img_diff_filtered.point(lambda x: 255 * bool(x))
    img_log(img_diff_filtered, 'img_diff_filtered')

    bbox = getbbox(img_diff)
    if not bbox:
        return None

    def check_edges(horiz):
        if horiz:
            r1 = bbox.left
            r2 = bbox.right
        else:
            r1 = bbox.top
            r2 = bbox.bottom
        ls = []
        for c in xrange(int(r1), int(r2)):
            if horiz:
                p1 = (c, bbox.top)
                p2 = (c, bbox.bottom)
            else:
                p1 = (bbox.left, c)
                p2 = (bbox.right, c)
            color1 = sum(img_diff_filtered.getpixel(p1))
            color2 = sum(img_diff_filtered.getpixel(p2))
            ls += [int(bool(color1 + color2))]

        if not 0 in ls:
            log.debug('split pos not found')
            return
        i = ls.index(0)
        if i == 0:
            ls.reverse()
            i = ls.index(0)
            i = len(ls) - i - 1
        pos = i + r1
        log.debug('split pos found:%s' % pos)
        if horiz:
            rsegment1 = ScreenRect(0, 0, pos, img_orig.size[1])
            rsegment2 = ScreenRect(pos, 0, img_orig.size[0], img_orig.size[1])
        else:
            rsegment1 = ScreenRect(0, 0, img_orig.size[0], pos)
            rsegment2 = ScreenRect(0, pos, img_orig.size[0], img_orig.size[1])
        box1 = getbbox(img_diff.crop(rsegment1))
        box1.move(rsegment1.topleft)

        box2 = getbbox(img_diff.crop(rsegment2))
        box2.move(rsegment2.topleft)

        return   box1, box2
    r = check_edges(0)
    if r is None:
        r = check_edges(1)
    if r is None:
        # in new styles the textbox is not changing
        return [bbox]
    box1, box2 = r
    d1 = darker(img_orig, im_next, box1)
    d2 = darker(img_orig, im_next, box2)
    if d1==d2:
        log.warning( 'd1 == d2  %s  %s %s %s', d1, d2, box1, box2)
    if d1 == 1:
        boxes = (box1, box2)
    else:
        boxes = (box2, box1)
    return boxes