Image Stim

362 views
Skip to first unread message

Colby Tibbets

unread,
Mar 1, 2015, 9:20:46 PM3/1/15
to psychop...@googlegroups.com
Does anyone know if it is possible to make a glowing boundary/edge appear around an imagestim after a mouse click event? What function can I use?

Jeremy Gray

unread,
Mar 1, 2015, 9:43:21 PM3/1/15
to psychop...@googlegroups.com
Hi Colby,

There's not built-in function but a simple highlight is easy with a little code.

If your image is defined like this:
    img = visual.ImageStim(win, 'image.png')

you can define a boundary like this (at the start of a routine, prior to a drawing loop):
    border = visual.ShapeStim(win, vertices=img.verticesPix, units='pix', lineColor='red')

Then inside a draw-loop (Every frame) you can do

if mouse.isPressedIn(img):
    border.autoDraw = True

To get a "glowing" highlight would be more effort, because you need to update the border's appearance on every frame. To be able to "unselect" the image is also more complex for a couple reasons.


--Jeremy

On Sun, Mar 1, 2015 at 9:20 PM, Colby Tibbets <colby....@gmail.com> wrote:
Does anyone know if it is possible to make a glowing boundary/edge appear around an imagestim after a mouse click event? What function can I use?

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/036dfb4e-25ff-4017-b4ca-6d5b64694b40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Gray

unread,
Mar 1, 2015, 10:18:06 PM3/1/15
to psychop...@googlegroups.com
A glowing effect is also easy, just vary the opacity as a function of time. Every frame do:

border.opacity = 0.6 + sin(core.getTime()*6) / 2.


--Jeremy

Colby Tibbets

unread,
Mar 2, 2015, 4:45:32 AM3/2/15
to psychop...@googlegroups.com
Jeremy, 
Thanks for the reply, a highlighted edge will certainly be fine. However, I need to have multiple image stimuli be selectable and deselectable. Why would this be particularly difficult?

Jeremy Gray

unread,
Mar 2, 2015, 6:38:52 AM3/2/15
to psychop...@googlegroups.com

Thanks for the reply, a highlighted edge will certainly be fine. However, I need to have multiple image stimuli be selectable and deselectable. Why would this be particularly difficult?

For multiple images, you just need to do the same thing for all of them, which is not so hard. Might be easiest to have an array of them, and have a loop to check them. At the end of the trial you might want to have another loop for each one to save its state (selected or not).

For deselecting, the mouse.isPressedIn() part will not be enough because it will do that every frame. The mouse being pressed down is not the same as a click. A single click results in the mouse being down for multiple frames. So you need to account for that and only accept the a click if the mouse is "currently pressed and has not recently been pressed". So you need to keep track of that information across visual frames.

Hezi Ben Sasson

unread,
Apr 8, 2015, 10:32:22 PM4/8/15
to psychop...@googlegroups.com
Hi Jeremy, 

I have a similar usage for glowing/highlighting a stim-image. I have two side-by-side pictures, and have the participants pick either by pressing a designated key on the keyboard. This is going in a loop a couple of times. Any idea on how to code such a thing?

Jeremy Gray

unread,
Apr 8, 2015, 10:43:20 PM4/8/15
to psychop...@googlegroups.com
I have a similar usage for glowing/highlighting a stim-image. I have two side-by-side pictures, and have the participants pick either by pressing a designated key on the keyboard. This is going in a loop a couple of times. Any idea on how to code such a thing?

Sure -- the other replies in this thread sketch out how to do the glowing part, and how to do it with multiple images. You'll need to become acquainted with python and using code components, and the API descriptions of a few things will be useful. To time the click for deselecting, you can use a Clock(); read up on PsychoPy's clocks online http://www.psychopy.org/api/core.html#psychopy.core.Clock

It will likely be easier to figure out how Clock()'s and other things work separately, and then use them in code in your experiment.

--Jeremy

 
On Sunday, March 1, 2015 at 9:43:21 PM UTC-5, Jeremy wrote:
Hi Colby,

There's not built-in function but a simple highlight is easy with a little code.

If your image is defined like this:
    img = visual.ImageStim(win, 'image.png')

you can define a boundary like this (at the start of a routine, prior to a drawing loop):
    border = visual.ShapeStim(win, vertices=img.verticesPix, units='pix', lineColor='red')

Then inside a draw-loop (Every frame) you can do

if mouse.isPressedIn(img):
    border.autoDraw = True

To get a "glowing" highlight would be more effort, because you need to update the border's appearance on every frame. To be able to "unselect" the image is also more complex for a couple reasons.


--Jeremy

On Sun, Mar 1, 2015 at 9:20 PM, Colby Tibbets <colby....@gmail.com> wrote:
Does anyone know if it is possible to make a glowing boundary/edge appear around an imagestim after a mouse click event? What function can I use?

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/036dfb4e-25ff-4017-b4ca-6d5b64694b40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

Hezi Ben Sasson

unread,
Apr 9, 2015, 12:08:00 AM4/9/15
to psychop...@googlegroups.com
Just to be more specific -- I am looking for a way for the participants to get receive feedback for their choice. The idea is once the participants pressed a designated button that represents one of the presented pictures, that picture will glow/be highlighted for 1-2s before moving in for the next trial. The participants don't get to select and confirm their selection, but rather what they see is a brief response feedback for their selection. Does that make any difference in regards your instructions? Also, I'm still a beginner in python, and so would love if you can give me more details about the procedure I need to undertake.  Thanks.

Jeremy Gray

unread,
Apr 9, 2015, 12:14:56 AM4/9/15
to psychop...@googlegroups.com
In that case I would just have the selected image persist for 1-2s, and make the non-selected one disappear. This will be as clear in terms of feedback, and easier to code.

This will still require some coding, its not a built-in feature. To give any sort of feedback you need to do something contingently upon how the subject responds, and doing that requires a) getting a response such as a keypress, and b) doing something with it. The conditional branching Builder demo illustrates some of this, and I think there are several other demos that do so too.

--Jeremy

Hezi Ben Sasson

unread,
Apr 12, 2015, 6:07:38 PM4/12/15
to psychop...@googlegroups.com
Hi Jeremy,

I think that I like better the highlight solution, but I can't get it to work.

Just for the context - I have a csv file with two columns filled with different pictures. In this trial, pairs of pictures from both columns are presented, and the participant is asked to pick either by pressing left/right key.

In the Begin Routine part of the code I inserted this:
border = visual.ShapeStim(win, vertices=iimage.verticesPix, units='pix', lineColor='red')      ### iimage is the name of the image stimuli.

Then, in the Each Frame part I inserted this:
if key_resp_2=="left":   ### key_resp_2 is the key
    border.autoDraw=True

I've also tried the code that you wrote for glowing effect instead of what below, but that didn't work either. 

What am I doing wrong?

Hezi Ben Sasson

unread,
Apr 12, 2015, 6:38:07 PM4/12/15
to psychop...@googlegroups.com
OK sorry, the highlighting works well (had a minor typo - was supposed to be: key_resp_2.keys=='left') 

- Is there a way to make the highlighting line a bit thicker? I still can't get the glowing effect to work...
- The highlighting persists throughout the entire sets of pairs - that is, though the picture itself changes, its frame continues to be highlighted. How can I make the highlight persist for a second and then go back to normal (until the next time a certain key is being pressed)?

Thanks a lot.

Jeremy Gray

unread,
Apr 12, 2015, 6:52:25 PM4/12/15
to psychop...@googlegroups.com
Hi Hezi,

I think that I like better the highlight solution, but I can't get it to work.

Just for the context - I have a csv file with two columns filled with different pictures. In this trial, pairs of pictures from both columns are presented, and the participant is asked to pick either by pressing left/right key.

In the Begin Routine part of the code I inserted this:
border = visual.ShapeStim(win, vertices=iimage.verticesPix, units='pix', lineColor='red')      ### iimage is the name of the image stimuli.

You'll need a border for each image, or you'll need to later set the vertices of a single border to be those verts of the particular image
 
Then, in the Each Frame part I inserted this:
if key_resp_2=="left":   ### key_resp_2 is the key
    border.autoDraw=True 

for the setting vertices approach, here is where you would do

    border.setVertices(iimage.verticesPix)

and then do a similar thing for the right picture
 
I've also tried the code that you wrote for glowing effect instead of what below, but that didn't work either. 

What am I doing wrong?

What is the error? How is it "not working"? (That's much too general a description for someone to be able to help.)

--Jeremy


 

Hezi Ben Sasson

unread,
Apr 12, 2015, 7:11:31 PM4/12/15
to psychop...@googlegroups.com
Hi Jeremy,

Thanks again for the help.

I hope you saw my second message as well. I was eventually able to use the highlighting line, by setting a boarder for each image.

In regards to the glowing - I didn't get any error message back, and didn't see any other effect (beside the highlighting line) in play.

Jeremy Gray

unread,
Apr 12, 2015, 7:13:07 PM4/12/15
to psychop...@googlegroups.com
Yeah, just saw this.

OK sorry, the highlighting works well (had a minor typo - was supposed to be: key_resp_2.keys=='left') 

- Is there a way to make the highlighting line a bit thicker?

check out the ShapeStim API online (google for it, "psychopy shapestim"), look for a lineWidth parameter
 
I still can't get the glowing effect to work...

every frame set the borders opacity. arrange for the value to be a function like sin() that will give you a nice rise and fall in intensity over time
 
- The highlighting persists throughout the entire sets of pairs - that is, though the picture itself changes, its frame continues to be highlighted. How can I make the highlight persist for a second and then go back to normal (until the next time a certain key is being pressed)?

you'll need a Clock() instance (check out the API for clocks, look for count down timers). check every frame whether the timer still has time left on it. if there's no time left, set autodraw = False

--Jeremy
 

HBS

unread,
Apr 16, 2015, 2:17:35 PM4/16/15
to psychop...@googlegroups.com
Hi Jeremy,

Success with the line width! 

However, I've tried to play with the clock but I can't seem to get it right.

if key_resp_2.keys=='left':
    border.autoDraw=True
    timer=core.CountdownTimer(2)
    while timer.getTime()<0:
        border.autoDraw=False
if key_resp_2.keys=='right':
    border_1.autoDraw=True
    timer=core.CountdownTimer(2)
    while timer.getTime()<0:
        border.autoDraw=False

I've also tried to play with time.sleep -- 

import time
if key_resp_2.keys=='left':
    border.autoDraw=True
    time.sleep(2)
    border.autoDraw=False

But in both cases - the line continues to appear, or isn't being activated in the first place.



----
Hezi Ben Sasson

--
You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/Rv3VnQulnrQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Jeremy Gray

unread,
Apr 16, 2015, 2:25:46 PM4/16/15
to psychop...@googlegroups.com
Ok, cool. You are close!

Try this Every frame:

if key_resp_2.keys=='left':
    border.autoDraw=True
    timer=core.CountdownTimer(2)
if key_resp_2.keys=='right':
    border_1.autoDraw=True
    timer=core.CountdownTimer(2)
if border.autoDraw and timer.getTime()<0:
    border.autoDraw=False
if border_1.autoDraw and timer.getTime()<0:
    border_1.autoDraw=False

--Jeremy

HBS

unread,
Apr 16, 2015, 2:40:58 PM4/16/15
to psychop...@googlegroups.com
Thanks for that, but I still have the same issue ---

 Inline image 1

The lines remain while the pictures change.

Inline image 2



----
Hezi Ben Sasson

Jeremy Gray

unread,
Apr 16, 2015, 4:35:05 PM4/16/15
to psychop...@googlegroups.com
You mean the border carries over to the next trial as well? If so, in End Routine, turn off autodraw:

border.autoDraw = False
border_1.autoDraw = False

--Jeremy

HBS

unread,
Apr 16, 2015, 6:17:17 PM4/16/15
to psychop...@googlegroups.com
Thanks, but that brings me back to the same problem - now the line doesn't appear at all. It is as if the newest addition automatically and immediately cancels out the drawing of the line. 

----
Hezi Ben Sasson

Jeremy Gray

unread,
Apr 16, 2015, 6:35:44 PM4/16/15
to psychop...@googlegroups.com

Thanks, but that brings me back to the same problem - now the line doesn't appear at all. It is as if the newest addition automatically and immediately cancels out the drawing of the line. 

Well, I am guessing that your design specifies that a keypress should end the trial. But you want a keypress to trigger the border, and not end the trial until some time later (so that you can see the border). So set the key component not to force-end the trial, and add continueRoutine = False here:

if border.autoDraw and timer.getTime()<0:
    border.autoDraw=False
    continueRoutine = False
if border_1.autoDraw and timer.getTime()<0:
    border_1.autoDraw=False
    continueRoutine = False

 

HBS

unread,
Apr 16, 2015, 7:36:56 PM4/16/15
to psychop...@googlegroups.com
OK - that solved the border problem! Now the border stays with the picture, disappears in the right time, and comes back to the new picture given a key pressed. Beautiful!

However - even after I press a key (left or right), I still have to press 'space' in order to finish/end the routine. Otherwise, I'm stuck at the same step. Any advise on that?

Thanks again for the help!

----
Hezi Ben Sasson

Jeremy Gray

unread,
Apr 16, 2015, 7:44:23 PM4/16/15
to psychop...@googlegroups.com

OK - that solved the border problem! Now the border stays with the picture, disappears in the right time, and comes back to the new picture given a key pressed. Beautiful!

Great
 
However - even after I press a key (left or right), I still have to press 'space' in order to finish/end the routine. Otherwise, I'm stuck at the same step. Any advise on that?

Sorry no, not really. Just try to take out waiting for a 'space' key somehow?

--Jeremy
 

Jeremy Gray

unread,
Apr 16, 2015, 7:44:23 PM4/16/15
to psychop...@googlegroups.com

OK - that solved the border problem! Now the border stays with the picture, disappears in the right time, and comes back to the new picture given a key pressed. Beautiful!

Great
 
However - even after I press a key (left or right), I still have to press 'space' in order to finish/end the routine. Otherwise, I'm stuck at the same step. Any advise on that?

Sorry no, not really. Just try to take out waiting for a 'space' key somehow?

--Jeremy
 

Kuppu Raj

unread,
May 14, 2018, 7:12:13 AM5/14/18
to psychopy-users
Dear Jeremy 

My routine looks like this. I want to draw a border around the image,  only while the subsequent sound is played. And, shall be drawn to the duration/length  of the sound.

How do I achieve this?

This was a useful post, and I managed to figure out how to draw a border with this comment around my image component 'first'

first = visual.ShapeStim(win, vertices=first.verticesPix, units='pix', lineColor='yellow',lineWidth=7)

I couldn't progress further with it though

Thanks for your help 
show_1.PNG
Reply all
Reply to author
Forward
0 new messages