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.

key events
# test_example = false

import numpy as np
import fastplotlib as fpl
import pygfx
import imageio.v3 as iio

data = iio.imread("imageio:camera.png")

figure = fpl.Figure(size=(700, 560))

image = figure[0, 0].add_image(data)


@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 -= 10
        case "=":
            image.vmin += 10
        case "_":
            image.vmax -= 10
        case "+":
            image.vmax += 10

        # 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]


figure.show()


# NOTE: fpl.loop.run() should not be used for interactive sessions
# See the "JupyterLab and IPython" section in the user guide
if __name__ == "__main__":
    print(__doc__)
    fpl.loop.run()

Total running time of the script: (0 minutes 0.334 seconds)

Gallery generated by Sphinx-Gallery