# Changing pictures in a single routine depending on scale value

I am trying to build an experiment in Psychopy that is called decentering/centering task. Participants see a face and have the task to move the eyes with the help of a scale/slider (whatever works). We got pictures for many angles in 1° steps (e.g. they are called A-20.jpg, A-15.jpg, A-05.jpg, A0.jpg, A17.jpg; Number indicates the angle the eyes are moved). in the centering task, they start with a person looking to the right or left (20°). Participants are using a slider to move the person’s eyes to that point where they have the impression the face is looking at them (let’s say 10°). So with every step they are moving the slider, the picture should change to the specific picture (slider value: -8°, picture to be shown: A-08.jpg s. In the other task, it starts in the center (0°) and it is the other way around.
I watched many youtube-videos and tried to figure out a way to realize that task in Psychopy, I Tried it with this code (each Frame):
“if Angle == -20:
Pic = ‘A-20.jpg’
elif Angle == -19:
Pic = ‘A-19.jpg’
elif Angle == -18:
Pic = ‘A-18.jpg’
elif Angle == -17:
Pic = ‘A-17.jpg’
…”

Scale ist called “Angle” and the Image to be shown is defined as “Pic”
Do any of you have an idea what could be wrong with this code or how I could get this thing running? I would be so thankful for any idea you have!

I am using the builder view on a window 10 computer

You can construct the name of the image file in one line:

Pic = 'A' + str(Angle.getRating()) + '.jpg'

You could then just put \$Pic in the image field of an image component, set to update every frame. However, this would be very inefficient, as it can often take more than one frame duration to update the image file for a stimulus, and you would be trying to do this on every single screen refresh, which will cause lots of dropped frames and potentially impair the timing of your experiment.

For a task like this, you probably should pre-create 21 stimuli (0 through 20 deg), and just select the appropriate one to draw on every frame (which will be basically instantaneous), flipping the images horizontally to cater for the ones that are less than 0.

Thank you for your comment!
I tried the first idea, with your one line code which is quite sufficient. An error occurred that I saw a lot the last times I worked on it:
“NameError: name ‘Pic’ is not defined”
When I put just “Pic” in the Image field Psychopy doesn# find the picture Pic which makes sense. When I put in " \$Pic" that error occurs.
Do I have to code anything else to create “Pic”?

This error arises because you are trying to refer to the variable before it has been defined. There are usually two causes for this:

• You need to set the image field to update on every frame (otherwise it will think the content is static and try to apply it at the start of the experiment, when the variable has not yet been defined).
• The order of components is important: the code component should be above the image component, so that the variable is defined before the image component tried to access it. You can change the order of components by right-clicking on the their icons.

This arrangement might appear to work for you, but as mentioned, I suspect there will be performance issues with updating the image file attribute on every single screen refresh. It might look like it is working OK but I suspect it could really disrupt the timing of your experiment in certain ways. We should probably look at a more intelligent way of pre-loading the images and switching between them instantaneously.

Thanks for the hint of the order! They were indeed not in the correct order. I still get the same error. But now I am kind of irritated since you mentioned “text field”. I tried it one more time with a text stimulus and inserted “\$Pic” in the text field. It worked quite well and showed me the specific name of the Picture depending on the scale value. Now it just has to show the picture itself instead.

It could be that there are performance issues with this way of proceeding. However, so far that is the only idea I have to get this task running.

Thank you so much for your help so far!

That was just an error on my part.

That doesn’t make much sense. But perhaps it isn’t worth pursuing anyway, as this isn’t probably isn’t a viable solution for an image stimulus (even if it works for a text stimulus).

We should probably pre-create 20 image stimuli in code and similarly, switch between them in code, to avoid performance problems.

I tried it again, copied the same “\$Pic” from the text field to the picture field and for one reason it is now working (font changed at that moment, I guess Psychopy found that variable).
So far there is no performance problem and it works quite well!
Thank you so much for your help!!