Question

Based on the tutorial found here, I was trying to create an IPython 2.0 widget which would let me add one or more horizontal lines to an image, and then move vertically them based on a slider's motion. My current attempt looks like (from within an IPython 2.0 notebook):

from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
import skimage
from skimage import data, filter, io

i = data.coffee()
io.Image(i)

def add_lines(image, line1 = 100, line2 = 200):
  new_image = image
  new_image[line1,:,:] = 0
  new_image[line2,:,:] = 0
  new_image = io.Image(new_image)
  display(new_image)
  return new_image

lims = (0,400,2)
w = interactive(add_lines, image=fixed(i), line1 = lims, line2 = lims)
display(w)

The results look like: lines are added, image is not redrawn

Where, instead, I would like the image to redraw with the same background image and updated line position based on slider value. Using the IPython interactive widget mechanism, is there a way to reuse the 'base' image and redraw it with new lines?

In case it matters, my current installed versions are:

$ ipython --version

2.0.0

$ python --version

Python 2.7.6 :: Anaconda 1.9.0 (x86_64)

Was it helpful?

Solution

You should work on a copy of the original image, like this:

from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
import skimage
from skimage import data, filter, io

i = data.coffee()

def add_lines(image, line1 = 100, line2 = 200):
  new_image = image.copy()  # Work on a copy
  new_image[line1,:,:] = 0
  new_image[line2,:,:] = 0
  new_image = io.Image(new_image)
  display(new_image)

lims = (0,400,2)
interactive(add_lines, image=fixed(i), line1 = lims, line2 = lims)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top