Skip to content

Instantly share code, notes, and snippets.

@BenedictWilkins
Last active March 1, 2020 20:36
Show Gist options
  • Save BenedictWilkins/d053fa09762eaeaf240daa6c25a1fede to your computer and use it in GitHub Desktop.
Save BenedictWilkins/d053fa09762eaeaf240daa6c25a1fede to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display\n",
"\n",
"from ipyevents import Event \n",
"from ipycanvas import Canvas, MultiCanvas\n",
"\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "be9f3f0ea33445be913d979e8efdbb61",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"MultiCanvas(height=100, width=100)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display\n",
"\n",
"from ipyevents import Event \n",
"from ipycanvas import Canvas, MultiCanvas\n",
"\n",
"import numpy as np\n",
"\n",
"class IPyEventMouseMoveHandler:\n",
"\n",
" def __init__(self, widget, callback, snap=(1,1)):\n",
" self.px = self.py = 0\n",
" \n",
" self._snapx = snap[0]\n",
" self._snapy = snap[1]\n",
" self.callback = callback\n",
" \n",
" self.ipyevent = Event(source=widget, watched_events=['mousemove'])\n",
" self.ipyevent.on_dom_event(self)\n",
"\n",
" def __call__(self, event):\n",
" x, y = event['relativeX'] - 2, event['relativeY'] - 2\n",
" x = int(x / self._snapx) * self._snapx\n",
" y = int(y / self._snapy) * self._snapy\n",
"\n",
" if self.px == x and self.py == y:\n",
" return\n",
"\n",
" self.px, self.py = x, y\n",
" self.callback(x, y)\n",
"\n",
"box_shape = (20,20)\n",
"image = np.zeros((100,100,3), dtype=np.uint8)\n",
"\n",
"canvas = MultiCanvas(2, width=image.shape[0], height=image.shape[1])\n",
"canvas[0].put_image_data(image, 0, 0)\n",
"\n",
"bw = box_shape[0]\n",
"bh = box_shape[1]\n",
"\n",
"def callback(x, y):\n",
" canvas[1].clear()\n",
" canvas[1].fill_style = 'white'\n",
" canvas[1].global_alpha = 0.2\n",
" canvas[1].fill_rect(x,y,bw,bh)\n",
"\n",
" canvas[1].global_alpha = 1.\n",
" canvas[1].stroke_style = 'red'\n",
" canvas[1].stroke_rect(x,y,bw,bh)\n",
"\n",
"mmh = IPyEventMouseMoveHandler(canvas, callback, snap=(bw,bh)) \n",
"display(canvas)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5fb049153b404570af795c716c7ad81c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Canvas(height=100, sync_image_data=True, width=100)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"image = np.zeros((100,100,3), dtype=np.uint8)\n",
"\n",
"canvas = Canvas(width=image.shape[0], height=image.shape[1], sync_image_data=True)\n",
"canvas.put_image_data(image, 0, 0)\n",
"\n",
"display(canvas)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#canvas.to_file('/home/ben/Downloads/black_canvas.png')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "747d6d86fe71422eae97c34e18e2c381",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"MultiCanvas(height=100, sync_image_data=True, width=100)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = 10\n",
"y = 10\n",
"box_shape = (20,20)\n",
"image = np.zeros((100,100,3), dtype=np.uint8)\n",
"\n",
"canvas = MultiCanvas(2, width=image.shape[0], height=image.shape[1], sync_image_data=True)\n",
"canvas[0].put_image_data(image, 0, 0)\n",
"\n",
"canvas[1].fill_style = 'white'\n",
"canvas[1].global_alpha = 0.2\n",
"canvas[1].fill_rect(x,y,box_shape[0],box_shape[1])\n",
"\n",
"canvas[1].global_alpha = 1.\n",
"canvas[1].stroke_style = 'red'\n",
"canvas[1].stroke_rect(x,y,box_shape[0],box_shape[1])\n",
" \n",
"display(canvas)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"#canvas.to_file('/home/ben/Downloads/roi_canvas.png')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8774b41a2f704186b78759b461fb994d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"MultiCanvas(height=100, sync_image_data=True, width=100)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = 10\n",
"y = 10\n",
"box_shape = (20,20)\n",
"image = np.zeros((100,100,3), dtype=np.uint8)\n",
"\n",
"canvas = MultiCanvas(2, width=image.shape[0], height=image.shape[1], sync_image_data=True)\n",
"canvas[0].put_image_data(image, 0, 0)\n",
"\n",
"o_canvas = canvas\n",
"canvas = canvas[1]\n",
"\n",
"canvas.stroke_style = 'red'\n",
"canvas.begin_path()\n",
"canvas.arc(50, 50, 30, 0, np.pi * 2, True)\n",
"canvas.stroke()\n",
"\n",
"canvas.begin_path()\n",
"canvas.arc(50, 50, 15, 0, np.pi * 2, True)\n",
"canvas.stroke()\n",
"\n",
"canvas.fill_style = 'white'\n",
"canvas.global_alpha = 0.2\n",
"canvas.begin_path()\n",
"canvas.arc(50, 50, 30, 0, np.pi * 2, True)\n",
"canvas.arc(50, 50, 15, 0, np.pi * 2, True)\n",
"canvas.fill('evenodd')\n",
"\n",
"'''\n",
"canvas.fill('evenodd')\n",
"\n",
"\n",
"#canvas[1].fill_rect(x,y,box_shape[0],box_shape[1])\n",
"\n",
"canvas.stroke_style = 'red'\n",
"canvas[1].begin_path()\n",
"\n",
"canvas[1].stroke()\n",
"'''\n",
"#canvas[1].arc(50, 50, 15, 0, np.pi * 2, True)\n",
"#canvas[1].fill('evenodd')\n",
"\n",
"display(o_canvas)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"#o_canvas.to_file('/home/ben/Downloads/novel_canvas.png')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8f2efdba8a924b26b3728791219b4418",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Canvas(height=100, image_data=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x00d\\x00\\x00\\x00d\\x08\\x06\\x00\\x00\\…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"class IPyEventMouseMoveHandler:\n",
"\n",
" def __init__(self, widget, callback):\n",
" self.px = self.py = 0\n",
" self.callback = callback\n",
" \n",
" self.ipyevent = Event(source=widget, watched_events=['mousemove'])\n",
" self.ipyevent.on_dom_event(self)\n",
"\n",
" def __call__(self, event): #call on mouse move\n",
" x, y = event['relativeX'] - 2, event['relativeY'] - 2\n",
"\n",
" if self.px == x and self.py == y: \n",
" return\n",
"\n",
" self.px, self.py = x, y\n",
" self.callback(x, y)\n",
" \n",
"IPyEventMouseMoveHandler(canvas, lambda x,y: print(x,y))\n",
"\n",
"display(canvas)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"file_extension": ".py",
"kernelspec": {
"display_name": "Python 3.8.1 64-bit ('PhD': conda)",
"language": "python",
"name": "python38164bitphdconda4a8805f6f8044089a89ef6c6f106aeb7"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 2
}
numpy==1.18.*
ipyevents==0.7.0
ipycanvas==0.4.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment