def test_cross_outside_canvas_raises_index_error(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=3, height=3) with self.assertRaises(IndexError): canvas.draw_cross(position=(1, 3), color=(1, 1, 1), radius=1) with self.assertRaises(IndexError): canvas.draw_cross(position=(3, 1), color=(1, 1, 1), radius=1)
def test_draw_cross_in_lower_right_corner(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=3, height=3) canvas.draw_cross(position=(2, 2), color=(1, 1, 1), radius=1) layer = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 1]], dtype=np.uint8) expected = np.dstack([layer, layer, layer]) self.assertTrue(np.array_equal(canvas, expected))
def test_mask_region(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(3, 3) region = np.zeros((3, 3), dtype=bool) region[1, 1] = True canvas.mask_region(region, color=(0, 1, 0)) self.assertEqual(np.sum(canvas), 1) self.assertTrue(canvas[1, 1, 1])
def test_blank_canvas(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=4, height=5) self.assertTrue(isinstance(canvas, Canvas)) self.assertTrue(isinstance(canvas, np.ndarray)) self.assertEqual(canvas.shape, (5, 4, 3)) self.assertEqual(canvas.dtype, np.uint8) self.assertEqual(np.sum(canvas), 0)
def test_draw_line_float_input(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(3, 3) layer = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8) expected = np.dstack([layer, layer, layer]) canvas.draw_line((0.2, 0.3), (1.9, 2.2), color=(1, 1, 1)) self.assertTrue(np.array_equal(canvas, expected))
def test_draw_line_color(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(3, 3) layer = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8) expected = np.dstack([layer, layer*2, layer*3]) canvas.draw_line((0, 0), (2, 2), color=(1, 2, 3)) self.assertTrue(np.array_equal(canvas, expected))
def test_text_at_outside_image(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=6, height=6) layer = np.array([ [0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0]], dtype=np.uint8) expected = np.dstack([layer, layer, layer]) canvas.text_at("e", (1, 0), color=(1, 1, 1), antialias=False) self.assertTrue(np.array_equal(canvas, expected))
def test_text_at_antialias(self): from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=6, height=6) layer = np.array( [[0, 38, 209, 233, 75, 0], [0, 182, 77, 48, 212, 0], [0, 239, 255, 255, 249, 0], [0, 239, 21, 0, 0, 0], [0, 176, 145, 10, 0, 0], [0, 31, 192, 246, 213, 0]], dtype=np.uint8) expected = np.dstack([layer, layer, layer]) canvas.text_at("e", (0, 0), color=(255, 255, 255), antialias=True) self.assertTrue(np.array_equal(canvas, expected))
def test_create_annotation_image_from_scratch(self): from jicbioimage.illustrate import Canvas # Create an empty canvas. canvas = Canvas.blank_canvas(50, 75) # Draw a cross on it, centered on pixel 10,15. canvas.draw_cross(position=(10, 15), radius=1, color=(1, 0, 0)) self.assertEqual(np.sum(canvas), 5) self.assertEqual(canvas[10, 15, 0], 1) self.assertEqual(canvas[9, 15, 0], 1) self.assertEqual(canvas[11, 15, 0], 1) self.assertEqual(canvas[10, 14, 0], 1) self.assertEqual(canvas[10, 16, 0], 1)
def generate_annotation(segmentation): areas = [ segmentation.region_by_identifier(i).area for i in segmentation.identifiers ] imin = min(areas) imax = max(areas) ydim, xdim = segmentation.shape canvas = Canvas.blank_canvas(width=xdim, height=ydim) for i in segmentation.identifiers: region = segmentation.region_by_identifier(i) area = region.area color = get_normalised_rgb(area, imin, imax) canvas.mask_region(region.inner.inner, color) return canvas
def write_zslice(zslice, cellinfo, fpath, intensity_method): """Write PNG z-slice.""" ydim, xdim = zslice.shape imin, imax = min_max_cell_intensity(cellinfo, intensity_method) canvas = Canvas.blank_canvas(width=xdim, height=ydim) for i, props in enumerate(cellinfo): cell_intenistity = intensity_method(props) color = get_normalised_rgb_from_cell_intensity(cell_intenistity, imin, imax) region = zslice == props["cell_id"] if np.sum(region) == 0: continue canvas.mask_region(region, color) with open(fpath, "wb") as fh: fh.write(canvas.png())
def test_draw_cross_on_rectangular_canvas(self): # This used to fail when there was an issue confusing the y, x axis. from jicbioimage.illustrate import Canvas canvas = Canvas.blank_canvas(width=3, height=30) canvas.draw_cross(position=(25, 1), color=(1, 1, 1), radius=1) self.assertEqual(np.sum(canvas), 15)