How could I present an image with fade in and fade out?

235 views
Skip to first unread message

nyessog

unread,
Mar 19, 2012, 3:07:31 AM3/19/12
to psychopy-users
I'd like to present an image with the image appearing progressively
and disappearing progressively too. Could someone tell me how I could
do that?

Thanks

Michael MacAskill

unread,
Mar 19, 2012, 5:14:31 AM3/19/12
to psychop...@googlegroups.com

Are you using the Builder? If so, the Patch stimulus has an opacity field. Set the button next to it to be "set every frame" so that its value can be changed progressively, and enter an equation in the box that does what you want.

e.g. if your screen refresh rate is 60 Hz, then entering:
frameN/120

would cycle the opacity linearly from 0 to 1.0 over 2s (it will then continue incrementing but it doesn't seem to matter if the value exceeds 1.0).
Using a code component might allow you to do more sophisticated things (e.g. fade in for a while, hold it, then fade out). Or more simply, you just create multiple successive Patch stimulus components, each with a different equation or value in the opacity field depending on their place in the timeline.

Regards,

Michael

nyessog

unread,
Mar 19, 2012, 7:36:22 AM3/19/12
to psychop...@googlegroups.com
Thank for your response Michael.

I have found the usage of the opacity field.

As soon as I understand how to draw a simple square in PsychoPy I'll update my script but for now here is my script (the script need a picture file).

-----fade-in-out.py-----
# -*- coding: utf-8 -*-
# fade-in-out.py
from psychopy import visual, core

#setup stimulus
win=visual.Window(size=[400, 400], color='white', units='pix')
stim = visual.PatchStim(win, tex='images/stim-200x200/hot1.jpg', mask='none',
    opacity=0.0, name='hot1', autoLog=False)

# Number of steps of the fade phase
fade_steps = 15

# Duration of the fade phase (number of frames)
fade_duration = 60

if fade_steps > fade_duration / 2:
    raise NameError('TooManySteps')

# fade variation at each step (fading from 10% to 100 % and reverse)
delta_fade = 0.9 / fade_steps

# Duration of a step (number of frames)
fade_step_duration = int(fade_duration / fade_steps)

# Duration of the central phase (number of frames)
central_duration = 180

total_presentation_duration = central_duration + 2 * fade_duration

clock = core.Clock()

fade_value = delta_fade
step_stop = fade_step_duration
for frameN in range(total_presentation_duration):
    if frameN < fade_duration:
        if frameN >= step_stop:
            step_stop += fade_step_duration
            fade_value += delta_fade
    elif frameN >= fade_duration + central_duration:
        if frameN >= step_stop:
            step_stop += fade_step_duration
            fade_value -= delta_fade
    else:
        if frameN >= step_stop:
            step_stop = fade_duration + central_duration
            fade_value = 1.0
    stim.opacity = fade_value
    stim.draw()
    win.flip()#flip the screen every frame

nyessog

unread,
Mar 19, 2012, 8:13:14 AM3/19/12
to psychop...@googlegroups.com
Le lundi 19 mars 2012 12:36:22 UTC+1, nyessog a écrit :
As soon as I understand how to draw a simple square in PsychoPy I'll update my script but for now here is my script (the script need a picture file).

Same script without picture.

-----fade-in-out.py-----
# -*- coding: utf-8 -*-
# fade-in-out.py
#
# This script shows how to present a stimulus with fade in and fade out transition before and after the stimulus
#

from psychopy import visual, core
import numpy as np  # whole numpy lib is available, pre-pend 'np.'

#setup stimulus
win=visual.Window(size=[400, 400], color='white', units='pix')
stim = visual.PatchStim(win, tex=np.array([[-1],[-1]]), mask='none', size=200, opacity=0.0, name='stim', autoLog=False)
Message has been deleted

Michael MacAskill

unread,
Mar 19, 2012, 4:08:17 PM3/19/12
to psychop...@googlegroups.com

On 20 Mar, 2012, at 00:36, nyessog wrote:

> As soon as I understand how to draw a simple square in PsychoPy I'll update my script but for now here is my script (the script need a picture file).

Ahh, so you're using Coder and clearly know your way around Python. Could have answered your question in one line then… :–)

Cheers,

Michael

Jeremy Gray

unread,
Mar 19, 2012, 4:23:12 PM3/19/12
to psychop...@googlegroups.com
I'll add this thread to the "recipes" section of the web documentation
(mostly copy & paste, some editing). It would make a nice addition to
an "Animation" recipe page (along with "scrolling text" from another
thread).

here's the text (will get formatted to look pretty):

https://github.com/jeremygray/psychopy/commit/ceb38a9fd4b514759dba6617a2f5716dcda2f9f2

> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/psychopy-users?hl=en.
>

Michael MacAskill

unread,
Mar 19, 2012, 5:08:57 PM3/19/12
to psychop...@googlegroups.com
Hi Jeremy,

Great idea, as this does come up a bit…

I'm not too familiar with the Builder, so I tend to just parrot some variation on "use frameN". But that is quite limited, as it can assume that the stimulus appears at frame 0, and is it tied to the refresh rate.

I wonder if we could collect some tricks for simple one-line animation expressions (i.e. which don't involve a code component). e.g. tracking the mouse position, or a position/colour/opacity value based on a sinusoidal function of elapsed time. I'm not sure what variable names Builder exposes that would be of use?

e.g. I see that there is globalClock.getTime(), but is there also a trial time that resets to zero each repetition, for example? A shopping list of those variables in the documentation would help people along the way. Even for frameN: does it reset each trial, or is it a global frame counter for the entire experiment?

I'll add what I can find to the documentation, but any existing suggestions very welcome.

Mike



On 20 Mar, 2012, at 09:23, Jeremy Gray wrote:

> I'll add this thread to the "recipes" section of the web documentation
> (mostly copy & paste, some editing). It would make a nice addition to
> an "Animation" recipe page (along with "scrolling text" from another
> thread).
>
> here's the text (will get formatted to look pretty):
>
> https://github.com/jeremygray/psychopy/commit/ceb38a9fd4b514759dba6617a2f5716dcda2f9f2

--
Michael R. MacAskill, PhD michael....@nzbri.org
Research Director,
New Zealand Brain Research Institute

66 Stewart St http://www.nzbri.org/macaskill
Christchurch 8011 Ph: +64 3 3786 072
NEW ZEALAND Fax: +64 3 3786 080


nyessog

unread,
Mar 20, 2012, 1:54:08 AM3/20/12
to psychop...@googlegroups.com


Le lundi 19 mars 2012 21:08:17 UTC+1, Michael MacAskill a écrit :

Ahh, so you're using Coder and clearly know your way around Python.  Could have answered your question in one line then… :–)

I'm new to PsychoPy and Python. These are my first lines of Python but I used other languages before. 

Gary Lupyan

unread,
Mar 20, 2012, 1:56:17 AM3/20/12
to psychop...@googlegroups.com
You may want to check out my psychopy tutorial / exercises:
http://sapir.psych.wisc.edu/wiki/index.php/Psych711

-----------------------------------------------------
Gary Lupyan - lup...@wisc.edu
Assistant Professor of Psychology
University of Wisconsin, Madison
http://sapir.psych.wisc.edu
-----------------------------------------------------

> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/psychopy-users/-/aE4vtmSdZpMJ.

Jonathan Peirce

unread,
Mar 20, 2012, 6:13:37 AM3/20/12
to psychop...@googlegroups.com

On 19/03/2012 21:08, Michael MacAskill wrote:
>
> e.g. I see that there is globalClock.getTime(), but is there also a trial time that resets to zero each repetition, for example? A shopping list of those variables in the documentation would help people along the way. Even for frameN: does it reset each trial, or is it a global frame counter for the entire experiment?
>
> I'll add what I can find to the documentation, but any existing suggestions very welcome.

some of these did get added by Becky to
http://www.psychopy.org/builder/components/code.html
The bottom of that page includes a list of some of the common variables
that people might want to use.

But we could do with more variables described there and more recipes,
maybe as a separate page (or pages) to which the Code Component points.

Jon

--
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk


This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.

This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

nyessog

unread,
Mar 20, 2012, 8:06:21 AM3/20/12
to psychop...@googlegroups.com
Le mardi 20 mars 2012 06:56:17 UTC+1, Gary a écrit :
You may want to check out my psychopy tutorial / exercises:
http://sapir.psych.wisc.edu/wiki/index.php/Psych711

Thanks for the link. Your tutorial make references to files that are not accessible. Could you put an archive with the needed files (perhaps in the installation part)?  

Michael MacAskill

unread,
Mar 20, 2012, 3:58:13 PM3/20/12
to psychop...@googlegroups.com

On 20 Mar, 2012, at 23:13, Jonathan Peirce wrote:

> some of these did get added by Becky to
> http://www.psychopy.org/builder/components/code.html
> The bottom of that page includes a list of some of the common variables that people might want to use.

Ah, that's exactly what I meant, and in retrospect the code component was the obvious place to look... But people will often want to know about these variables to make full use of the standard components, even when they don't have the need for a code component per se (and so also won't be looking there in the help documents). So perhaps the documentation for each relevant component could have an explicit link to that section, saying something like "The attributes of this stimulus can be modified in real time as a function of a number of variables, as explained here…" It might forestall a lot of common questions.

Cheers,

Regards,

Michael


Reply all
Reply to author
Forward
0 new messages