def __init__(self, size, *args, **kwargs): super().__init__() self._width = size[0] self._height = size[1] self.pix_format = kwargs.get("pix_format", "rgba32") shape = (self._height, self._width, 4) buffer = np.zeros(shape, dtype=np.uint8) self._buffer = buffer self._image = blend2d.Image(buffer) self.gc = blend2d.Context(self._image) # Graphics state self.path = blend2d.Path() self.font = None self._kiva_font = None self.text_pos = (0, 0) self.text_drawing_mode = constants.TEXT_FILL # flip y / HiDPI self.base_scale = kwargs.pop("base_pixel_scale", 1) self.gc.translate(0, size[1]) self.gc.scale(self.base_scale, -self.base_scale) # Lock it in self.gc.user_to_meta()
def spiral(size, hue, sat, val): array = np.empty((size[1], size[0], 4), dtype=np.uint8) image = blend2d.Image(array) canvas = blend2d.Context(image) circle = blend2d.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) canvas.clear() # fill with black for idx, offset in enumerate(offsets): canvas.set_fill_style(spectrum[idx]) radius = np.pi * offset / CIRCLE_COUNT scale = radius / CIRCLE_SIZE for i in range(CIRCLE_COUNT): if ((idx + i) % 2) == 0: continue canvas.reset_matrix() canvas.translate(size[0] / 2 + offset * centers[i, 0], size[1] / 2 + offset * centers[i, 1]) canvas.scale(scale, scale) canvas.fill_path(circle) # BGRA -> RGBA array[:, :, [0, 1, 2]] = array[:, :, [2, 1, 0]] imsave('spiral.png', array)
context.set_fill_style((1.0, 1.0, 1.0)) context.fill_all() path = blend2d.Path() for (x0, x1), (y0, y1), color in zip(xs, ys, colors): context.set_stroke_style(color) path.reset() path.move_to(x0, y0) path.line_to(x1, y1) context.stroke_path(path) if __name__ == '__main__': image_array = np.empty((256, 256, 4), dtype=np.uint8) image = blend2d.Image(image_array) image_context = blend2d.Context(image) random_lines(image_context, 256, 256) array = np.empty((512, 512, 4), dtype=np.uint8) context = blend2d.Context(blend2d.Image(array)) context.clear() src = blend2d.Rect(0, 0, *image_array.shape[:2]) dst = blend2d.Rect(0, 0, 100, 100) context.blit_scaled_image(dst, image, src) context.rotate(-np.pi / 12) context.blit_image((10.0, 100.0), image, src) imsave('image.png', array)
import blend2d import numpy as np from skimage.color import hsv2rgb from skimage.io import imsave if __name__ == '__main__': array = np.empty((500, 500, 4), dtype=np.uint8) image = blend2d.Image(array) canvas = blend2d.Context(image) canvas.clear() canvas.set_stroke_width(20.0) path = blend2d.Path() path.move_to(100, 0) path.quadric_to(125, 25, 250, 0) path.quadric_to(375, -25, 400, 0) caps = { blend2d.StrokeCap.CAP_BUTT, blend2d.StrokeCap.CAP_SQUARE, blend2d.StrokeCap.CAP_ROUND, blend2d.StrokeCap.CAP_ROUND_REV, blend2d.StrokeCap.CAP_TRIANGLE, blend2d.StrokeCap.CAP_TRIANGLE_REV, } for i, cap in enumerate(caps): with canvas: color = hsv2rgb([[i / len(caps), 0.75, 0.75]])[0] canvas.set_stroke_style(color)
context.set_fill_style((1.0, 1.0, 1.0)) context.fill_all() path = blend2d.Path() for (x0, x1), (y0, y1), color in zip(xs, ys, colors): context.set_stroke_style(color) path.reset() path.move_to(x0, y0) path.line_to(x1, y1) context.stroke_path(path) if __name__ == '__main__': pattern_image = blend2d.Image(np.empty((256, 256, 4), dtype=np.uint8)) pattern_context = blend2d.Context(pattern_image) random_lines(pattern_context, 256, 256) array = np.empty((512, 512, 4), dtype=np.uint8) image = blend2d.Image(array) context = blend2d.Context(image) context.clear() area = blend2d.RectI(0, 0, 256, 256) matrix = blend2d.Matrix2D() matrix.rotate(128.0, 128.0, 45.0) matrix.scale(0.25, 0.25) pattern = blend2d.Pattern(pattern_image, area, blend2d.ExtendMode.REPEAT, matrix) context.set_fill_style(pattern)
import blend2d import numpy as np from skimage.io import imsave if __name__ == '__main__': array = np.empty((256, 256, 4), dtype=np.uint8) image = blend2d.Image(array) context = blend2d.Context(image) gradient = blend2d.LinearGradient(0, 0, 256, 256) gradient.extend_mode = blend2d.ExtendMode.PAD gradient.add_stop(0.0, (1.0, 1.0, 1.0)) gradient.add_stop(0.5, (1.0, 0.69, 0.0)) gradient.add_stop(1.0, (1.0, 0.0, 0.0)) context.set_fill_style(gradient) context.fill_all() circle = blend2d.Path() circle.ellipse(128, 128, 64, 64) context.set_comp_op(blend2d.CompOp.EXCLUSION) context.set_fill_style((0.0, 1.0, 1.0)) context.fill_path(circle) imsave('gradient.png', array)