.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/events/key_events.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_key_events.py: Key Events ========== Move an image around using and change some of its properties using keyboard events. - Use the arrows keys to move the image by changing its offset - Press "v", "g", "p" to change the colormaps (viridis, grey, plasma). - Press "r" to rotate the image +18 degrees (pi / 10 radians) - Press "Shift + R" to rotate the image -18 degrees - Axis of rotation is the origin - Press "-", "=" to decrease/increase the vmin - Press "_", "+" to decrease/increase the vmax We use the ImageWidget here because the histogram LUT tool makes it easy to see the changes in vmin and vmax. .. GENERATED FROM PYTHON SOURCE LINES 20-85 .. image-sg:: /_gallery/events/images/sphx_glr_key_events_001.webp :alt: key events :srcset: /_gallery/events/images/sphx_glr_key_events_001.webp :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/fastplotlib/fastplotlib/fastplotlib/graphics/features/_base.py:18: UserWarning: casting float64 array to float32 warn(f"casting {array.dtype} array to float32") | .. code-block:: Python # test_example = false import numpy as np import fastplotlib as fpl import pygfx import imageio.v3 as iio data = iio.imread("imageio:camera.png") iw = fpl.ImageWidget(data, figure_kwargs={"size": (700, 560)}) image = iw.managed_graphics[0] @iw.figure.renderer.add_event_handler("key_down") def handle_event(ev: pygfx.KeyboardEvent): match ev.key: # change the cmap case "v": image.cmap = "viridis" case "g": image.cmap = "grey" case "p": image.cmap = "plasma" # keys to change vmin/vmax case "-": image.vmin -= 1 case "=": image.vmin += 1 case "_": image.vmax -= 1 case "+": image.vmax += 1 # rotate case "r": image.rotate(np.pi / 10, axis="z") case "R": image.rotate(-np.pi / 10, axis="z") # arrow key events to move the image case "ArrowUp": image.offset = image.offset + [0, -10, 0] # remember y-axis is flipped for images case "ArrowDown": image.offset = image.offset + [0, 10, 0] case "ArrowLeft": image.offset = image.offset + [-10, 0, 0] case "ArrowRight": image.offset = image.offset + [10, 0, 0] iw.show() figure = iw.figure # ignore, this is just so the docs gallery scraper picks up the figure # 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.660 seconds) .. _sphx_glr_download__gallery_events_key_events.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: key_events.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: key_events.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: key_events.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_