| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"## The `Image` Mark\n", |
| 8 |
"\n", |
| 9 |
"`Image` is a `Mark` object, used to visualize images in standard format (png, jpg etc...), in a `bqplot` `Figure`" |
| 10 |
] |
| 11 |
}, |
| 12 |
{ |
| 13 |
"cell_type": "markdown", |
| 14 |
"metadata": {}, |
| 15 |
"source": [ |
| 16 |
"It takes as input an `ipywidgets` [Image](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html#Image) widget" |
| 17 |
] |
| 18 |
}, |
| 19 |
{ |
| 20 |
"cell_type": "markdown", |
| 21 |
"metadata": {}, |
| 22 |
"source": [ |
| 23 |
"### The ipywidgets Image" |
| 24 |
] |
| 25 |
}, |
| 26 |
{ |
| 27 |
"cell_type": "code", |
| 28 |
"execution_count": null, |
| 29 |
"metadata": {}, |
| 30 |
"outputs": [], |
| 31 |
"source": [ |
| 32 |
"import ipywidgets as widgets\n", |
| 33 |
"import os\n", |
| 34 |
"\n", |
| 35 |
"image_path = os.path.abspath('../../data_files/trees.jpg')\n", |
| 36 |
"\n", |
| 37 |
"with open(image_path, 'rb') as f:\n", |
| 38 |
" raw_image = f.read()\n", |
| 39 |
"ipyimage = widgets.Image(value=raw_image, format='jpg')\n", |
| 40 |
"ipyimage" |
| 41 |
] |
| 42 |
}, |
| 43 |
{ |
| 44 |
"cell_type": "markdown", |
| 45 |
"metadata": {}, |
| 46 |
"source": [ |
| 47 |
"### Displaying the image inside a bqplot Figure" |
| 48 |
] |
| 49 |
}, |
| 50 |
{ |
| 51 |
"cell_type": "code", |
| 52 |
"execution_count": null, |
| 53 |
"metadata": {}, |
| 54 |
"outputs": [], |
| 55 |
"source": [ |
| 56 |
"from bqplot import LinearScale, Figure, Lines, Axis, Image\n", |
| 57 |
"\n", |
| 58 |
"# Create the scales for the image coordinates\n", |
| 59 |
"scales={'x': LinearScale(), 'y': LinearScale()}\n", |
| 60 |
"# Define the bqplot Image mark\n", |
| 61 |
"image = Image(image=ipyimage, scales=scales)\n", |
| 62 |
"# Create the bqplot Figure to display the mark\n", |
| 63 |
"fig = Figure(title='Trees', marks=[image], padding_x=0, padding_y=0)\n", |
| 64 |
"fig" |
| 65 |
] |
| 66 |
}, |
| 67 |
{ |
| 68 |
"cell_type": "markdown", |
| 69 |
"metadata": {}, |
| 70 |
"source": [ |
| 71 |
"### Mixing with other marks\n", |
| 72 |
"\n", |
| 73 |
"`Image` is a mark like any other, so they can be mixed and matched together." |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
"cell_type": "code", |
| 78 |
"execution_count": null, |
| 79 |
"metadata": {}, |
| 80 |
"outputs": [], |
| 81 |
"source": [ |
| 82 |
"scales = {'x': LinearScale(min=-1, max=2), 'y': LinearScale(min=-0.5, max=2)}\n", |
| 83 |
"image = Image(image=ipyimage, scales=scales)\n", |
| 84 |
"lines = Lines(x=[0, 1, 1, 0, 0], y=[0, 0, 1, 1, 0], scales=scales, colors=['red'])\n", |
| 85 |
"fig = Figure(marks=[image, lines], padding_x=0, padding_y=0, animation_duration=1000)\n", |
| 86 |
"fig.axes = [Axis(scale=scales['x']), Axis(scale=scales['y'], orientation='vertical')]\n", |
| 87 |
"fig" |
| 88 |
] |
| 89 |
}, |
| 90 |
{ |
| 91 |
"cell_type": "markdown", |
| 92 |
"metadata": {}, |
| 93 |
"source": [ |
| 94 |
"Its traits (attributes) will also respond dynamically to a change from the backend" |
| 95 |
] |
| 96 |
}, |
| 97 |
{ |
| 98 |
"cell_type": "code", |
| 99 |
"execution_count": null, |
| 100 |
"metadata": {}, |
| 101 |
"outputs": [], |
| 102 |
"source": [ |
| 103 |
"# Full screen\n", |
| 104 |
"image.x = [-1, 2]\n", |
| 105 |
"image.y = [-.5, 2]" |
| 106 |
] |
| 107 |
}, |
| 108 |
{ |
| 109 |
"cell_type": "markdown", |
| 110 |
"metadata": {}, |
| 111 |
"source": [ |
| 112 |
"### Interactions\n", |
| 113 |
"\n", |
| 114 |
"Like other bqplot marks, you can watch for mouse clicks. For the image mark, the position of the click (on the x/y scale set up in the figure) is returned." |
| 115 |
] |
| 116 |
}, |
| 117 |
{ |
| 118 |
"cell_type": "code", |
| 119 |
"execution_count": null, |
| 120 |
"metadata": {}, |
| 121 |
"outputs": [], |
| 122 |
"source": [ |
| 123 |
"def print_event(_, target):\n", |
| 124 |
" print(target)\n", |
| 125 |
" \n", |
| 126 |
"image.on_element_click(print_event)" |
| 127 |
] |
| 128 |
}, |
| 129 |
{ |
| 130 |
"cell_type": "markdown", |
| 131 |
"metadata": {}, |
| 132 |
"source": [ |
| 133 |
"## Pyplot\n", |
| 134 |
"\n", |
| 135 |
"It may seem verbose to first open the image file, create an `ipywidgets` `Image`, then create the scales and so forth.\n", |
| 136 |
"\n", |
| 137 |
"The `pyplot` api does all of that for you, via the `imshow` function." |
| 138 |
] |
| 139 |
}, |
| 140 |
{ |
| 141 |
"cell_type": "code", |
| 142 |
"execution_count": null, |
| 143 |
"metadata": {}, |
| 144 |
"outputs": [], |
| 145 |
"source": [ |
| 146 |
"import bqplot.pyplot as bqp\n", |
| 147 |
"\n", |
| 148 |
"bqp.figure()\n", |
| 149 |
"bqp.imshow(image_path, 'filename')\n", |
| 150 |
"bqp.show()" |
| 151 |
] |
| 152 |
}, |
| 153 |
{ |
| 154 |
"cell_type": "markdown", |
| 155 |
"metadata": {}, |
| 156 |
"source": [ |
| 157 |
"\n", |
| 158 |
"The signature is \n", |
| 159 |
"\n", |
| 160 |
"`bqp.imshow(image, format)`\n", |
| 161 |
"\n", |
| 162 |
"- `image` is the `Image` data, depending on the passed `format`, can be one of:\n", |
| 163 |
" - an instance of an ipywidgets Image\n", |
| 164 |
" - a file name\n", |
| 165 |
" - a raw byte string\n", |
| 166 |
"- `format`: {'widget', 'filename', ...}\n", |
| 167 |
" Type of the input argument.\n", |
| 168 |
" If not 'widget' or 'filename', must be a format supported by the \n", |
| 169 |
" `ipywidgets` `Image`.\n" |
| 170 |
] |
| 171 |
} |
| 172 |
], |
| 173 |
"metadata": { |
| 174 |
"kernelspec": { |
| 175 |
"display_name": "Python 3", |
| 176 |
"language": "python", |
| 177 |
"name": "python3" |
| 178 |
}, |
| 179 |
"language_info": { |
| 180 |
"codemirror_mode": { |
| 181 |
"name": "ipython", |
| 182 |
"version": 3 |
| 183 |
}, |
| 184 |
"file_extension": ".py", |
| 185 |
"mimetype": "text/x-python", |
| 186 |
"name": "python", |
| 187 |
"nbconvert_exporter": "python", |
| 188 |
"pygments_lexer": "ipython3", |
| 189 |
"version": "3.7.6" |
| 190 |
} |
| 191 |
}, |
| 192 |
"nbformat": 4, |
| 193 |
"nbformat_minor": 2 |
| 194 |
} |