はじめに
jupyter notebook, labで対話的な操作ができる機能(ipywidgets)のドロップダウンで画像を選択して表示する方法について説明する。
コード

解説
モジュールのインポートなど
jupyter labでインタラクティブな操作をするには、冒頭で%matplotlib widgetとする。
jupyter lnotebookの場合は%matplotlib notebookとなる。
所定のGUIで別ウインドウで表示するには、%matplotlibとする。
バージョン
画像データの作成
128 x 128のサイズの画像に強度の高い箇所が複数ある画像を作成する。
空のリストであるim_listに画像データをappendでいれることで画像が3つ入ったリストを作成する。
画像の表示
imshowで画像を表示する。
Dropdownの設定
optionsで[(‘image1’, 0), (‘image2’, 1), (‘image3’, 2)]のようにすると、image1を選択したときに0が返される。valueは初期値となる。
@interactでipywidgetsを有効にする。
f(im_dd)はドロップダウンを選択するたびに実行される。
image0などから任意のものを選択すると、ax.claで画像を消して、imshowで選択した画像を表示する。
image2の場合

image3の場合

参考
Widget List — Jupyter Widgets 8.1.5 documentation
コメント