.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/events/image_data_event.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_events_image_data_event.py: Image data event ================ Example showing how to add an event handler to an ImageGraphic to capture when the data changes. .. GENERATED FROM PYTHON SOURCE LINES 7-56 .. image-sg:: /_gallery/events/images/sphx_glr_image_data_event_001.webp :alt: image data event :srcset: /_gallery/events/images/sphx_glr_image_data_event_001.webp :class: sphx-glr-single-img .. code-block:: Python # test_example = false import fastplotlib as fpl import imageio.v3 as iio from scipy.ndimage import gaussian_filter rgb_weights = [0.299, 0.587, 0.114] # load images, convert to grayscale img1 = iio.imread("imageio:wikkie.png") @ rgb_weights img2 = iio.imread("imageio:astronaut.png") @ rgb_weights # Create a figure figure = fpl.Figure( shape=(1, 2), size=(700, 560), names=["image", "gaussian filtered image"] ) # create image graphics image_raw = figure[0, 0].add_image(img1) image_filt = figure[0, 1].add_image(gaussian_filter(img1, sigma=5)) # show the plot figure.show() # add event handler @image_raw.add_event_handler("data") def data_changed(ev: fpl.GraphicFeatureEvent): # get the new image data new_img = ev.info["value"] # set the filtered image graphic image_filt.data = gaussian_filter(new_img, sigma=5) # set the data on the first image graphic # this will trigger the `data_changed()` handler to be called image_raw.data = img2 # NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively # please see our docs for using fastplotlib interactively in ipython and jupyter if __name__ == "__main__": print(__doc__) fpl.loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.688 seconds) .. _sphx_glr_download__gallery_events_image_data_event.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: image_data_event.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: image_data_event.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: image_data_event.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_