Пример #1
0
def test_canvas_background():
    with open(get_reference_image('dither.png'), 'rb') as p1:
        with open(get_reference_image('background.png'), 'rb') as p2:
            bgnd = Image.open(p1)
            reference = Image.open(p2)
            device = dummy()

            with canvas(device, background=bgnd) as draw:
                draw.rectangle((20, 15, device.width - 20, device.height - 15),
                               fill="black",
                               outline="white")
                draw.text((36, 22), "Background", fill="white")
                draw.text((52, 32), "Test", fill="white")

            assert_identical_image(reference, device.image)
Пример #2
0
def test_seven_segment():
    with open(get_reference_image("seven_segment.png"), "rb") as fp:
        ref = Image.open(fp)
        chars = [
            # Alphabet with omissions
            0x00, 0x01, 0x08, 0x02, 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f,
            0x70, 0x7f, 0x7b, 0x7d, 0x1f, 0x0d, 0x3d, 0x6f, 0x47, 0x7b, 0x17,
            0x10, 0x18, 0x06, 0x15, 0x1d, 0x67, 0x73, 0x05, 0x5b, 0x0f, 0x1c,
            0x1c, 0x3b, 0x6d, 0x77, 0x7f, 0x4e, 0x7e, 0x4f, 0x47, 0x5e, 0x37,
            0x30, 0x38, 0x0e, 0x76, 0x7e, 0x67, 0x73, 0x46, 0x5b, 0x0f, 0x3e,
            0x3e, 0x3b, 0x6d, 0x80, 0x80
        ]
        device = dummy(width=len(chars), height=8)
        with canvas(device) as draw:
            for x in range(len(chars)):
                val = chars[x]
                for y in range(8):
                    mask = 1 << y
                    if val & mask != 0:
                        draw.point((device.width - x, y), fill="white")
        surface = to_pygame_surface(device.image)
        tf = transformer(pygame, device.width, device.height, 16)
        im = to_pillow_img(tf.seven_segment(surface))
        bbox = ImageChops.difference(ref, im).getbbox()
        assert bbox is None
Пример #3
0
def test_canvas_background():
    img_path = get_reference_image('background.png')
    with open(get_reference_image('dither.png'), 'rb') as p1:
        with open(img_path, 'rb') as p2:
            bgnd = Image.open(p1)
            reference = Image.open(p2)
            device = dummy()

            with canvas(device, background=bgnd) as draw:
                draw.rectangle((20, 15, device.width - 20, device.height - 15),
                               fill='black',
                               outline='white')
                draw.text((36, 22), 'Background', fill='white')
                draw.text((52, 32), 'Test', fill='white')

            assert_identical_image(reference, device.image, img_path)
Пример #4
0
def test_identity():
    with open(get_reference_image("identity.png"), "rb") as fp:
        ref = Image.open(fp)
        surface = baseline_im()
        tf = transformer(pygame, 128, 64, 2)
        im = to_pillow_img(tf.identity(surface))
        bbox = ImageChops.difference(ref, im).getbbox()
        assert bbox is None
def test_segment_mapper():
    img_path = get_reference_image('neosegment.png')

    with open(img_path, 'rb') as img:
        reference = Image.open(img)
        neoseg = neosegment(width=6, device=dummy(width=6, height=7))
        neoseg.color = ["red", "green", "blue", "yellow", "cyan", "magenta"]
        neoseg.text = "012345"
        assert_identical_image(reference, neoseg.device.image)
Пример #6
0
def test_segment_mapper():
    img_path = get_reference_image('neosegment.png')

    with open(img_path, 'rb') as img:
        reference = Image.open(img)
        neoseg = neosegment(width=6, device=dummy(width=6, height=7))
        neoseg.color = ["red", "green", "blue", "yellow", "cyan", "magenta"]
        neoseg.text = "012345"
        assert_identical_image(reference, neoseg.device.image)
Пример #7
0
def assert_text(device, term, reference_img, text):
    img_path = get_reference_image(reference_img)

    with open(img_path, 'rb') as p:
        reference = Image.open(p)

        for line in text:
            term.println(line)

        assert_identical_image(reference, device.image)
Пример #8
0
def test_gifanim_max_frames():
    reference = get_reference_image('anim.gif')
    img = Image.open(reference)
    with NamedTemporaryFile(suffix='.gif') as temp:
        fname = temp.name
        device = gifanim(256, 128, filename=fname, max_frames=1)

        with pytest.raises(SystemExit) as ex:
            device.display(img)
        assert str(ex.value) == '0'
Пример #9
0
def test_font(fontname):
    font = getattr(luma.core.legacy.font, fontname)
    w, h = textsize(charset, proportional(font))
    device = dummy(width=w, height=h, mode="1")
    with canvas(device) as draw:
        text(draw, (0, 0), charset, "white", font=proportional(font))

    img_path = get_reference_image(f'{fontname}.png')
    with open(img_path, 'rb') as fp:
        reference = Image.open(fp)
        assert_identical_image(reference, device.image, img_path)
Пример #10
0
def test_setter_getter():
    img_path = get_reference_image('golden_ratio.png')

    with open(img_path, 'rb') as img:
        reference = Image.open(img)
        device = dummy(width=24, height=8)
        seg = sevensegment(device, segment_mapper=dot_muncher)
        seg.text = "1.61803398875"
        assert str(seg.text) == "1.61803398875"

        assert_identical_image(reference, device.image)
Пример #11
0
def test_setter_getter():
    img_path = get_reference_image('golden_ratio.png')

    with open(img_path, 'rb') as img:
        reference = Image.open(img)
        device = dummy(width=24, height=8)
        seg = sevensegment(device)
        seg.text = "1.61803398875"
        assert str(seg.text) == "1.61803398875"

        bbox = ImageChops.difference(reference, device.image).getbbox()
        assert bbox is None
Пример #12
0
def test_setter_getter():
    fnt_path = get_reference_file(os.path.join('font', 'hd44780a02.pil'))
    img_path = get_reference_image('character_golden_ratio.png')

    with open(img_path, 'rb') as img:
        fnt = ImageFont.load(fnt_path)
        reference = Image.open(img)
        device = dummy(width=80, height=16, mode="1")
        c = character(device, font=fnt)
        c.text = "1.61803398875\n1.61803398875"
        assert str(c.text) == "1.61803398875\n1.61803398875"

        assert_identical_image(reference, device.image, img_path)
Пример #13
0
def test_led_matrix():
    with open(get_reference_image("led_matrix.png"), "rb") as fp:
        ref = Image.open(fp)
        device = dummy(width=40, height=24)
        with canvas(device) as draw:
            draw.rectangle(device.bounding_box, outline="white")
            draw.text((5, 2), "Hello", fill="white")
            draw.text((5, 10), "World", fill="white")
        surface = to_pygame_surface(device.image)
        tf = transformer(pygame, device.width, device.height, 16)
        im = to_pillow_img(tf.led_matrix(surface))
        bbox = ImageChops.difference(ref, im).getbbox()
        assert bbox is None
Пример #14
0
def assert_text(device, term, reference_img, text, save=None):
    img_path = get_reference_image(reference_img)

    with open(img_path, 'rb') as fp:
        reference = Image.open(fp)

        for line in text:
            term.println(line)

        if save is not None:
            device.image.save(save)

        assert_identical_image(reference, device.image, img_path)
Пример #15
0
def test_portrait():
    img_path = get_reference_image('portrait.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)

        device = dummy(rotate=1)

        # Use the same drawing primitives as the demo
        with canvas(device) as draw:
            baseline_data.primitives(device, draw)

        assert_identical_image(reference, device.image)
Пример #16
0
def test_dither():
    img_path = get_reference_image('dither.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy(mode="1")

        with canvas(device, dither=True) as draw:
            draw.rectangle((0, 0, 64, 32), fill="red")
            draw.rectangle((64, 0, 128, 32), fill="yellow")
            draw.rectangle((0, 32, 64, 64), fill="blue")
            draw.rectangle((64, 32, 128, 64), fill="white")

        assert_identical_image(reference, device.image)
Пример #17
0
def test_viewport_set_position():
    img_path = get_reference_image('set_position.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy()
        virtual = viewport(device, 200, 200)

        # Use the same drawing primitives as the demo
        with canvas(virtual) as draw:
            baseline_data.primitives(virtual, draw)

        virtual.set_position((20, 30))

        assert_identical_image(reference, device.image, img_path)
Пример #18
0
def test_viewport_hotspot():
    img_path = get_reference_image('hotspot.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy()
        virtual = viewport(device, 200, 200)

        def draw_fn(draw, width, height):
            baseline_data.primitives(device, draw)

        widget = hotspot(device.width, device.height, draw_fn)

        virtual.add_hotspot(widget, (19, 56))
        virtual.set_position((28, 30))
        virtual.remove_hotspot(widget, (19, 56))

        assert_identical_image(reference, device.image, img_path)
Пример #19
0
def test_viewport_dithering():
    img_path = get_reference_image('hotspot_dither.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy(mode="1")
        virtual = viewport(device, 200, 200, mode='RGBA', dither=True)

        def draw_fn(draw, width, height):
            draw.rectangle((0, 0, width / 2, height / 2), fill="red")
            draw.rectangle((width / 2, 0, width, height / 2), fill="yellow")
            draw.rectangle((0, height / 2, width / 2, height), fill="blue")
            draw.rectangle((width / 2, height / 2, width, height),
                           fill="white")

        widget = hotspot(device.width, device.height, draw_fn)

        virtual.add_hotspot(widget, (19, 56))
        virtual.set_position((28, 30))
        virtual.remove_hotspot(widget, (19, 56))

        assert_identical_image(reference, device.image, img_path)
Пример #20
0
def baseline_im():
    return pygame.image.load(get_reference_image("capture.png"))