7
$\begingroup$

I tried to build a simple 'blink comparator' - where you can click on an image repeatedly to switch between two similar images. (An old-fashioned notion in the days of powerful image processing software, it's true.) I think the problem with what I've done so far is the behaviour of the symbols inside a DynamicModule:

image1 = Thumbnail[ExampleData[{"TestImage", "Mandrill"}], 300];
image2 = Thumbnail[
   ColorNegate[ExampleData[{"TestImage", "Mandrill"}]], 300];
DynamicModule[{imageList = {image1, image2}},
 EventHandler[
  Show[
   First[imageList],
   Graphics[
    {
     Text[
      Style[
       "name of First[imageList]]?", 
       14, White],
      ImageDimensions[First[imageList]]/2]
     }
    ]],
  {"MouseDown" :> 
    {
     RotateLeft[imageList],
     Beep[]
     }
   }]]

gratuitous mandrill picture

The Beep confirms the EventHandler action, but the image doesn't change.

$\endgroup$
4
  • 1
    $\begingroup$ RotateLeft doesn't change the list inplace rather it creates a new one, so your imageList is never updated $\endgroup$
    – ssch
    Commented Dec 10, 2012 at 11:21
  • $\begingroup$ Good point - but imageList = RotateLeft[imageList], doesn't seem to work... $\endgroup$
    – cormullion
    Commented Dec 10, 2012 at 11:24
  • 3
    $\begingroup$ FlipView[{image1, image2}]? $\endgroup$
    – Rojo
    Commented Dec 10, 2012 at 12:05
  • $\begingroup$ @rojo :) why didn't I search for 'flip'!? Looks like a much easier way than mine. Still, learning about Dynamic is always a good thing... :) $\endgroup$
    – cormullion
    Commented Dec 10, 2012 at 13:22

3 Answers 3

8
$\begingroup$

You need to assign imageList for it to actually update, also you need to make the graphics dynamic for it to update when the list is changed. Code changes are:

... imageList = RotateLeft[imageList] ...
... EventHandler[Dynamic@Show[First[  ...

So it's :

image1 = Thumbnail[ExampleData[{"TestImage", "Mandrill"}], 300];
image2 = Thumbnail[
 ColorNegate[ExampleData[{"TestImage", "Mandrill"}]], 300];
DynamicModule[{imageList = {image1, image2}}, 
 EventHandler[
 Dynamic@Show[First[imageList], 
  Graphics[{Text[Style["name of First[imageList]]?", 14, White], 
   ImageDimensions[First[imageList]]/
   2]}]], {"MouseDown" :> {imageList = RotateLeft[imageList], 
 Beep[]}}]]
$\endgroup$
4
  • $\begingroup$ @NasserM.Abbasi, @jVincent, Don't forget to wrap Show in Dynamic. $\endgroup$
    – ssch
    Commented Dec 10, 2012 at 11:34
  • $\begingroup$ @jVincent Thanks - works well. Any idea how to get the name of the currently displayed image (eg "image1") into the Text expression? $\endgroup$
    – cormullion
    Commented Dec 10, 2012 at 13:24
  • $\begingroup$ @cormullion You could simply define names={"name1","name2"}; and also rotate it when you rotate the other list; names = RotateLeft[names]. Then ofcause switch out the string you use now with First@names. $\endgroup$
    – jVincent
    Commented Dec 10, 2012 at 13:32
  • $\begingroup$ OK. I'd been fiddling with something else - yours is easier. $\endgroup$
    – cormullion
    Commented Dec 10, 2012 at 13:32
16
$\begingroup$

You can use FlipView

FlipView[{image1, image2}]
$\endgroup$
2
  • 2
    $\begingroup$ That's cool Rojo, I never noticed FlipView before. Thanks for the tip. $\endgroup$
    – bill s
    Commented Dec 10, 2012 at 19:34
  • 1
    $\begingroup$ Yes, good find, @rojo! $\endgroup$
    – cormullion
    Commented Dec 10, 2012 at 23:29
4
$\begingroup$

Here's a somewhat simpler approach using the Manipulate[] function:

 image1 = Thumbnail[ExampleData[{"TestImage", "Mandrill"}], 300];
 image2 = Thumbnail[ColorNegate[ExampleData[{"TestImage", "Mandrill"}]], 300];
 Manipulate[If[t == 1, image1, image2], {t, {0, 1}}]

Each time you click on the checkbox, it alternates between the two images.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.