Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
RotateLeft doesn't change the list inplace rather it creates a new one, so your imageList is never updated –  ssch Dec 10 '12 at 11:21
    
Good point - but imageList = RotateLeft[imageList], doesn't seem to work... –  cormullion Dec 10 '12 at 11:24
3  
FlipView[{image1, image2}]? –  Rojo Dec 10 '12 at 12:05
    
@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... :) –  cormullion Dec 10 '12 at 13:22
add comment

3 Answers 3

up vote 7 down vote accepted

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[]}}]]
share|improve this answer
    
@NasserM.Abbasi, @jVincent, Don't forget to wrap Show in Dynamic. –  ssch Dec 10 '12 at 11:34
    
@jVincent Thanks - works well. Any idea how to get the name of the currently displayed image (eg "image1") into the Text expression? –  cormullion Dec 10 '12 at 13:24
    
@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. –  jVincent Dec 10 '12 at 13:32
    
OK. I'd been fiddling with something else - yours is easier. –  cormullion Dec 10 '12 at 13:32
add comment

You can use FlipView

FlipView[{image1, image2}]
share|improve this answer
2  
That's cool Rojo, I never noticed FlipView before. Thanks for the tip. –  bill s Dec 10 '12 at 19:34
1  
Yes, good find, @rojo! –  cormullion Dec 10 '12 at 23:29
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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