def setUp(self): self.data = np.empty(shape=(128, 256, 4), dtype='uint8') self.data[:, :, 0] = np.arange(256) self.data[:, :, 1] = np.arange(128)[:, np.newaxis] self.data[:, :, 2] = np.arange(256)[::-1] self.data[:, :, 3] = np.arange(128)[::-1, np.newaxis] self.image_24 = Image(self.data[..., :3]) self.image_32 = Image(self.data)
def test_fromfile_png_rgba(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basi6a08.png') image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 4)) self.assertEqual(image.format, 'rgba32')
def dash(sz=(1000, 1000)): gc = GraphicsContextArray(sz) gc.set_fill_color((1.0, 0.0, 0.0, 0.1)) gc.set_stroke_color((0.0, 1.0, 0.0, 0.6)) width = 10 gc.set_line_width(10) phase = width * 2.5 pattern = width * numpy.array((5, 5)) gc.set_line_dash(pattern, phase) gc.set_line_cap(constants.CAP_BUTT) t1 = perf_counter() gc.move_to(10, 10) gc.line_to(sz[0] - 10, sz[1] - 10) gc.line_to(10, sz[1] - 10) gc.close_path() gc.draw_path() t2 = perf_counter() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") tot_time = t2 - t1 print("time:", tot_time) return image
def compiled_path(): # Creating the compiled path star_points = [ (-20, -30), (0, 30), (20, -30), (-30, 10), (30, 10), (-20, -30), ] path = CompiledPath() path.move_to(*star_points[0]) for pt in star_points[1:]: path.line_to(*pt) locs = [ (100, 100), (100, 300), (100, 500), (200, 100), (200, 300), (200, 500), ] gc = GraphicsContext((300, 600)) gc.set_stroke_color((0, 0, 1, 1)) gc.draw_path_at_points(locs, path, STROKE) with tempfile.NamedTemporaryFile(suffix=".jpg") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def test_fromfile_png_rgb(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, "PngSuite", "basn2c08.png") image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 3)) self.assertEqual(image.format, "rgb24")
def gradient(): gc = GraphicsContext((500, 500)) gc.scale_ctm(1.25, 1.25) draw(gc) with tempfile.NamedTemporaryFile(suffix=".png") as fid: gc.save(fid.name, file_format="png") image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def rect(): gc = GraphicsContext((500, 500)) gc.clear() gc.rect(100, 100, 300, 300) gc.draw_path() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def ellipse(): gc = GraphicsContext((300, 300)) gc.set_alpha(0.3) gc.set_stroke_color((1.0, 0.0, 0.0)) gc.set_fill_color((0.0, 1.0, 0.0)) gc.rect(95, 95, 10, 10) gc.fill_path() draw_ellipse(gc, 100, 100, 35.0, 25.0, pi / 6) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def simple(): gc = GraphicsContext((499, 500)) gc.set_fill_color((0, 0, 0)) gc.rect(99, 100, 300, 300) gc.draw_path() # directly manipulate the underlying Numeric array. # The color tuple is expressed as BGRA. gc.bmp_array[:99, :100] = (139, 60, 71, 255) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def simple(): gc = GraphicsContext((100, 100)) gc.clear() gc.set_line_cap(constants.CAP_SQUARE) gc.set_line_join(constants.JOIN_MITER) gc.set_stroke_color((1, 0, 0)) gc.set_fill_color((0, 0, 1)) gc.rect(0, 0, 30, 30) gc.draw_path() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def stars(): gc = GraphicsContext((500, 500)) gc.translate_ctm(250, 300) add_star(gc) gc.draw_path() gc.translate_ctm(0, -100) add_star(gc) gc.set_fill_color((0.0, 0.0, 1.0)) gc.draw_path(constants.EOF_FILL_STROKE) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( fid.name, resist_width="weak", resist_height="weak" ) return image
def _create_window(self): path = os.path.join(THIS_DIR, 'deepfield.jpg') image = Image.from_file(path, resist_width='weak', resist_height='weak') container = ConstraintsContainer(bounds=[500, 500]) container.add(image) ratio = float(image.data.shape[1])/image.data.shape[0] container.layout_constraints = [ image.left == container.contents_left, image.right == container.contents_right, image.top == container.contents_top, image.bottom == container.contents_bottom, image.layout_width == ratio*image.layout_height, ] return Window(self, -1, component=container)
def _create_component(self): file_path = rect() image = Image.from_file(file_path, resist_width='weak', resist_height='weak') container = ConstraintsContainer(bounds=[500, 500]) container.add(image) ratio = float(image.data.shape[1]) / image.data.shape[0] container.layout_constraints = [ image.left == container.contents_left, image.right == container.contents_right, image.top == container.contents_top, image.bottom == container.contents_bottom, image.layout_width == ratio * image.layout_height, ] return container
def _create_component(self): path = os.path.join(THIS_DIR, "deepfield.jpg") image = Image.from_file(path, resist_width="weak", resist_height="weak") container = ConstraintsContainer(bounds=[500, 500]) container.add(image) ratio = float(image.data.shape[1]) / image.data.shape[0] container.layout_constraints = [ image.left == container.contents_left, image.right == container.contents_right, image.top == container.contents_top, image.bottom == container.contents_bottom, image.layout_width == ratio * image.layout_height, ] return container
def stars(): gc = GraphicsContext((500, 500)) with gc: gc.set_alpha(0.3) gc.set_stroke_color((1.0, 0.0, 0.0)) gc.set_fill_color((0.0, 1.0, 0.0)) for i in range(0, 600, 5): with gc: gc.translate_ctm(i, i) gc.rotate_ctm(i * pi / 180.0) add_star(gc) gc.draw_path() gc.set_fill_color((0.5, 0.5, 0.5, 0.4)) gc.rect(150, 150, 200, 200) gc.fill_path() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") return image
def test_init_bad_shape(self): data = np.zeros(shape=(256, 256), dtype='uint8') with self.assertRaises(TraitError): Image(data=data)
class ImageTest(unittest.TestCase, UnittestTools): def setUp(self): self.data = np.empty(shape=(128, 256, 4), dtype='uint8') self.data[:, :, 0] = np.arange(256) self.data[:, :, 1] = np.arange(128)[:, np.newaxis] self.data[:, :, 2] = np.arange(256)[::-1] self.data[:, :, 3] = np.arange(128)[::-1, np.newaxis] self.image_24 = Image(self.data[..., :3]) self.image_32 = Image(self.data) def test_fromfile_png_rgb(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basn2c08.png') image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 3)) self.assertEqual(image.format, 'rgb24') def test_fromfile_png_rgba(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basi6a08.png') image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 4)) self.assertEqual(image.format, 'rgba32') def test_init_bad_shape(self): data = np.zeros(shape=(256, 256), dtype='uint8') with self.assertRaises(TraitError): Image(data=data) def test_init_bad_dtype(self): data = np.array(['red'] * 65536).reshape(128, 128, 4) with self.assertRaises(TraitError): Image(data=data) def test_set_bad_shape(self): data = np.zeros(shape=(256, 256), dtype='uint8') with self.assertRaises(TraitError): self.image_32.data = data def test_set_bad_dtype(self): data = np.array(['red'] * 65536).reshape(128, 128, 4) with self.assertRaises(TraitError): self.image_32.data = data def test_format(self): self.assertEqual(self.image_24.format, 'rgb24') self.assertEqual(self.image_32.format, 'rgba32') def test_format_change(self): image = self.image_24 with self.assertTraitChanges(image, 'format'): image.data = self.data self.assertEqual(self.image_24.format, 'rgba32') def test_bounds_default(self): self.assertEqual(self.image_24.bounds, [256, 128]) self.assertEqual(self.image_32.bounds, [256, 128]) def test_bounds_overrride(self): image = Image(self.data, bounds=[200, 100]) self.assertEqual(image.bounds, [200, 100]) def test_size_hint(self): self.assertEqual(self.image_24.layout_size_hint, (256, 128)) self.assertEqual(self.image_32.layout_size_hint, (256, 128)) def test_size_hint_change(self): data = np.zeros(shape=(256, 128, 3), dtype='uint8') image = self.image_24 with self.assertTraitChanges(image, 'layout_size_hint'): image.data = data self.assertEqual(self.image_24.layout_size_hint, (128, 256)) def test_image_gc_24(self): # this is non-contiguous, because data comes from slice image_gc = self.image_24._image assert_array_equal(image_gc.bmp_array, self.data[..., :3]) def test_image_gc_32(self): # this is contiguous image_gc = self.image_32._image assert_array_equal(image_gc.bmp_array, self.data) def test_draw_24(self): gc = GraphicsContext((256, 128), pix_format='rgb24') self.image_24.draw(gc) # if test is failing, uncomment this line to see what is drawn #gc.save('test_image_draw_24.png') # smoke test: image isn't all white assert_array_equal(gc.bmp_array[..., :3], self.data[..., :3]) gc2 = GraphicsContext((256, 128), pix_format='rgba32') self.image_24.draw(gc2) assert_array_equal(gc2.bmp_array[..., :3], self.data[..., :3]) def test_draw_32(self): gc = GraphicsContext((256, 128), pix_format='rgba32') self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_32.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 128, 4), dtype='uint8') * 255 self.assertFalse(np.array_equal(white_image, gc.bmp_array)) def test_draw_stretched(self): gc = GraphicsContext((256, 256), pix_format='rgba32') self.image_32.bounds = [128, 258] self.image_32.position = [128, 0] self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_stretched.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 256, 4), dtype='uint8') * 255 self.assertFalse(np.array_equal(white_image, gc.bmp_array)) # left half of the image *should* be white assert_array_equal(gc.bmp_array[:, :128, :], white_image[:, :128, :])
def test_init_bad_dtype(self): data = np.array(['red'] * 65536).reshape(128, 128, 4) with self.assertRaises(TraitError): Image(data=data)
def test_bounds_overrride(self): image = Image(self.data, bounds=[200, 100]) self.assertEqual(image.bounds, [200, 100])
class ImageTest(unittest.TestCase, UnittestTools): def setUp(self): self.data = np.empty(shape=(128, 256, 4), dtype='uint8') self.data[:, :, 0] = np.arange(256) self.data[:, :, 1] = np.arange(128)[:, np.newaxis] self.data[:, :, 2] = np.arange(256)[::-1] self.data[:, :, 3] = np.arange(128)[::-1, np.newaxis] self.image_24 = Image(self.data[..., :3]) self.image_32 = Image(self.data) def test_fromfile_png_rgb(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basn2c08.png') image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 3)) self.assertEqual(image.format, 'rgb24') def test_fromfile_png_rgba(self): # basic smoke test - assume that kiva.image does the right thing path = os.path.join(data_dir, 'PngSuite', 'basi6a08.png') image = Image.from_file(path) self.assertEqual(image.data.shape, (32, 32, 4)) self.assertEqual(image.format, 'rgba32') def test_init_bad_shape(self): data = np.zeros(shape=(256, 256), dtype='uint8') with self.assertRaises(TraitError): Image(data=data) def test_init_bad_dtype(self): data = np.array(['red']*65536).reshape(128, 128, 4) with self.assertRaises(TraitError): Image(data=data) def test_set_bad_shape(self): data = np.zeros(shape=(256, 256), dtype='uint8') with self.assertRaises(TraitError): self.image_32.data = data def test_set_bad_dtype(self): data = np.array(['red']*65536).reshape(128, 128, 4) with self.assertRaises(TraitError): self.image_32.data = data def test_format(self): self.assertEqual(self.image_24.format, 'rgb24') self.assertEqual(self.image_32.format, 'rgba32') def test_format_change(self): image = self.image_24 with self.assertTraitChanges(image, 'format'): image.data = self.data self.assertEqual(self.image_24.format, 'rgba32') def test_bounds_default(self): self.assertEqual(self.image_24.bounds, [256, 128]) self.assertEqual(self.image_32.bounds, [256, 128]) def test_bounds_overrride(self): image = Image(self.data, bounds=[200, 100]) self.assertEqual(image.bounds, [200, 100]) def test_size_hint(self): self.assertEqual(self.image_24.layout_size_hint, (256, 128)) self.assertEqual(self.image_32.layout_size_hint, (256, 128)) def test_size_hint_change(self): data = np.zeros(shape=(256, 128, 3), dtype='uint8') image = self.image_24 with self.assertTraitChanges(image, 'layout_size_hint'): image.data = data self.assertEqual(self.image_24.layout_size_hint, (128, 256)) def test_image_gc_24(self): # this is non-contiguous, because data comes from slice image_gc = self.image_24._image assert_array_equal(image_gc.bmp_array, self.data[..., :3]) def test_image_gc_32(self): # this is contiguous image_gc = self.image_32._image assert_array_equal(image_gc.bmp_array, self.data) def test_draw_24(self): gc = GraphicsContext((256, 128), pix_format='rgb24') self.image_24.draw(gc) # if test is failing, uncomment this line to see what is drawn #gc.save('test_image_draw_24.png') # smoke test: image isn't all white assert_array_equal(gc.bmp_array[..., :3], self.data[..., :3]) gc2 = GraphicsContext((256, 128), pix_format='rgba32') self.image_24.draw(gc2) assert_array_equal(gc2.bmp_array[..., :3], self.data[..., :3]) def test_draw_32(self): gc = GraphicsContext((256, 128), pix_format='rgba32') self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_32.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 128, 4), dtype='uint8')*255 self.assertFalse(np.array_equal(white_image, gc.bmp_array)) def test_draw_stretched(self): gc = GraphicsContext((256, 256), pix_format='rgba32') self.image_32.bounds = [128, 258] self.image_32.position = [128, 0] self.image_32.draw(gc) # if test is failing, uncommetn this line to see what is drawn #gc.save('test_image_draw_stretched.png') # smoke test: image isn't all white # XXX actually compute what it should look like with alpha transfer white_image = np.ones(shape=(256, 256, 4), dtype='uint8')*255 self.assertFalse(np.array_equal(white_image, gc.bmp_array)) # left half of the image *should* be white assert_array_equal(gc.bmp_array[:, :128, :], white_image[:, :128, :])