示例#1
0
def test_static_animation():
    img1 = py_gd.Image(200, 200)
    img2 = py_gd.Image(200, 200)
    endpoints = np.array(((-100, 0), (100, 0)))
    offset = np.array((100, 100))

    fname = "test_animation.gif"
    anim = py_gd.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
        #         print points
        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)


#     img.draw_line(np.array((200,100)),np.array((0,100)), 'green')
#     anim.add_frame(img)
    anim.close_anim()
    print anim.frames_written
示例#2
0
def test_init2():
    img = py_gd.Image(width=400, height=400)

    img = py_gd.Image(400, 400)

    # need to pass in width and height
    with pytest.raises(TypeError):
        py_gd.Image()
    with pytest.raises(TypeError):
        py_gd.Image(200)
示例#3
0
    def create_images(self, preset_colors):
        width, height = self.image_size[:2]
        self.fore_image = py_gd.Image(width=width, height=height,
                                      preset_colors=preset_colors)

        self.back_image = py_gd.Image(width=width, height=height,
                                      preset_colors=preset_colors)
        if preset_colors is not None:
            "can't clear image if there are no colors"
            self.clear_background()
            self.clear_foreground()
示例#4
0
def test_copy_ul():
    """
    test copying parts of an image
    """
    img1 = py_gd.Image(10, 10)
    img2 = py_gd.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"))
示例#5
0
def test_polygon_clip():
    img = py_gd.Image(100, 200)

    img = py_gd.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"))
示例#6
0
def test_copy2():
    """
    test copying parts of an image
    """
    img1 = py_gd.Image(10, 10)
    img2 = py_gd.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"))
示例#7
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 = py_gd.Image(32768, 32768)  # 1 GB image

    with pytest.raises(MemoryError):
        img = py_gd.Image(32768, 32769)  # > 1 GB image
示例#8
0
def test_copy_transparent():
    """
    test copying parts of an image that are transparent
    """
    img1 = py_gd.Image(10, 10)
    img2 = py_gd.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")
示例#9
0
 def test_negative(self):
     '''negative coords value too large'''
     img = py_gd.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_init_simple_add_rgba():
    """
    simplest possible initilization -- no preset color palette
    """
    img = py_gd.Image(width=400, height=400, preset_colors=None)

    img.add_color('white', (255, 255, 255, 127))
示例#11
0
def test_text_background():
    img = py_gd.Image(200, 200)
    img.draw_text("abcd", (0, 0), font="tiny", color='white')
    img.draw_text("abcd", (100, 0), font="small", color='white', align='ct')
    img.draw_text("abcd", (200, 0),
                  font="medium",
                  color='white',
                  align='rt',
                  background='red')
    img.draw_text("abcd", (200, 100), font="large", color='white', align='r')
    img.draw_text("abcd", (200, 200), font="tiny", color='white', align='rb')
    img.draw_text("abcd", (100, 200),
                  font="small",
                  color='white',
                  align='cb',
                  background='green')
    img.draw_text("abcd", (0, 200), font="medium", color='white', align='lb')
    img.draw_text("abcd", (0, 100), font="large", color='white', align='l')
    img.draw_text("9999", (0, 0), font="tiny", color='white')
    img.draw_text("9999", (100, 0), font="small", color='red', align='ct')
    img.draw_text("9999", (200, 0),
                  font="medium",
                  color='white',
                  align='rt',
                  background='red')
    img.draw_text("9999", (200, 100), font="large", color='blue', align='r')
    img.draw_text("9999", (200, 200), font="tiny", color='white', align='rb')
    img.draw_text("9999", (100, 200),
                  font="small",
                  color='white',
                  align='cb',
                  background='green')
    img.draw_text("9999", (0, 200), font="medium", color='white', align='lb')
    img.draw_text("9999", (0, 100), font="large", color='white', align='l')
    img.save(outfile("test_text_background.bmp"), "bmp")
示例#12
0
    def save(self, filename, file_type='png'):
        """
        save the map image to the specified filename

        This copies the foreground image on top of the
        background image and saves teh whole thing.

        :param filename: full path of file to be saved to
        :type filename: string

        :param file_type: type of file to save
        :type file_type: one of the following:
                         {'png', 'gif', 'jpeg', 'bmp'}
        """
        # create a new image to composite
        width, height = self.image_size[:2]
        image = py_gd.Image(width=width, height=height)
        # copy the pallette from the foreground image
        print dir(self.fore_image)
        print self.fore_image.colors
        assert False

        self.fore_image.copy(self.back_image,
                             (0, 0), (0, 0),
                             self.back_image.size)
示例#13
0
 def test_outside(self):
     '''second value too large'''
     img = py_gd.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)
示例#14
0
def test_colors():
    img = py_gd.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])
示例#15
0
def test_size():
    """
    test the size property
    """
    img = py_gd.Image(10, 15)

    assert img.size == (10, 15)
示例#16
0
def test_equality():
    """
    test that an image is equal to itself and an identical image
    """
    img1 = py_gd.Image(10, 10)
    img2 = py_gd.Image(10, 10)
    img3 = py_gd.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
示例#17
0
def test_polyline():
    img = py_gd.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"))
示例#18
0
class TestLine():
    # line drawing -- all seems to work fine

    # Create a sample array to test against
    img = py_gd.Image(10, 10)
    img.draw_line( (0, 0), (10, 10), 'white', line_width=2)
    # save this one as an array
    line_arr = np.array(img)

    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = py_gd.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)

    def test_outside(self):
        '''second value too large'''
        img = py_gd.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)


    def test_negative(self):
        '''negative coords value too large'''
        img = py_gd.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)


    def test_big(self):
        '''
        really big values, negative and positive

        but not quite enough to overflow an integer
        '''

        img = py_gd.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)

    def test_overflow(self):
        '''
        Big enough to overflow an 32 bit int
        '''

        img = py_gd.Image(10, 10)
        val = int(2**33)
        with pytest.raises(OverflowError):
            img.draw_line( (-val, -val), (val, val), 'white', line_width=2)
示例#19
0
    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = py_gd.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)
示例#20
0
def test_array():
    img = py_gd.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, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
              [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]])
示例#21
0
def test_SetPixel():
    img = py_gd.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("test_image_pixel.bmp")
示例#22
0
    def test_outside(self):
        '''second value too large'''
        img = py_gd.Image(10, 10)
        points = ((-1,-1),(100,100),(-1,100))# a triangle that divides the image
        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)
示例#23
0
def test_text():
    img = py_gd.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("test_image_text.png", "png")
示例#24
0
    def test_inside(self):
        '''just to make sure the comparing is working'''
        img = py_gd.Image(10, 10)
        points = ((-1,-1),(11,11),(-1,11))# a triangle that divides the image
        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)
示例#25
0
    def test_overflow(self):
        '''
        Big enough to overflow an 32 bit int
        '''

        img = py_gd.Image(10, 10)
        val = int(2**33)
        with pytest.raises(OverflowError):
            img.draw_line( (-val, -val), (val, val), 'white', line_width=2)
示例#26
0
def test_copy1():
    """
    test copying a full image
    """
    img1 = py_gd.Image(5, 5)

    img2 = py_gd.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))
示例#27
0
def test_clip_draw():
    img = py_gd.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)
示例#28
0
def test_add_colors_max():
    img = py_gd.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))
示例#29
0
def test_rectangles():
    img = py_gd.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"))
示例#30
0
def test_draw_dots3():
    img = py_gd.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")