diff --git a/py_gd/test/test_buffer.py b/py_gd/test/test_buffer.py index 292519c..e9d7c0f 100644 --- a/py_gd/test/test_buffer.py +++ b/py_gd/test/test_buffer.py @@ -3,24 +3,24 @@ """ tests for buffer access to py_gd Image """ +import pytest -import py_gd +from py_gd import Image -import pytest -## skip this until we get it working... +# skip this until we get it working... @pytest.mark.skipif('True') def test_mem_view(): - img = py_gd.Image(5,10) + img = Image(5, 10) print img - #get a memoryview of it: + # get a memory view of it: m = memoryview(img) print m print m.format - print m.ndim - print m.shape + print m.ndim + print m.shape print m.suboffsets print m.itemsize print m.readonly diff --git a/py_gd/test/test_gd.py b/py_gd/test/test_gd.py index 26e8997..59ced36 100644 --- a/py_gd/test/test_gd.py +++ b/py_gd/test/test_gd.py @@ -1,18 +1,17 @@ #!/usr/bin/env python - """ unit tests for the py_gd project designed to be run with pytest: py.test test_gd.py - """ - -import os, hashlib +import os +import hashlib import pytest import numpy as np -import py_gd + +from py_gd import Image, Animation, asn2array, from_array def outfile(file_name): @@ -83,9 +82,7 @@ def test_init_simple(): """ simplest possible initilization -- no preset color palette """ - img = py_gd.Image(width=400, - height=400, - preset_colors=None) + img = Image(width=400, height=400, preset_colors=None) assert img @@ -93,7 +90,7 @@ def test_asn2array(): """ check if it works when the right input is used """ - arr = py_gd.asn2array([(1, 2), (3, 4), (5, 6)], dtype=np.intc) + arr = asn2array([(1, 2), (3, 4), (5, 6)], dtype=np.intc) assert type(arr) == np.ndarray assert arr.shape == (3, 2) @@ -105,18 +102,17 @@ def test_asn2array_fail(): check if it fails when wrong imput is used """ with pytest.raises(ValueError): - arr = py_gd.asn2array([(1, 2, 3), (3, 4, 5), (5, 6, 7)], dtype=np.intc) + _arr = asn2array([(1, 2, 3), (3, 4, 5), (5, 6, 7)], dtype=np.intc) with pytest.raises(ValueError): - arr = py_gd.asn2array((1, 2, 3, 3, 4, 5, 5, 6, 7), dtype=np.intc) + _arr = asn2array((1, 2, 3, 3, 4, 5, 5, 6, 7), dtype=np.intc) assert True def test_cant_save_file(): - img = py_gd.Image(width=400, - height=400 - ) + img = Image(width=400, height=400) + with pytest.raises(IOError): img.save("a/non_existant/file_path") @@ -125,9 +121,7 @@ def test_init_simple_add_rgb(): """ simplest possible initilization -- no preset color palette """ - img = py_gd.Image(width=400, - height=400, - preset_colors=None) + img = Image(width=400, height=400, preset_colors=None) img.add_color('white', (255, 255, 255)) @@ -136,9 +130,7 @@ 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 = Image(width=400, height=400, preset_colors=None) img.add_color('white', (255, 255, 255, 127)) @@ -147,23 +139,24 @@ def test_init_default_colors(): """ Initialize with the default palette """ - img = py_gd.Image(width=400, height=300) + _img = Image(width=400, height=300) def test_init_BW(): - img = py_gd.Image(10, 10, preset_colors='BW') + _img = Image(10, 10, preset_colors='BW') def test_init2(): - img = py_gd.Image(width=400, height=400) + _img = Image(width=400, height=400) - img = py_gd.Image(400, 400) + _img = Image(400, 400) # need to pass in width and height with pytest.raises(TypeError): - py_gd.Image() + Image() + with pytest.raises(TypeError): - py_gd.Image(200) + Image(200) def test_mem_limit(): @@ -176,46 +169,48 @@ def test_mem_limit(): 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 + _img = Image(32768, 32768) # 1 GB image with pytest.raises(MemoryError): - img = py_gd.Image(32768, 32769) # > 1 GB image + _img = Image(32768, 32769) # > 1 GB image def test_set_size(): """ you should not be able to set the size or width or height """ - img = py_gd.Image(40, 30) + img = Image(40, 30) - assert img.size == (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 - def test_info(): - img = py_gd.Image(400, 300) - - assert str(img) == "py_gd.Image: width:400 and height:300" + img = Image(400, 300) + assert str(img) == "py_gd.Image: width: 400, height: 300" assert repr(img) == "Image(width=400, height=300)" def test_add_colors(): - img = py_gd.Image(10, 10, preset_colors='BW') + img = Image(10, 10, preset_colors='BW') assert img.get_color_names() == ['transparent', 'black', 'white'] img.add_color('light grey', (220, 220, 220)) - assert img.get_color_names() == ['transparent', 'black', 'white', 'light grey'] + + assert img.get_color_names() == ['transparent', 'black', 'white', + 'light grey'] assert img.get_color_index('light grey') == 3 img.draw_rectangle((2, 2), (7, 7), fill_color='light grey') @@ -227,7 +222,7 @@ def test_add_colors(): def test_add_colors_repeat(): - img = py_gd.Image(10, 10, preset_colors='BW') + img = Image(10, 10, preset_colors='BW') index_1 = img.add_color('blue', (0, 0, 255)) @@ -238,11 +233,12 @@ def test_add_colors_repeat(): # Adding the same color with a different name: # adds another index -- should it? index_2 = img.add_color('full_blue', (0, 0, 255)) + assert index_1 != index_2 def test_add_colors_max(): - img = py_gd.Image(10, 10, preset_colors='BW') + img = Image(10, 10, preset_colors='BW') # should be able to add this many: for i in range(253): @@ -255,7 +251,7 @@ def test_add_colors_max(): @pytest.mark.parametrize("filetype", ["bmp", "jpg", "gif", "png"]) def test_save_image(filetype): - img = py_gd.Image(400, 300) + 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) @@ -271,7 +267,7 @@ def test_save_image(filetype): def test_clear(): - img = py_gd.Image(100, 200) + img = Image(100, 200) # just to put something in there to clear. img.draw_rectangle((-10, -10), (50, 100), fill_color='red') @@ -287,7 +283,7 @@ def test_clear(): def test_clear_color(): - img = py_gd.Image(100, 200) + img = Image(100, 200) # just to put something in there to clear. img.draw_rectangle((-10, -10), (50, 100), fill_color='red') @@ -301,12 +297,13 @@ def test_clear_color(): def test_line(): - img = py_gd.Image(100,200) + img = Image(100, 200) img.draw_line((0, 0), (99, 199), 'white') img.draw_line((0, 199), (99, 0), 'red', line_width=2) img.draw_line((0, 100), (99, 100), 'green', line_width=4) img.draw_line((50, 0), (50, 199), 'blue', line_width=8) + img.save(outfile("test_image_line.bmp")) assert check_file("test_image_line.bmp") @@ -315,14 +312,14 @@ def test_line(): def test_line_clip(): - img = py_gd.Image(100, 200) + img = Image(100, 200) img.draw_line((-30, -10), (150, 250), 'white') img.save(outfile("test_image_line_clip.bmp")) def test_SetPixel(): - img = py_gd.Image(5, 5) + img = Image(5, 5) img.draw_pixel((0, 0), 'white') img.draw_pixel((1, 1), 'red') @@ -334,7 +331,7 @@ def test_SetPixel(): def test_GetPixel(): - img = py_gd.Image(5, 5) + img = Image(5, 5) img.draw_pixel((0, 0), 'white') img.draw_pixel((1, 1), 'red') @@ -348,26 +345,24 @@ def test_GetPixel(): def test_Polygon1(): - img = py_gd.Image(100, 200) + img = Image(100, 200) points = ((10, 10), (20, 190), (90, 10), - (50, 50), - ) + (50, 50)) img.draw_polygon(points, 'red') img.save(outfile("test_image_poly1.bmp")) def test_Polygon2(): - img = py_gd.Image(100, 200) + img = Image(100, 200) points = ((10, 10), (20, 190), (90, 10), - (50, 50), - ) + (50, 50)) img.draw_polygon(points, fill_color='blue') @@ -375,48 +370,42 @@ def test_Polygon2(): def test_Polygon3(): - img = py_gd.Image(100, 200) + img = Image(100, 200) points = ((10, 10), (20, 190), (90, 10), - (50, 50), - ) + (50, 50)) img.draw_polygon(points, fill_color='blue', line_color='red', line_width=4) img.save(outfile("test_image_poly3.bmp")) def test_polygon_clip(): - img = py_gd.Image(100, 200) - - img = py_gd.Image(100, 200) + img = Image(100, 200) points = ((-20, 10), (20, 250), (120, 10), - (50, 50), - ) + (50, 50)) img.draw_polygon(points, fill_color='blue', line_color='red') img.save(outfile("test_image_polygon_clip.bmp")) def test_polyline(): - img = py_gd.Image(100, 200) + img = Image(100, 200) points = ((10, 10), (20, 190), (90, 10), - (50, 50), - ) + (50, 50)) img.draw_polyline(points, 'red', line_width=3) points = ((50, 50), (90, 190), - (10, 10), - ) + (10, 10)) img.draw_polyline(points, 'blue', line_width=5) @@ -428,58 +417,76 @@ def test_polyline(): def test_rectangles(): - img = py_gd.Image(100 , 200) + 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.draw_rectangle((40, 80), (90, 220), fill_color='white', + line_color='green', line_width=2) + img.save(outfile("test_image_rectangle.bmp")) def test_arc(): - img = py_gd.Image(400,600) - # possible flags: "Arc", "Pie", "Chord", "NoFill", "Edged" (Arc and Pie are the same) + img = Image(400, 600) + + # possible flags: "Arc", "Pie", "Chord", "NoFill", "Edged" + # (Arc and Pie are the same) center = (200, 150) - # just the lines - img.draw_arc(center, 380, 280, start=-30, end=30, line_color='white', style='arc', draw_wedge=False) - img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', style='chord', draw_wedge=False, line_width=3) - img.draw_arc(center, 380, 280, start=90, end=150, line_color='white', style='arc', draw_wedge=True, line_width=5) - img.draw_arc(center, 380, 280, start=150, end=210, line_color='white', style='chord', draw_wedge=True) + # just the lines + img.draw_arc(center, 380, 280, start=-30, end=30, line_color='white', + style='arc', draw_wedge=False) + img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', + style='chord', draw_wedge=False, line_width=3) + img.draw_arc(center, 380, 280, start=90, end=150, line_color='white', + style='arc', draw_wedge=True, line_width=5) + img.draw_arc(center, 380, 280, start=150, end=210, line_color='white', + style='chord', draw_wedge=True) # just fill - img.draw_arc(center, 380, 280, start=210, end= 270, fill_color='purple', style='arc') - img.draw_arc(center, 380, 280, start=270, end= 330, fill_color='teal', style='chord') + img.draw_arc(center, 380, 280, start=210, end=270, fill_color='purple', + style='arc') + img.draw_arc(center, 380, 280, start=270, end=330, fill_color='teal', + style='chord') # line and fill center = (200, 450) - img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', fill_color='green', style='chord') - # img.draw_arc(center, 380, 280, start= 90, end= 150, line_color='white', fill_color='blue', styles=['NoFill']) - img.draw_arc(center, 380, 280, start=150, end= 210, line_color='green', fill_color='white', style='arc') - # img.draw_arc(center, 380, 280, start=210, end= 270, line_color='white', fill_color='purple', styles=['Chord','Edged', 'NoFill']) - img.draw_arc(center, 380, 280, start=270, end= 330, line_color='blue', fill_color='red', line_width=3) + img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', + fill_color='green', style='chord') + # img.draw_arc(center, 380, 280, start= 90, end= 150, line_color='white', + # fill_color='blue', styles=['NoFill']) + img.draw_arc(center, 380, 280, start=150, end=210, line_color='green', + fill_color='white', style='arc') + # img.draw_arc(center, 380, 280, start=210, end= 270, line_color='white', + # fill_color='purple', styles=['Chord','Edged', 'NoFill']) + img.draw_arc(center, 380, 280, start=270, end=330, line_color='blue', + fill_color='red', line_width=3) img.save(outfile("test_image_arc.bmp")) # errors with pytest.raises(ValueError): - img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', style='fred') + img.draw_arc(center, 380, 280, start=30, end=90, line_color='white', + style='fred') def test_text(): - - img = py_gd.Image(200, 200) + 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") + def test_text_align(): - img = py_gd.Image(200, 200) + img = 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') @@ -488,145 +495,129 @@ def test_text_align(): img.draw_text("abcd", (100, 200), font="small", color='white', align='cb') 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.save(outfile("test_text_align.bmp"), "bmp") + def test_text_background(): - img = py_gd.Image(200, 200) + img = 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, 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", (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, 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", (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") def test_draw_dot(): - img = py_gd.Image(100, 100,) + img = Image(100, 100,) img.draw_dot((10, 10)) - img.draw_dot((20, 20), diameter=2, color='black') - img.draw_dot((30, 30), diameter=3, color='red') - img.draw_dot((40, 0), diameter=4, color='blue') - img.draw_dot((50, 50), diameter=6, color='aqua') - img.draw_dot((60, 60), diameter=8, color='lime') - img.draw_dot((70, 70), diameter=10, color='fuchsia') - img.draw_dot((80, 80), diameter=15, color='purple') img.save(outfile("test_image_dot.png"), "png") def test_draw_dots(): - img = py_gd.Image(20, 20) + img = Image(20, 20) img.draw_dots(((2, 2), - (2, 18), - (10, 10) - ) - ) + (2, 18), + (10, 10))) img.draw_dots(((18, 18), - (18, 2), - ), - diameter=2, - color='red' - ) + (18, 2)), + diameter=2, + color='red') img.save(outfile("test_image_points.png"), "png") + def test_draw_dots3(): - img = py_gd.Image(20, 20) + img = Image(20, 20) img.draw_dots(((2, 2), - (2, 18), - (10, 10) - ), - diameter=3 - ) + (2, 18), + (10, 10)), + diameter=3) img.draw_dots(((18, 18), - (18, 2), - ), - diameter=4, - color='red' - ) + (18, 2)), + diameter=4, + color='red') img.save(outfile("test_image_points3.png"), "png") + def test_draw_dots_large(): - img = py_gd.Image(200, 200) + img = Image(200, 200) img.draw_dots(((5, 5),), diameter=3, - color='red', - ) + color='red') img.draw_dots(((15, 15),), diameter=4, - color='red', - ) + color='red') img.draw_dots(((25, 25),), diameter=5, - color='red', - ) + color='red') img.draw_dots(((35, 35),), diameter=6, - color='red', - ) + color='red') img.draw_dots(((45, 45),), diameter=7, - color='red', - ) + color='red') img.draw_dots(((55, 55),), diameter=9, - color='red', - ) + color='red') img.draw_dots(((65, 65),), diameter=12, - color='red', - ) + color='red') img.draw_dots(((80, 80),), diameter=15, - color='red', - ) + color='red') img.draw_dots(((100, 100),), diameter=20, - color='red', - ) + color='red') img.draw_dots(((120, 120),), diameter=30, - color='red', - ) + color='red') img.draw_dots(((65, 65),), diameter=12, - color='red', - ) + color='red') img.save(outfile("test_image_dots_large.png"), "png") @@ -637,9 +628,10 @@ def test_draw_dots_lots(): """ import random w, h, = 1000, 500 - img = py_gd.Image(w, h) + img = Image(w, h) - points = [(random.randint(0, w), random.randint(0, w)) for i in range(10000)] + points = [(random.randint(0, w), random.randint(0, w)) + for _i in range(10000)] img.draw_dots(points, diameter=2, color='red') @@ -651,13 +643,13 @@ def test_draw_dots_wrong_shape(): test passing in a wrong-shaped points """ w, h, = 1000, 500 - img = py_gd.Image(w, h) + img = Image(w, h) points = [(1, 2, 3), (4, 5, 6), (7, 8, 9), - (10, 11, 12), - ] + (10, 11, 12)] + with pytest.raises(ValueError): img.draw_dots(points, diameter=2, color='red') @@ -670,9 +662,10 @@ def test_draw_x_lots(): """ import random w, h, = 1000, 500 - img = py_gd.Image(w, h) + img = Image(w, h) - points = [(random.randint(0, w), random.randint(0, w)) for i in range(1000)] + points = [(random.randint(0, w), random.randint(0, w)) + for _i in range(1000)] img.draw_xes(points, diameter=2, color='red') @@ -680,73 +673,62 @@ def test_draw_x_lots(): def test_draw_x_large(): - img = py_gd.Image(200, 200) + img = Image(200, 200) img.draw_xes(((5, 5),), diameter=3, - color='red', - ) + color='red') img.draw_xes(((15, 15),), diameter=4, - color='red', - ) + color='red') img.draw_xes(((25, 25),), diameter=5, - color='purple', - ) + color='purple') img.draw_xes(((35, 35),), diameter=6, - color='red', - ) + color='red') img.draw_xes(((45, 45),), diameter=7, - color='red', - ) + color='red') img.draw_xes(((55, 55),), diameter=9, - color='green', - ) + color='green') img.draw_xes(((65, 65),), diameter=12, color='red', - line_width=2, - ) + line_width=2) img.draw_xes(((80, 80),), diameter=15, color='blue', - line_width=3, - ) + line_width=3) img.draw_xes(((100, 100),), diameter=20, color='fuchsia', - line_width=4, - ) + line_width=4) img.draw_xes(((120, 120),), diameter=30, color='red', - line_width=5, - ) + line_width=5) img.draw_xes(((160, 160),), diameter=40, color='red', - line_width=10, - ) + line_width=10) img.save(outfile("test_image_x_large.png"), "png") def test_colors(): - img = py_gd.Image(5, 5) + img = Image(5, 5) # this shold work img.get_color_index('black') @@ -777,9 +759,11 @@ def test_colors(): def test_array(): - img = py_gd.Image(10, 5) + 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], @@ -790,15 +774,15 @@ def test_array(): [0, 0, 0, 1, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], - [0, 0, 0, 0, 1]] - ) + [0, 0, 0, 0, 1]]) def test_set_pixel_value(): """ test if setting the pixel value directly works. """ - img = py_gd.Image(4, 5) + img = Image(4, 5) + for i in range(4): for j in range(5): img.set_pixel_value((i, j), i) @@ -817,12 +801,14 @@ def test_array_set(): dtype=np.uint8, order='f') - img = py_gd.Image(arr.shape[0], arr.shape[1], preset_colors='web') + img = Image(arr.shape[0], arr.shape[1], preset_colors='web') + img.set_data(arr) img.save(outfile('test_image_array1.bmp')) print img.get_color_names() + for i in range(4): for j in range(3): print img.get_pixel_color((i, j)), @@ -841,7 +827,7 @@ def test_array_creation(): dtype=np.uint8, order='c') - img = py_gd.from_array(arr) + img = from_array(arr) img.save(outfile('test_image_array2.bmp')) @@ -854,9 +840,8 @@ def test_copy1(): """ test copying a full image """ - img1 = py_gd.Image(5, 5) - - img2 = py_gd.Image(5, 5) + img1 = Image(5, 5) + img2 = Image(5, 5) img1.draw_pixel((0, 0), 'white') img1.draw_pixel((1, 1), 'red') @@ -875,13 +860,13 @@ def test_copy2(): """ test copying parts of an image """ - img1 = py_gd.Image(10, 10) - img2 = py_gd.Image(10, 10) + 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)) + img2.copy(img1, (3, 3), (3, 3), (4, 4)) img1.save(outfile("image_copy_middle1.bmp")) img2.save(outfile("image_copy_middle2.bmp")) @@ -891,8 +876,8 @@ def test_copy_ul(): """ test copying parts of an image """ - img1 = py_gd.Image(10, 10) - img2 = py_gd.Image(10, 10) + 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') @@ -906,8 +891,8 @@ 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 = 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') @@ -922,9 +907,9 @@ 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 = 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') @@ -937,25 +922,23 @@ def test_equality(): def test_size(): - """ test the size property """ - img = py_gd.Image(10, 15) + img = Image(10, 15) assert img.size == (10, 15) # Some tests of Clipping def test_clip_getter(): - - img1 = py_gd.Image(100, 100) + img1 = Image(100, 100) assert img1.clip_rect == ((0, 0), (99, 99)) def test_clip_setter(): - img1 = py_gd.Image(100, 100) + img1 = Image(100, 100) img1.clip_rect = ((20, 20), (79, 79)) @@ -963,21 +946,23 @@ def test_clip_setter(): def test_clip_deleter(): - img = py_gd.Image(100, 100) + img = Image(100, 100) # set to a non-default img.clip_rect = ((20, 20), (79, 79)) - # check that that took + 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)) def test_clip_draw(): - img = py_gd.Image(100, 100) + img = Image(100, 100) + img.clip_rect = ((20, 20), (80, 80)) img.draw_line((0, 0), (100, 100), color='red', line_width=4) @@ -985,63 +970,69 @@ def test_clip_draw(): fname = "image_clip.bmp" img.save(outfile(fname)) + assert check_file(fname) def test_animation(): - img = py_gd.Image(200, 200) + img = 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(img,0) + anim = Animation(outfile(fname)) - for ang in range(0,360,10): + anim.begin_anim(img, 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 + if (ang < 180): img.draw_line(points[0], points[1], 'red') else: img.draw_line(points[0], points[1], 'red') + anim.add_frame(img) -# img.draw_line(np.array((200,100)),np.array((0,100)), 'green') -# anim.add_frame(img) img.draw_line(np.array((0, 100)), np.array((200, 100)), 'green') + anim.add_frame(img) anim.close_anim() + print anim.frames_written def test_static_animation(): - img1 = py_gd.Image(200, 200) - img2 = py_gd.Image(200, 200) + 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 = py_gd.Animation(outfile(fname)) + + 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 -# 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 + if __name__ == "__main__": # just run these tests.. # test_init_default_palette() diff --git a/py_gd/test/test_overflow.py b/py_gd/test/test_overflow.py index d23a2a0..e582e5f 100644 --- a/py_gd/test/test_overflow.py +++ b/py_gd/test/test_overflow.py @@ -1,5 +1,4 @@ #!/usr/bin/env python - """ tests for overflow in py_gd code @@ -8,28 +7,29 @@ It fails to properly fill polygons even when teh coords are well less than what can fit in a 32 bit int. -- it seems as thoough there is a multiplication in play -- the limit is around the square root of a max int. - """ -import math -import numpy as np -import py_gd import pytest +import numpy as np + +from py_gd import Image 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) + img = 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) + 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) @@ -37,61 +37,74 @@ def test_inside(self): 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) + 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) + 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) + 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) + 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 + 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) - 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 = Image(10, 10) + val = int(2 ** 33) - 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) + img.draw_line((-val, -val), (val, val), 'white', line_width=2) + class TestPolyLine(): # polygon drawing -- with just a line # Create a sample array to test against - img = py_gd.Image(10, 10) - points = ((-1,-1),(11,11),(-1,11))# a traingle that divides the image + 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) 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) + 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) @@ -99,37 +112,49 @@ def test_inside(self): 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) + 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) - def test_negative(self): '''negative coords value too large''' - img = py_gd.Image(10, 10) - points = ((-100,-100),(10,10),(-100,10))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color=None, line_width=1) + 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=None, + line_width=1) + # save this one as an array arr = np.array(img) assert np.array_equal(arr, self.arr) - 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) + + # a triangle that divides the image + points = ((-val, -val), (val, val), (-val, val)) + + img.draw_polygon(points, line_color='black', fill_color=None, + line_width=1) - img = py_gd.Image(10, 10) - val = int(2**30) - points = ((-val,-val),(val,val),(-val,val))# 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) @@ -139,34 +164,50 @@ def test_overflow(self): ''' Big enough to overflow an 32 bit int ''' - - img = py_gd.Image(10, 10) + img = Image(10, 10) val = int(2**33) -# with pytest.raises(OverflowError): -# img.draw_line( (-val, -val), (val, val), 'white', line_width=2) - points = ((-val,-val),(val,val),(-val,val))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color=None, line_width=1) + + # 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=None, + 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) + class TestPolyFill(): # polygon drawing -- with just a line # Create a sample array to test against - img = py_gd.Image(10, 10) - points = ((-1,-1),(11,11),(-1,11))# a traingle that divides the image - img.draw_polygon(points, line_color='black', fill_color='red', line_width=1) + img = Image(10, 10) + + # a traingle that divides the image + points = ((-1, -1), (11, 11), (-1, 11)) + + img.draw_polygon(points, line_color='black', fill_color='red', + line_width=1) + # save this one as an array arr = np.array(img) 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='red', line_width=1) + 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='red', + line_width=1) + # save this one as an array arr = np.array(img) @@ -174,37 +215,49 @@ def test_inside(self): 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='red', line_width=1) + 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='red', + line_width=1) + # save this one as an array arr = np.array(img) assert np.array_equal(arr, self.arr) - def test_negative(self): '''negative coords value too large''' - img = py_gd.Image(10, 10) - points = ((-100,-100),(10,10),(-100,10))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color='red', line_width=1) + 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) - @pytest.mark.xfail # this is giving an overflow problem -- only on fill + @pytest.mark.xfail # this is giving an overflow problem -- only on fill def test_huge(self): ''' - really big values, negative and positive - - but not quite enough to overflow an integer + 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) - img = py_gd.Image(10, 10) - val = int(2**30) - points = ((-val,-val),(val,val),(-val,val))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color='red', line_width=1) # save this one as an array arr = np.array(img) @@ -213,16 +266,18 @@ def test_huge(self): 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) - img = py_gd.Image(10, 10) - val = int(2**14) - points = ((-val,-val),(val,val),(-val,val))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color='red', line_width=1) # save this one as an array arr = np.array(img) @@ -231,56 +286,66 @@ def test_large(self): 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) - img = py_gd.Image(10, 10) - val = int(2**15) - points = ((-val,-val),(val,val),(-1,val))# a triangle that divides the image - 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 + # this is expected to not be equal -- we've found a too-big value assert not np.array_equal(arr, self.arr) def test_multi_segment(self): ''' what if we break it down into smaller segments? ''' - img = py_gd.Image(10, 10) - val = int(2**30)# should work + 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) + 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) + # this is expected to not be equal -- we've found a too-big value + assert np.array_equal(arr, self.arr) 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) - 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) - points = ((-val,-val),(val,val),(-val,val))# a triangle that divides the image - img.draw_polygon(points, line_color='black', fill_color='red', line_width=1) # save this one as an array arr = np.array(img)