示例#1
0
def test_mask():

    # Initialize test surface and masks
    # NOTE: Testing KLDraw objects further down, since KLD rectangles have 1px
    # transparent padding that breaks this test loop's logic
    surface = NumpySurface(width=100, height=100, fill=[255, 0, 0])
    nps_mask = NumpySurface(width=50, height=50, fill=[255, 255, 255])
    np_mask = nps_mask.render()
    greyscale_mask = Image.new('L', (50, 50), 0)
    ImageDraw.Draw(greyscale_mask).rectangle((0, 0, 50, 50), fill=255)
    rgb_mask = Image.new('RGB', (50, 50), (0, 0, 0))
    ImageDraw.Draw(rgb_mask).rectangle((0, 0, 50, 50), fill=(255, 0, 0))

    # Test different mask types
    for mask in [nps_mask, np_mask, greyscale_mask, rgb_mask]:
        surf = surface.copy()
        surf.mask(mask, registration=7, location=(0, 0))
        assert surf.content[49][49][3] == 0
        assert surf.content[50][50][3] == 255

    # Test legacy positioning
    surf = surface.copy()
    surf.mask(nps_mask, (25, 25))
    assert surf.content[0][0][3] == 255 and surf.content[25][25][3] == 0

    # Test with mask partially off surface
    surf = surface.copy()
    surf.mask(nps_mask, registration=3, location=(25, 25))
    assert surf.content[24][24][3] == 0 and surf.content[25][25][3] == 255
    surf.mask(nps_mask, registration=7, location=(75, 75))
    assert surf.content[74][74][3] == 255 and surf.content[75][75][3] == 0

    # Test inverse/non-inverse modes and complete masking
    circle_mask = kld.Ellipse(50, fill=(255, 255, 255))
    for complete in [True, False]:
        surf = surface.copy()
        surf.mask(circle_mask, location=(0, 0), invert=True, complete=complete)
        assert surf.content[0][0][3] == 255 and surf.content[25][25][3] == 0
        assert surf.content[-1][-1][3] == (0 if complete else 255)
        surf = surface.copy()
        surf.mask(circle_mask,
                  location=(0, 0),
                  invert=False,
                  complete=complete)
        assert surf.content[0][0][3] == 0 and surf.content[25][25][3] == 255
        assert surf.content[-1][-1][3] == (0 if complete else 255)

    # Test exception for invalid mask type
    with pytest.raises(TypeError):
        surf = surface.copy()
        surf.mask("hello")
示例#2
0
def test_render():

    surf = NumpySurface(width=100, height=100, fill=[255, 0, 0])
    rendered = surf.render()
    assert rendered.dtype == np.uint8
    assert rendered[0][0][0] == 255 and rendered[0][0][1] == 0