Exemple #1
0
def test_crop():
    img = stbt.load_image("action-panel.png")
    cropped = stbt.crop(img, stbt.Region(x=1045, y=672, right=1081,
                                         bottom=691))
    reference = stbt.load_image("action-panel-blue-button.png")
    assert numpy.array_equal(reference, cropped)

    # It's a view onto the same memory:
    assert cropped[0, 0, 0] == img[672, 1045, 0]
    assert img[672, 1045, 0] != 0
    cropped[0, 0, 0] = 0
    assert img[672, 1045, 0] == 0

    assert img.filename == "action-panel.png"
    assert cropped.filename == img.filename

    # Region is clipped to the frame boundaries:
    assert numpy.array_equal(
        stbt.crop(img, stbt.Region(x=1045, y=672, right=1280, bottom=720)),
        stbt.crop(img, stbt.Region(x=1045, y=672, right=1281, bottom=721)))
    assert numpy.array_equal(
        stbt.crop(img, stbt.Region(x=0, y=0, right=10, bottom=10)),
        stbt.crop(
            img,
            stbt.Region(x=float("-inf"), y=float("-inf"), right=10,
                        bottom=10)))

    # But a region entirely outside the frame is not allowed:
    with pytest.raises(TypeError):
        stbt.crop(img, None)
    with pytest.raises(ValueError):
        stbt.crop(img, stbt.Region(x=-10, y=-10, right=0, bottom=0))
Exemple #2
0
def test_crop():
    f = stbt.load_image("action-panel.png")
    cropped = stbt.crop(f, stbt.Region(x=1045, y=672, right=1081, bottom=691))
    reference = stbt.load_image("action-panel-blue-button.png")
    assert numpy.array_equal(reference, cropped)

    # It's a view onto the same memory:
    assert cropped[0, 0, 0] == f[672, 1045, 0]
    cropped[0, 0, 0] = 0
    assert cropped[0, 0, 0] == f[672, 1045, 0]

    # Region must be inside the frame (unfortunately this means that you can't
    # use stbt.Region.ALL):
    with pytest.raises(ValueError):
        stbt.crop(f, stbt.Region(x=1045, y=672, right=1281, bottom=721))
Exemple #3
0
def test_that_match_all_can_be_used_with_ocr_to_read_buttons():
    # Demonstrates how match_all can be used with ocr for UIs consisting of text
    # on buttons
    frame = stbt.load_image('buttons.png')

    text = [
        stbt.ocr(frame=stbt.crop(
            frame, m.region.extend(x=30, y=10, right=-30, bottom=-10)))
        for m in stbt.match_all('button-transparent.png', frame=frame)
    ]
    text = sorted([t for t in text if t not in ['', '\\s']])
    print(text)
    assert text == [u'Button 1', u'Button 2', u'Buttons']
Exemple #4
0
def test_that_slicing_a_Frame_is_still_a_Frame():
    f = stbt.Frame(numpy.zeros((720, 1280, 3), dtype=numpy.uint8), time=1234)

    f1 = f[10:20, 10:, 0]
    assert isinstance(f1, stbt.Frame)
    assert f1.time == 1234

    f2 = stbt.crop(f, stbt.Region(10, 10, 20, 20))
    assert isinstance(f2, stbt.Frame)
    assert f2.time == 1234

    f3 = f.copy()
    assert isinstance(f3, stbt.Frame)
    assert f3.time == 1234
    assert (f.__array_interface__["data"][0] !=
            f3.__array_interface__["data"][0])

    f4 = stbt.Frame(f)
    assert f4.time == 1234