{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%gui qt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Apply Contour Filter on an Image\n\nSimple use of SceneCanvas to display an Image.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n\nfrom vispy import scene, app\nfrom vispy.visuals.filters import IsolineFilter\nfrom vispy.io import load_data_file, read_png\n\ncanvas = scene.SceneCanvas(keys='interactive')\ncanvas.size = 600, 800\ncanvas.show()\n\n# Set up a viewbox to display the image with interactive pan/zoom\nview = canvas.central_widget.add_view()\n\ninterpolation = 'cubic'\nimg_data = read_png(load_data_file('mona_lisa/mona_lisa_sm.png'))\nimage = scene.visuals.Image(img_data, interpolation=interpolation,\n parent=view.scene, method='impostor')\nlevel = 10\niso = IsolineFilter(level=level, width=1., color='white')\n\n# Set 2D camera (the camera will scale to the contents in the scene)\nview.camera = scene.PanZoomCamera(aspect=1)\n# flip y-axis to have correct aligment\nview.camera.flip = (0, 1, 0)\n# select face part\nview.camera.rect = (160, 130, 240, 200)\n\ncanvas.title = ('Spatial Filtering using %s Filter - Isoline %d level'\n % (image.interpolation, iso.level))\n\n# get interpolation functions from Image\nnames = image.interpolation_functions\nact = names.index(interpolation)\n\n\n# Implement key presses\n@canvas.events.key_press.connect\ndef on_key_press(event):\n global act, level, first, interpolation\n if event.key in ['Left', 'Right']:\n if event.key == 'Right':\n step = 1\n else:\n step = -1\n act = (act + step) % len(names)\n image.interpolation = names[act]\n\n if event.key in ['Up', 'Down']:\n iso.level += 1 if event.key == 'Up' else -1\n canvas.title = ('Spatial Filtering using %s Filter - Isoline %d level'\n % (image.interpolation, iso.level))\n canvas.update()\n\n\n# attaching of isoline filter via timer\ndef on_timer1(event):\n image.attach(iso)\n canvas.update()\n\ntimer1 = app.Timer('auto', iterations=1, connect=on_timer1, start=True)\n\nif __name__ == '__main__' and sys.flags.interactive == 0:\n app.run()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 0 }