示例#1
0
def test_static_animation():
    img1 = Image(200, 200)
    img2 = Image(200, 200)

    endpoints = np.array(((-100, 0), (100, 0)))
    offset = np.array((100, 100))

    fname = "test_animation.gif"

    anim = Animation(outfile(fname))
    anim.begin_anim(img1, 0)

    for ang in range(0, 360, 10):
        rad = np.deg2rad(ang)
        rot_matrix = [(np.cos(rad), np.sin(rad)), (-np.sin(rad), np.cos(rad))]
        points = np.dot(endpoints, rot_matrix).astype(np.int32) + offset

        img1.draw_line(points[0], points[1], 'red')
        img2.draw_line(points[0], points[1], 'red')

        assert img1 == img2

        anim.add_frame(img1)
        anim.add_frame(img2)

    anim.close_anim()
    print anim.frames_written
示例#2
0
def test_init2():
    _img = Image(width=400, height=400)

    _img = Image(400, 400)

    # need to pass in width and height
    with pytest.raises(TypeError):
        Image()

    with pytest.raises(TypeError):
        Image(200)
示例#3
0
def test_copy_ul():
    """
    test copying parts of an image
    """
    img1 = Image(10, 10)
    img2 = Image(10, 10)

    img1.draw_rectangle((1, 1), (8, 8), fill_color='red')
    img2.draw_rectangle((0, 0), (9, 9), fill_color='blue')

    img2.copy(img1, (0, 0), (7, 7), (4, 4))

    img2.save(outfile("image_copy_upper_left.bmp"))
示例#4
0
def test_copy2():
    """
    test copying parts of an image
    """
    img1 = Image(10, 10)
    img2 = Image(10, 10)

    img1.draw_rectangle((1, 1), (8, 8), fill_color='red')
    img2.draw_rectangle((0, 0), (9, 9), fill_color='blue')

    img2.copy(img1, (3, 3), (3, 3), (4, 4))

    img1.save(outfile("image_copy_middle1.bmp"))
    img2.save(outfile("image_copy_middle2.bmp"))
示例#5
0
def test_mem_limit():
    """
    test the limit for largest image.

    note that the 1GB max is arbitrary -- youc an change it iin the code.

    But my system, at least, will try to allocate much more memory that
    you'd want, bringing the system to an almost halt, before raising
    a memory error, so I sete a limit.
    """
    _img = Image(32768, 32768)  # 1 GB image

    with pytest.raises(MemoryError):
        _img = Image(32768, 32769)  # > 1 GB image
示例#6
0
def test_copy_transparent():
    """
    test copying parts of an image that are transparent
    """
    img1 = Image(10, 10)
    img2 = Image(10, 10)

    img1.draw_rectangle((0, 0), (9, 9), fill_color='red')
    img1.draw_rectangle((2, 2), (7, 7), fill_color='transparent')
    img2.draw_rectangle((0, 0), (9, 9), fill_color='blue')

    img2.copy(img1, (0, 0), (7, 7), (4, 4))

    img2.save(outfile("image_copy_trans.png"), file_type="png")
示例#7
0
def test_size():
    """
    test the size property
    """
    img = Image(10, 15)

    assert img.size == (10, 15)
示例#8
0
def test_equality():
    """
    test that an image is equal to itself and an identical image
    """
    img1 = Image(10, 10)
    img2 = Image(10, 10)
    img3 = Image(10, 11)

    img1.draw_rectangle((1, 1), (8, 8), fill_color='red')
    img2.draw_rectangle((1, 1), (8, 8), fill_color='red')
    img3.draw_rectangle((1, 1), (8, 8), fill_color='blue')

    assert img1 == img1
    assert img1 == img2
    assert img1 != img3
    assert (img1 == img3) is False
示例#9
0
def test_Polygon3():
    img = Image(100, 200)

    points = ((10, 10), (20, 190), (90, 10), (50, 50))

    img.draw_polygon(points, fill_color='blue', line_color='red', line_width=4)
    img.save(outfile("test_image_poly3.bmp"))
示例#10
0
def test_Polygon1():
    img = Image(100, 200)

    points = ((10, 10), (20, 190), (90, 10), (50, 50))

    img.draw_polygon(points, 'red')
    img.save(outfile("test_image_poly1.bmp"))
示例#11
0
def test_draw_x_large():
    img = Image(200, 200)

    img.draw_xes(((5, 5), ), diameter=3, color='red')

    img.draw_xes(((15, 15), ), diameter=4, color='red')

    img.draw_xes(((25, 25), ), diameter=5, color='purple')

    img.draw_xes(((35, 35), ), diameter=6, color='red')

    img.draw_xes(((45, 45), ), diameter=7, color='red')

    img.draw_xes(((55, 55), ), diameter=9, color='green')

    img.draw_xes(((65, 65), ), diameter=12, color='red', line_width=2)

    img.draw_xes(((80, 80), ), diameter=15, color='blue', line_width=3)

    img.draw_xes(((100, 100), ), diameter=20, color='fuchsia', line_width=4)

    img.draw_xes(((120, 120), ), diameter=30, color='red', line_width=5)

    img.draw_xes(((160, 160), ), diameter=40, color='red', line_width=10)

    img.save(outfile("test_image_x_large.png"), "png")
示例#12
0
def test_colors():
    img = Image(5, 5)

    # this shold work
    img.get_color_index('black')

    # so should this:
    img.get_color_index(0)
    img.get_color_index(255)

    # will round floating point numbers
    # shoul dthi sbe changed?
    assert img.get_color_index(2.3) == 2

    with pytest.raises(ValueError):
        # error if index not in 0--255
        img.get_color_index(300)

    with pytest.raises(ValueError):
        # error if color is not in dict
        img.get_color_index('something else')

    with pytest.raises(ValueError):
        # error if color is not anumber
        img.get_color_index((1, 2, 3))

    with pytest.raises(TypeError):
        # error if color is unhasable
        img.get_color_index(['a', 'random', 4])
示例#13
0
    def test_multi_segment(self):
        '''
        what if we break it down into smaller segments?
        '''
        img = Image(10, 10)
        val = int(2**30)  # should work

        coords = np.linspace(-val, val, 100000)
        rev_coords = np.flipud(coords)

        diag = np.c_[coords, coords]
        bottom = np.c_[rev_coords, np.ones_like(coords) * val]
        side = np.c_[np.ones_like(coords) * -val, rev_coords, ]
        points = np.r_[diag, bottom, side]

        img.draw_polygon(points,
                         line_color='black',
                         fill_color='red',
                         line_width=1)

        # save this one as an array
        arr = np.array(img)
        print arr
        print self.arr

        # this is expected to not be equal -- we've found a too-big value
        assert np.array_equal(arr, self.arr)
示例#14
0
def test_draw_dots_large():
    img = Image(200, 200)

    img.draw_dots(((5, 5), ), diameter=3, color='red')

    img.draw_dots(((15, 15), ), diameter=4, color='red')

    img.draw_dots(((25, 25), ), diameter=5, color='red')

    img.draw_dots(((35, 35), ), diameter=6, color='red')

    img.draw_dots(((45, 45), ), diameter=7, color='red')

    img.draw_dots(((55, 55), ), diameter=9, color='red')

    img.draw_dots(((65, 65), ), diameter=12, color='red')

    img.draw_dots(((80, 80), ), diameter=15, color='red')

    img.draw_dots(((100, 100), ), diameter=20, color='red')

    img.draw_dots(((120, 120), ), diameter=30, color='red')

    img.draw_dots(((65, 65), ), diameter=12, color='red')

    img.save(outfile("test_image_dots_large.png"), "png")
示例#15
0
def test_polygon_clip():
    img = Image(100, 200)

    points = ((-20, 10), (20, 250), (120, 10), (50, 50))

    img.draw_polygon(points, fill_color='blue', line_color='red')
    img.save(outfile("test_image_polygon_clip.bmp"))
示例#16
0
def test_init_simple_add_rgba():
    """
    simplest possible initilization -- no preset color palette
    """
    img = Image(width=400, height=400, preset_colors=None)

    img.add_color('white', (255, 255, 255, 127))
示例#17
0
def test_draw_dots3():
    img = Image(20, 20)

    img.draw_dots(((2, 2), (2, 18), (10, 10)), diameter=3)

    img.draw_dots(((18, 18), (18, 2)), diameter=4, color='red')

    img.save(outfile("test_image_points3.png"), "png")
示例#18
0
def test_Polygon2():
    img = Image(100, 200)

    points = ((10, 10), (20, 190), (90, 10), (50, 50))

    img.draw_polygon(points, fill_color='blue')

    img.save(outfile("test_image_poly2.bmp"))
示例#19
0
    def test_overflow(self):
        '''
        Big enough to overflow an 32 bit int
        '''
        img = Image(10, 10)
        val = int(2**33)

        with pytest.raises(OverflowError):
            img.draw_line((-val, -val), (val, val), 'white', line_width=2)
示例#20
0
    def test_outside(self):
        '''second value too large'''
        img = Image(10, 10)
        img.draw_line((0, 0), (100, 100), 'white', line_width=2)

        # save this as an array
        arr = np.array(img)

        assert np.array_equal(arr, self.line_arr)
示例#21
0
def test_copy1():
    """
    test copying a full image
    """
    img1 = Image(5, 5)
    img2 = Image(5, 5)

    img1.draw_pixel((0, 0), 'white')
    img1.draw_pixel((1, 1), 'red')
    img1.draw_pixel((2, 2), 'green')
    img1.draw_pixel((3, 3), 'blue')
    img1.draw_pixel((4, 4), 'gray')

    img2.copy(img1)

    img2.save(outfile("image_copy.bmp"))

    assert np.array_equal(np.array(img1), np.array(img2))
示例#22
0
    def test_negative(self):
        '''negative coords value too large'''
        img = Image(10, 10)
        img.draw_line((-100, -100), (10, 10), 'white', line_width=2)

        # save this as an array
        arr = np.array(img)

        assert np.array_equal(arr, self.line_arr)
示例#23
0
    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = Image(10, 10)
        img.draw_line((0, 0), (10, 10), 'white', line_width=2)

        # save this one as an array
        arr = np.array(img)

        assert np.array_equal(arr, self.line_arr)
示例#24
0
def test_add_colors_max():
    img = Image(10, 10, preset_colors='BW')

    # should be able to add this many:
    for i in range(253):
        img.add_color("color_%i" % i, (i, i, i))

    # adding one more should raise an exception:
    with pytest.raises(ValueError):
        img.add_color("color_max", (10, 100, 200))
示例#25
0
def test_SetPixel():
    img = Image(5, 5)

    img.draw_pixel((0, 0), 'white')
    img.draw_pixel((1, 1), 'red')
    img.draw_pixel((2, 2), 'green')
    img.draw_pixel((3, 3), 'blue')

    img.save(outfile("test_image_pixel.bmp"))
    assert check_file("test_image_pixel.bmp")
示例#26
0
def test_text():
    img = Image(200, 200)

    img.draw_text("Some Tiny Text", (20, 20), font="tiny", color='white')
    img.draw_text("Some Small Text", (20, 40), font="small", color='white')
    img.draw_text("Some Medium Text", (20, 60), font="medium", color='white')
    img.draw_text("Some Large Text", (20, 80), font="large", color='white')
    img.draw_text("Some Giant Text", (20, 100), font="giant", color='white')

    img.save(outfile("test_image_text.bmp"), "bmp")
示例#27
0
def test_rectangles():
    img = Image(100, 200)

    img.draw_rectangle((10, 10), (30, 40), fill_color='blue')
    img.draw_rectangle((20, 50), (40, 70), line_color='blue', line_width=5)
    img.draw_rectangle((40, 80), (90, 220),
                       fill_color='white',
                       line_color='green',
                       line_width=2)

    img.save(outfile("test_image_rectangle.bmp"))
示例#28
0
def test_clip_draw():
    img = Image(100, 100)

    img.clip_rect = ((20, 20), (80, 80))

    img.draw_line((0, 0), (100, 100), color='red', line_width=4)
    img.draw_line((0, 100), (100, 0), color='blue', line_width=4)

    fname = "image_clip.bmp"
    img.save(outfile(fname))

    assert check_file(fname)
示例#29
0
def test_GetPixel():
    img = Image(5, 5)

    img.draw_pixel((0, 0), 'white')
    img.draw_pixel((1, 1), 'red')
    img.draw_pixel((2, 2), 'green')
    img.draw_pixel((3, 3), 'blue')

    assert img.get_pixel_color((0, 0)) == 'white'
    assert img.get_pixel_color((1, 1)) == 'red'
    assert img.get_pixel_color((2, 2)) == 'green'
    assert img.get_pixel_color((3, 3)) == 'blue'
示例#30
0
def test_array():
    img = Image(10, 5)

    img.draw_line((0, 0), (9, 4), 'black', line_width=1)
    print "result from __array__", img.__array__()

    arr = np.asarray(img)
    assert np.array_equal(
        arr,
        [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0],
         [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0],
         [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]])