def spiral(size, hue, sat, val): canvas = agg.CanvasRGB24(np.zeros((size[1], size[0], 3), dtype=np.uint8)) gs = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawFill) transform = agg.Transform() circle = agg.Path() circle.ellipse(0, 0, CIRCLE_SIZE, CIRCLE_SIZE) divisions = np.linspace(0, 2*np.pi, CIRCLE_COUNT, endpoint=False) centers = np.stack((np.cos(divisions), np.sin(divisions)), axis=1) offsets = compute_offsets(np.sqrt(size[0]**2 + size[1]**2) / 2) color_count = len(offsets) hsv = np.ones((color_count, 1, 3)) hsv[:, 0, 0] = np.linspace(hue[0], hue[1], color_count, endpoint=False) hsv[:, 0, 1] = np.linspace(sat[0], sat[1], color_count, endpoint=False) hsv[:, 0, 2] = np.linspace(val[0], val[1], color_count, endpoint=False) spectrum = hsv2rgb(hsv).reshape(color_count, 3) for idx, offset in enumerate(offsets): paint = agg.SolidPaint(*spectrum[idx]) radius = np.pi * offset / CIRCLE_COUNT scale = radius / CIRCLE_SIZE for i in range(CIRCLE_COUNT): if ((idx + i) % 2) == 0: continue transform.reset() transform.translate(size[0]/2 + offset*centers[i, 0], size[1]/2 + offset*centers[i, 1]) transform.scale(scale, scale) canvas.draw_shape(circle, transform, gs, fill=paint) imsave('spiral.png', canvas.array)
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 terrain(size): canvas = agg.CanvasRGB24(np.empty((size[1], size[0], 3), dtype='uint8'), bottom_up=True) gs = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawFill) sky(size[0], size[1], canvas, gs) xs, ys = gen_horizon(nearest_pow2(size[0] // 4) + 1, size[0], size[1]) draw_horizon(xs, ys / 2, canvas, gs) imsave('terrain.png', canvas.array)
def render(context, scale=1.0): docattrs = context['__document__'] width = parse_united(docattrs.get('width', '100')) * scale height = parse_united(docattrs.get('height', '100')) * scale canvas = agg.CanvasRGB24( np.empty((int(height), int(width), 3), dtype=np.uint8)) canvas.clear(1, 1, 1) for group in context['graphics']: for elem in group.values(): transform = elem.get('transform') or agg.Transform() shape = elem['data'] gs, stroke, fill = interpret_style(elem['style'], context) transform.scale(scale, scale) canvas.draw_shape(shape, transform, gs, stroke=stroke, fill=fill) return canvas.array
def test_cleanup(): # Make sure our array references don't linger buffer = np.zeros((10, 10, 3), dtype=np.uint8) canvas = agg.CanvasRGB24(buffer) weak_buf = weakref.ref(buffer) weak_arr = weakref.ref(canvas.array) assert weak_buf() is weak_arr() del buffer assert weak_buf() is not None del canvas assert weak_buf() is None
def test_clear(): expected = np.zeros((4, 4, 3), dtype=np.uint8) buffer = np.zeros((4, 4, 3), dtype=np.uint8) canvas = agg.CanvasRGB24(buffer) expected[:] = 0 canvas.clear(0, 0, 0) assert_equal(expected, canvas.array) expected[:] = 128 canvas.clear(.5, .5, .5) assert_equal(expected, canvas.array) expected[:] = 255 canvas.clear(1, 1, 1) assert_equal(expected, canvas.array)
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) stencil_canvas.draw_shape(star_shape, star_transform, gs, fill=white_paint)
import celiagg as agg import numpy as np from skimage.io import imsave canvas = agg.CanvasRGB24(np.ones((400, 400, 3), dtype=np.uint8)) state = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawStroke, line_width=10.0) transform = agg.Transform() red_paint = agg.SolidPaint(1.0, 0.0, 0.0) black_paint = agg.SolidPaint(0.0, 0.0, 0.0) font = agg.Font("/Library/Fonts/Verdana.ttf", 96.0, agg.FontCacheType.RasterFontCache) path = agg.Path() path.ellipse(200, 200, 190, 190) canvas.clear(1.0, 1.0, 1.0) canvas.draw_shape(path, transform, state, stroke=red_paint) transform.translate(30.0, 220.0) canvas.draw_text("celiagg", font, transform, state, stroke=black_paint) imsave("example.png", canvas.array)