|
| 1 | +import blend2d |
| 2 | +import numpy as np |
| 3 | +from skimage.io import imsave |
| 4 | + |
| 5 | + |
| 6 | +def random_lines(context, width, height): |
| 7 | + xs = np.random.randint(0, high=width, size=(100, 2)) |
| 8 | + ys = np.random.randint(0, high=height, size=(100, 2)) |
| 9 | + colors = np.random.random_sample(size=(100, 3)) |
| 10 | + |
| 11 | + context.set_fill_style((1.0, 1.0, 1.0)) |
| 12 | + context.fill_all() |
| 13 | + |
| 14 | + path = blend2d.Path() |
| 15 | + for (x0, x1), (y0, y1), color in zip(xs, ys, colors): |
| 16 | + context.set_stroke_style(color) |
| 17 | + path.reset() |
| 18 | + path.move_to(x0, y0) |
| 19 | + path.line_to(x1, y1) |
| 20 | + context.stroke_path(path) |
| 21 | + |
| 22 | + |
| 23 | +if __name__ == '__main__': |
| 24 | + image_array = np.empty((256, 256, 4), dtype=np.uint8) |
| 25 | + image = blend2d.Image(image_array) |
| 26 | + image_context = blend2d.Context(image) |
| 27 | + random_lines(image_context, 256, 256) |
| 28 | + |
| 29 | + array = np.empty((512, 512, 4), dtype=np.uint8) |
| 30 | + context = blend2d.Context(blend2d.Image(array)) |
| 31 | + context.clear() |
| 32 | + |
| 33 | + src = blend2d.Rect(0, 0, *image_array.shape[:2]) |
| 34 | + dst = blend2d.Rect(0, 0, 100, 100) |
| 35 | + context.blit_scaled_image(dst, image, src) |
| 36 | + |
| 37 | + context.rotate(-np.pi/12) |
| 38 | + context.blit_image((10.0, 100.0), image, src) |
| 39 | + |
| 40 | + imsave('image.png', array) |
0 commit comments