| 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 os\n", |
| 33 |
"import ipywidgets as widgets\n", |
| 34 |
"\n", |
| 35 |
"import bqplot.pyplot as plt\n", |
| 36 |
"from bqplot import LinearScale" |
| 37 |
] |
| 38 |
}, |
| 39 |
{ |
| 40 |
"cell_type": "code", |
| 41 |
"execution_count": null, |
| 42 |
"metadata": {}, |
| 43 |
"outputs": [], |
| 44 |
"source": [ |
| 45 |
"image_path = os.path.abspath('../../data_files/trees.jpg')\n", |
| 46 |
"\n", |
| 47 |
"with open(image_path, 'rb') as f:\n", |
| 48 |
" raw_image = f.read()\n", |
| 49 |
"ipyimage = widgets.Image(value=raw_image, format='jpg')\n", |
| 50 |
"ipyimage" |
| 51 |
] |
| 52 |
}, |
| 53 |
{ |
| 54 |
"cell_type": "markdown", |
| 55 |
"metadata": {}, |
| 56 |
"source": [ |
| 57 |
"## Using pyplot's imshow to display the image" |
| 58 |
] |
| 59 |
}, |
| 60 |
{ |
| 61 |
"cell_type": "code", |
| 62 |
"execution_count": null, |
| 63 |
"metadata": {}, |
| 64 |
"outputs": [], |
| 65 |
"source": [ |
| 66 |
"plt.figure(padding_y=0)\n", |
| 67 |
"axes_options = {'x': {'visible': False}, 'y': {'visible': False}}\n", |
| 68 |
"plt.imshow(image_path, 'filename')\n", |
| 69 |
"plt.show()" |
| 70 |
] |
| 71 |
}, |
| 72 |
{ |
| 73 |
"cell_type": "markdown", |
| 74 |
"metadata": {}, |
| 75 |
"source": [ |
| 76 |
"### Displaying the image inside a bqplot Figure" |
| 77 |
] |
| 78 |
}, |
| 79 |
{ |
| 80 |
"cell_type": "code", |
| 81 |
"execution_count": null, |
| 82 |
"metadata": {}, |
| 83 |
"outputs": [], |
| 84 |
"source": [ |
| 85 |
"fig = plt.figure(title='Trees', padding_x=0, padding_y=0)\n", |
| 86 |
"image = plt.imshow(ipyimage, 'widget')\n", |
| 87 |
"fig" |
| 88 |
] |
| 89 |
}, |
| 90 |
{ |
| 91 |
"cell_type": "markdown", |
| 92 |
"metadata": {}, |
| 93 |
"source": [ |
| 94 |
"### Mixing with other marks\n", |
| 95 |
"\n", |
| 96 |
"`Image` is a mark like any other, so they can be mixed and matched together." |
| 97 |
] |
| 98 |
}, |
| 99 |
{ |
| 100 |
"cell_type": "code", |
| 101 |
"execution_count": null, |
| 102 |
"metadata": {}, |
| 103 |
"outputs": [], |
| 104 |
"source": [ |
| 105 |
"fig = plt.figure(padding_x=0, padding_y=0)\n", |
| 106 |
"plt.scales(scales={'x': LinearScale(min=-1, max=2), \n", |
| 107 |
" 'y': LinearScale(min=-0.5, max=2)})\n", |
| 108 |
"image = plt.imshow(ipyimage, format='widget')\n", |
| 109 |
"plt.plot([0, 1, 1, 0, 0], [0, 0, 1, 1, 0], 'r')\n", |
| 110 |
"fig" |
| 111 |
] |
| 112 |
}, |
| 113 |
{ |
| 114 |
"cell_type": "markdown", |
| 115 |
"metadata": {}, |
| 116 |
"source": [ |
| 117 |
"Its traits (attributes) will also respond dynamically to a change from the backend" |
| 118 |
] |
| 119 |
}, |
| 120 |
{ |
| 121 |
"cell_type": "code", |
| 122 |
"execution_count": null, |
| 123 |
"metadata": {}, |
| 124 |
"outputs": [], |
| 125 |
"source": [ |
| 126 |
"# Full screen\n", |
| 127 |
"image.x = [-1, 2]\n", |
| 128 |
"image.y = [-.5, 2]" |
| 129 |
] |
| 130 |
} |
| 131 |
], |
| 132 |
"metadata": { |
| 133 |
"kernelspec": { |
| 134 |
"display_name": "Python 3", |
| 135 |
"language": "python", |
| 136 |
"name": "python3" |
| 137 |
}, |
| 138 |
"language_info": { |
| 139 |
"codemirror_mode": { |
| 140 |
"name": "ipython", |
| 141 |
"version": 3 |
| 142 |
}, |
| 143 |
"file_extension": ".py", |
| 144 |
"mimetype": "text/x-python", |
| 145 |
"name": "python", |
| 146 |
"nbconvert_exporter": "python", |
| 147 |
"pygments_lexer": "ipython3", |
| 148 |
"version": "3.7.6" |
| 149 |
} |
| 150 |
}, |
| 151 |
"nbformat": 4, |
| 152 |
"nbformat_minor": 2 |
| 153 |
} |