
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/scene/contour.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_scene_contour.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_scene_contour.py:


Apply Contour Filter on an Image
================================

Simple use of SceneCanvas to display an Image.

.. GENERATED FROM PYTHON SOURCE LINES 13-76



.. image-sg:: /gallery/scene/images/sphx_glr_contour_001.png
   :alt: contour
   :srcset: /gallery/scene/images/sphx_glr_contour_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Downloading data from https://raw.githubusercontent.com/vispy/demo-data/main/mona_lisa/mona_lisa_sm.png (818 kB)

    [...                                     ] 7.82614 | downloading   
    [......                                  ] 15.65228 / downloading   
    [.........                               ] 23.47841 - downloading   
    [............                            ] 31.30455 \ downloading   
    [...............                         ] 39.13069 | downloading   
    [..................                      ] 46.95683 / downloading   
    [.....................                   ] 54.78296 - downloading   
    [.........................               ] 62.60910 \ downloading   
    [............................            ] 70.43524 | downloading   
    [...............................         ] 78.26138 / downloading   
    [..................................      ] 86.08752 - downloading   
    [.....................................   ] 93.91365 \ downloading   
    [........................................] 100.00000 | downloading   
    File saved as /tmp/tmp.NVC9tXepCc/.vispy/data/mona_lisa/mona_lisa_sm.png.






|

.. code-block:: default

    import sys

    from vispy import scene, app
    from vispy.visuals.filters import IsolineFilter
    from vispy.io import load_data_file, read_png

    canvas = scene.SceneCanvas(keys='interactive')
    canvas.size = 600, 800
    canvas.show()

    # Set up a viewbox to display the image with interactive pan/zoom
    view = canvas.central_widget.add_view()

    interpolation = 'cubic'
    img_data = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))
    image = scene.visuals.Image(img_data, interpolation=interpolation,
                                parent=view.scene, method='impostor')
    level = 10
    iso = IsolineFilter(level=level, width=1., color='white')

    # Set 2D camera (the camera will scale to the contents in the scene)
    view.camera = scene.PanZoomCamera(aspect=1)
    # flip y-axis to have correct aligment
    view.camera.flip = (0, 1, 0)
    # select face part
    view.camera.rect = (160, 130, 240, 200)

    canvas.title = ('Spatial Filtering using %s Filter - Isoline %d level'
                    % (image.interpolation, iso.level))

    # get interpolation functions from Image
    names = image.interpolation_functions
    act = names.index(interpolation)


    # Implement key presses
    @canvas.events.key_press.connect
    def on_key_press(event):
        global act, level, first, interpolation
        if event.key in ['Left', 'Right']:
            if event.key == 'Right':
                step = 1
            else:
                step = -1
            act = (act + step) % len(names)
            image.interpolation = names[act]

        if event.key in ['Up', 'Down']:
            iso.level += 1 if event.key == 'Up' else -1
        canvas.title = ('Spatial Filtering using %s Filter - Isoline %d level'
                        % (image.interpolation, iso.level))
        canvas.update()


    # attaching of isoline filter via timer
    def on_timer1(event):
        image.attach(iso)
        canvas.update()

    timer1 = app.Timer('auto', iterations=1, connect=on_timer1, start=True)

    if __name__ == '__main__' and sys.flags.interactive == 0:
        app.run()


.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_gallery_scene_contour.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: contour.py <contour.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: contour.ipynb <contour.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
