def app(tester): m = np.ones((16, 32, 3), 'u1') * 0 # m[50:150, :, 1] = 256 m[:, 7:20, 0] = 255 im = c.Image(m) return c.orr([ c.image("", c.Image(m[:,:28]), height=100), c.image("", c.Image(m[:,:29]), height=100), c.image("", c.Image(m[:,:30]), height=100), c.image("", c.Image(m[:,:31]), height=100), tester.pause(), ])
def test_shapes(tester): def content(tf): np.random.seed(0) return c.orr([ # Empty polygonal shapes of all kinds c.draw.polygon([], 'white', tf=tf), c.draw.polygon(np.array([]), 'white', tf=tf), c.draw.polygon(np.zeros((0, 2)), 'white', tf=tf), c.draw.polyline([], 'white', tf=tf), c.draw.polyline(np.array([]), 'white', tf=tf), c.draw.polyline(np.zeros((0, 2)), 'white', tf=tf), c.draw.polygons(np.zeros((0, 123, 2)), 'white', tf=tf), c.draw.polygons(np.zeros((123, 0, 2)), 'white', tf=tf), c.draw.polylines([], 'white', tf=tf), c.draw.polylines(np.zeros((0, 123, 2)), 'white', tf=tf), c.draw.polylines(np.zeros((123, 0, 2)), 'white', tf=tf), c.draw.scatter(np.array([]), 'white', 'x', tf=tf), c.draw.scatter(np.zeros([0]), 'white', 'x', tf=tf), c.draw.scatter(np.zeros([0,2]), 'white', 'x', tf=tf), # Normal polygonal shapes c.draw.polygon(np.array([(0.5,0.5), (0.7,0.5), (0.7,0.7), (0.5,0.7)]), 'white', tf=tf), c.draw.polygon([(0.5,0.5), (0.6,0.5), (0.6,0.6), (0.5,0.6)], 'brown', tf=tf), ]) yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])
def app(image_path, output_path): view = c.Image(Image.open(image_path)) autosave = True try: r = csv.reader(open(output_path, 'r')) pts = [(int(x), int(y)) for x, y in r] except FileNotFoundError: pts = [] while True: tag, value = yield from c.orr([ c.orr_same_line([c.button("Save"), c.text(f"Click to (de)annotate. N: {len(pts)}"), c.checkbox("Autosave", autosave)]), c.image("Image", view, content_gen=c.partial(overlay, np.array(pts).reshape(-1, 2))), ]) if tag == "Image": view = value elif tag == "Autosave": autosave = value elif tag == "Rem": pts.pop(value) elif tag == "Add": pts.append((int(value[0]), int(value[1]))) if tag == "Save" or autosave and tag in ["Rem", "Add"]: with open(output_path, 'w') as f: csv.writer(f).writerows(pts) yield
def app(tester): canvas = np.zeros([1000, 1000, 3], 'u1') im = c.Image(canvas) t = 0 x, y = 0, 0 for i in range(2): if i: yield res = yield from c.orr([ c.image("", im, content_gen=lambda tf: c.draw.rect_filled( x - 40, y - 40, x + 40, y + 40, tuple(np.array([222, 111, 111, 255]) / 255), tf=tf)), c.event(None), ]) t += 0.02 x = int(500 + 400 * np.sin(t)) y = int(500 + 400 * np.cos(t)) canvas[...] = 0 canvas[y - 41:y + 41, x - 41:x + 41] = [111, 222, 111] canvas[y - 40:y + 40, x - 40:x + 40] = [111, 111, 222] im.change_image(canvas)
def test_scatter(tester): def content(tf): np.random.seed(0) return c.orr([ c.draw.scatter(np.random.rand(100, 2), 'white', '+', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'yellow', 'x', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'magenta', 'o', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'green', '.', marker_size=3, tf=tf), ]) yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])
def test_polylines_perf(): side, length = int(np.sqrt(10000)), 4 x, y, t = np.meshgrid(np.linspace(0, 1, side), np.linspace(0, 1, side), np.linspace(0, 2 * np.pi, length, endpoint=False)) polylines = np.stack([np.sin(t) * 1/side/2 + x, np.cos(t) * 1/side/2 + y], axis=3).reshape(-1, length, 2) im = c.Image() def content(tf): return c.draw.polylines(polylines, 'white', tf=tf) while True: yield from c.image("Image", im, content_gen=content) yield
def image_ui(q): i = 0 im1 = c.Image(Image.open("examples/lenna.png")) im2 = copy.deepcopy(im1) while True: i += 1 ret = yield from c.orr([c.listen(q), c.image("This", im2)]) if ret is None: return im1, im2 im2 = ret[1] yield
def app(): im1 = c.Image(Image.open("examples/lenna.png")) im2 = c.Image(Image.open("examples/lenna.png")) while True: k, v = yield from c.orr([ c.button("Hello,"), c.button("world!"), c.orr_same_line([ c.text("Hello,"), c.nothing(), c.text_tooltip("Text tooltip", c.text("world!")), c.text_tooltip("Orr tooltip", c.orr([ c.text_tooltip("Button tooltip", c.button("Button1")), c.button("Button2"), ])), c.orr([ c.button("Button3"), c.button("Button4"), ]), c.text("Finish line."), ]), c.draggable("Drag", c.orr([c.button("Draggable Button"), c.forever(c.button, "Another")])), c.input_text("Hello", "world!", 123), c.collapsing_header("Image", c.orr([ c.tooltip(c.orr([c.text("Hello!"), c.image("", im1, width=300, height=200), c.event(("Key", "Tooltip value"))]), c.image("Im1", im1, width=30, height=20)), c.image("Im2", im2, width=30, height=20), ])), c.input_text("Hello", "world!", 123), c.key_press("Key", glfw.KEY_SPACE), ]) if k == "Im1": im1 = v if k == "Im2": im2 = v print(k, v) yield
def app(): image = Image.open("examples/lenna.png") view = c.Image(image) while True: tag, value = yield from c.orr([ c.text( "Drag using right mouse button,\nscroll using mouse wheel."), c.image("Image", view, content_gen=overlay), ]) if tag == "Image": view = value elif tag == "Rotate": image = image.transpose(Image.ROTATE_270) view.change_image(image) yield
def app(): view = c.Image(Image.open("examples/lenna.png")) lines = [] while True: tag, value = yield from c.orr([ c.text("Create lines by dragging with the left mouse button."), c.image("Image", view, content_gen=c.partial(overlay, deepcopy(lines)), drag_tag="Drag", down_tag="Down"), ]) if tag == "Image": view = value elif tag == "Draw": lines = value yield
def controlee(): view = c.Image(Image.open("examples/lenna.png")) while True: _, view = yield from c.image("Image", view) yield