def test_bad_method_args(): canvas = agg.CanvasG8(np.zeros((1, 1), dtype=np.uint8)) pix_format = agg.PixelFormat.Gray8 gs = agg.GraphicsState() path = agg.Path() transform = agg.Transform() with pytest.raises(TypeError): canvas.draw_image(None, pix_format, transform, gs) with pytest.raises(TypeError): canvas.draw_image(canvas.array, None, transform, gs) with pytest.raises(TypeError): canvas.draw_image(canvas.array, pix_format, None, gs) with pytest.raises(TypeError): canvas.draw_image(canvas.array, pix_format, transform, None) # But this version should work canvas.draw_image(canvas.image, None, transform, gs) with pytest.raises(TypeError): canvas.draw_shape(None, transform, gs) with pytest.raises(TypeError): canvas.draw_shape(path, None, gs) with pytest.raises(TypeError): canvas.draw_shape(path, transform, None) text = "Hello!" font = agg.Font("Times New Roman", 12.0, agg.FontCacheType.RasterFontCache) with pytest.raises(TypeError): canvas.draw_text(text, None, transform, gs) with pytest.raises(TypeError): canvas.draw_text(text, font, None, gs) with pytest.raises(TypeError): canvas.draw_text(text, font, transform, None)
def draw_mask(image_shape, geometry, antialias=False): """Draw a mask (0-255) from a given celiagg path. Requires celiagg installed. Note: to produce a True/False mask from the output, simply do the following: mask = draw_mask(image_shape, geometry) bool_mask = mask > 0 Parameters: image_shape: shape of the resulting image geometry: celiagg VertexSource class, such as celiagg.Path or celiagg.BSpline, containing geometry to draw antialias: if False (default), output contains only 0 and 255. If True, output will be antialiased (better for visualization). Returns: mask array of dtype numpy.uint8 """ import celiagg image = numpy.zeros(image_shape, dtype=numpy.uint8, order='F') # NB celiagg uses (h, w) C-order convention for image shapes, so give it the transpose canvas = celiagg.CanvasG8(image.T) state = celiagg.GraphicsState(drawing_mode=celiagg.DrawingMode.DrawFill, anti_aliased=antialias) fill = celiagg.SolidPaint(1,1,1) transform = celiagg.Transform() canvas.draw_shape(geometry, transform, state, fill=fill) return image
def test_stencil_size_mismatch(): canvas = agg.CanvasRGB24(np.zeros((4, 5, 3), dtype=np.uint8)) stencil_canvas = agg.CanvasG8(np.zeros((1, 2), dtype=np.uint8)) gs = agg.GraphicsState(stencil=stencil_canvas.image) path = agg.Path() transform = agg.Transform() with pytest.raises(agg.AggError): canvas.draw_shape(path, transform, gs)
def test_no_text_draw_text_failure(): if agg.HAS_TEXT: return buffer = np.zeros((1, 1), dtype=np.uint8) canvas = agg.CanvasG8(buffer) transform = agg.Transform() line_paint = agg.SolidPaint(1.0, 1.0, 1.0) gs = agg.GraphicsState() with pytest.raises(RuntimeError): canvas.draw_text("", None, transform, line_paint, line_paint, gs)
def _clip_impl(self, shape, drawing_mode): """ Internal implementation for the complex clipping methods. """ size = (self._height, self._width) stencil = agg.CanvasG8(np.empty(size, dtype=np.uint8), bottom_up=True) stencil.clear(0.0, 0.0, 0.0) clip_box = agg.Rect(0, 0, self._width, self._height) gs = agg.GraphicsState(drawing_mode=drawing_mode, clip_box=clip_box) paint = agg.SolidPaint(1.0, 1.0, 1.0) stencil.draw_shape(shape, self.transform, gs, stroke=paint, fill=paint) self.canvas_state.stencil = stencil.image
def make_mask(metadata, mask_file): spine_tck = metadata['spine_tck'] width_tck = metadata['width_tck'] outline = spline_geometry.outline(spine_tck, width_tck, num_points=400)[-1] image = numpy.zeros( (1040, 1388), dtype=numpy.uint8) # Celiagg convention: image.shape = (H, W) canvas = celiagg.CanvasG8(image) path = celiagg.Path() path.lines(outline) path.close() canvas.draw_shape(path, AGG_TRANSFORM, AGG_STATE, fill=AGG_PAINT, stroke=AGG_TRANSPARENT) freeimage.write( image.T, mask_file ) # freeimage convention: image.shape = (W, H). So take transpose.
def canvas(): buffer = np.zeros((5, 5), dtype=np.uint8) return agg.CanvasG8(buffer)
angles = np.linspace(0, 2 * np.pi, num=5, endpoint=False) pts = np.stack([np.cos(angles), np.sin(angles)]).T * SIZE / 2 + SIZE / 2 star_shape = agg.Path() star_shape.lines([pts[i] for i in (0, 2, 4, 1, 3)]) star_shape.close() star_transform = agg.Transform() star_transform.translate(SIZE / 2, SIZE / 2) star_transform.rotate(-np.pi / 2) star_transform.translate(-SIZE / 2, -SIZE / 2) big_box = agg.Path() big_box.rect(0, 0, SIZE, SIZE) small_box = agg.Path() small_box.rect(0, 0, SIZE / 2, SIZE / 2) stencil_canvas = agg.CanvasG8(np.zeros((SIZE, SIZE), dtype=np.uint8)) canvas = agg.CanvasRGB24(np.zeros((SIZE, SIZE, 3), dtype=np.uint8)) gs = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawFill, line_width=6.0) transform = agg.Transform() blue_paint = agg.SolidPaint(0.1, 0.1, 1.0) white_paint = agg.SolidPaint(1.0, 1.0, 1.0) stops = ((0.0, 0.0, 0.0, 0.0, 1.0), (1.0, 0.3, 0.3, 0.75, 1.0)) bw_grad = agg.LinearGradientPaint(0, 0, 2, 5, stops, agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) stops = ((0.0, 1.0, 1.0, 1.0, 1.0), (1.0, 0.7, 0.7, 0.25, 1.0)) lin_grad = agg.LinearGradientPaint(0, 0, 2, 5, stops, agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) stencil_canvas.clear(0, 0, 0)