Пример #1
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)
Пример #2
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))
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #12
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)
Пример #13
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"))
Пример #14
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]])
Пример #15
0
def test_draw_dots_wrong_shape():
    """
    test passing in a wrong-shaped points
    """
    w, h, = 1000, 500
    img = Image(w, h)

    points = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]

    with pytest.raises(ValueError):
        img.draw_dots(points, diameter=2, color='red')

    assert True
Пример #16
0
def test_clip_deleter():
    img = Image(100, 100)

    # set to a non-default
    img.clip_rect = ((20, 20), (79, 79))

    assert img.clip_rect == ((20, 20), (79, 79))

    # delete it
    del img.clip_rect

    # it should be re-set to the image size.
    assert img.clip_rect == ((0, 0), (img.width - 1, img.height - 1))
Пример #17
0
    def test_big(self):
        '''
        really big values, negative and positive, but not quite enough
        to overflow an integer
        '''
        img = Image(10, 10)
        val = int(2**30)

        img.draw_line((-val, -val), (val, val), 'white', line_width=2)

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

        assert np.array_equal(arr, self.line_arr)
Пример #18
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
Пример #19
0
    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-1, -1), (11, 11), (-1, 11))

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

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

        assert np.array_equal(arr, self.arr)
Пример #20
0
    def test_outside(self):
        '''second value too large'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-1, -1), (100, 100), (-1, 100))

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

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

        assert np.array_equal(arr, self.arr)
Пример #21
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
Пример #22
0
def test_set_pixel_value():
    """
    test if setting the pixel value directly works.
    """
    img = Image(4, 5)

    for i in range(4):
        for j in range(5):
            img.set_pixel_value((i, j), i)

    # check:
    for i in range(4):
        for j in range(5):
            assert img.get_pixel_value((i, j)) == i
Пример #23
0
    def test_big(self):
        '''
        really big values, negative and positive, but not quite enough
        to overflow an integer
        '''
        img = Image(10, 10)
        val = int(2 ** 30)

        img.draw_line((-val, -val), (val, val), 'white', line_width=2)

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

        assert np.array_equal(arr, self.line_arr)
Пример #24
0
    def test_negative(self):
        '''negative coords value too large'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-100, -100), (10, 10), (-100, 10))

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

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

        assert np.array_equal(arr, self.arr)
Пример #25
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")
Пример #26
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)
Пример #27
0
def test_size():
    """
    test the size property
    """
    img = Image(10, 15)

    assert img.size == (10, 15)
Пример #28
0
    def test_negative(self):
        '''negative coords value too large'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-100, -100), (10, 10), (-100, 10))

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

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

        assert np.array_equal(arr, self.arr)
Пример #29
0
    def test_outside(self):
        '''second value too large'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-1, -1), (100, 100), (-1, 100))

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

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

        assert np.array_equal(arr, self.arr)
Пример #30
0
    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = Image(10, 10)

        # a triangle that divides the image
        points = ((-1, -1), (11, 11), (-1, 11))

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

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

        assert np.array_equal(arr, self.arr)
Пример #31
0
def test_polyline():
    img = Image(100, 200)

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

    img.draw_polyline(points, 'red', line_width=3)

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

    img.draw_polyline(points, 'blue', line_width=5)

    with pytest.raises(ValueError):
        # can't accept just one point
        img.draw_polyline(((10, 10), ), 'blue')

    img.save(outfile("test_image_polyline.bmp"))
Пример #32
0
def test_set_size():
    """
    you should not be able to set the size or width or height
    """
    img = Image(40, 30)

    assert img.size == (40, 30)
    assert img.height == 30
    assert img.width == 40

    with pytest.raises(AttributeError):
        img.size = (50, 60)

    with pytest.raises(AttributeError):
        img.height = 100

    with pytest.raises(AttributeError):
        img.width = 100
Пример #33
0
    def test_huge(self):
        '''
        really big values, negative and positive, but not quite enough
        to overflow an integer
        '''
        img = Image(10, 10)
        val = int(2 ** 30)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-val, val))

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

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

        assert np.array_equal(arr, self.arr)
Пример #34
0
    def test_large(self):
        '''
        large values, negative and positive
        just less than sqrt(max_int32)
        this seems to confirm that that's the limit
        '''
        img = Image(10, 10)
        val = int(2 ** 14)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-val, val))

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

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

        assert np.array_equal(arr, self.arr)
Пример #35
0
    def test_huge(self):
        '''
        really big values, negative and positive, but not quite enough
        to overflow an integer
        '''
        img = Image(10, 10)
        val = int(2**30)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-val, val))

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

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

        assert np.array_equal(arr, self.arr)
Пример #36
0
    def test_too_large(self):
        '''
        large values, negative and positive
        just more than sqrt(max_int32)
        this seems to confirm that that's the limit
        '''
        img = Image(10, 10)
        val = int(2 ** 15)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-1, val))

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

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

        # this is expected to not be equal -- we've found a too-big value
        assert not np.array_equal(arr, self.arr)
Пример #37
0
    def test_large(self):
        '''
        large values, negative and positive
        just less than sqrt(max_int32)
        this seems to confirm that that's the limit
        '''
        img = Image(10, 10)
        val = int(2**14)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-val, val))

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

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

        assert np.array_equal(arr, self.arr)
Пример #38
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)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-val, val))

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

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

        # This isn't expect to draw correctly
        assert not np.array_equal(arr, self.arr)
Пример #39
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"))
Пример #40
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"))
Пример #41
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"))
Пример #42
0
def test_save_image(filetype):
    img = Image(400, 300)

    img.draw_line((0, 0), (399, 299), 'white', line_width=4)
    img.draw_line((0, 299), (399, 0), 'green', line_width=4)

    fname = "test_image_save." + filetype
    img.save(outfile(fname), filetype)

    if filetype is not "jpg":  # jpeg is lossy and thus inconsistent
        assert check_file(fname)

    with pytest.raises(ValueError):
        img.save(outfile("test_image1.something"), "random_string")
Пример #43
0
    def test_too_large(self):
        '''
        large values, negative and positive
        just more than sqrt(max_int32)
        this seems to confirm that that's the limit
        '''
        img = Image(10, 10)
        val = int(2**15)

        # a triangle that divides the image
        points = ((-val, -val), (val, val), (-1, val))

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

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

        # this is expected to not be equal -- we've found a too-big value
        assert not np.array_equal(arr, self.arr)