Skip to content

Commit 467f21b

Browse files
committed
Add example
1 parent 84c8547 commit 467f21b

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

examples/binary_image.ipynb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Draw a NumPy array directly on the Canvas with `put_image_data`"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"from ipycanvas import Canvas, hold_canvas"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"dx, dy = 0.01, 0.01\n",
27+
"\n",
28+
"y, x = np.mgrid[slice(1, 5 + dy, dy),\n",
29+
" slice(1, 5 + dx, dx)]\n",
30+
"\n",
31+
"z = np.sin(x)**5 + np.cos(5 + y*x) * np.cos(x)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"min = np.min(z)\n",
41+
"max = np.max(z)\n",
42+
"\n",
43+
"def scale(value):\n",
44+
" scaled_value = (value - min) / (max - min) \n",
45+
" return 255 if value > max else scaled_value * 255\n",
46+
"\n",
47+
"vecscale = np.vectorize(scale)"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"zz = np.stack((np.zeros_like(z), vecscale(z), vecscale(z)), axis=2)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"canvas = Canvas(size=(zz.shape[0], zz.shape[1]))\n",
66+
"canvas"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"canvas.put_image_data(zz, 0, 0)"
76+
]
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.7.3"
96+
}
97+
},
98+
"nbformat": 4,
99+
"nbformat_minor": 4
100+
}

0 commit comments

Comments
 (0)