| Reference | Downloads | Github

Using a code component to set size/dimensions of image component

Mac OS, latest PsychoPY
What are you trying to achieve?:
I am displaying one image/trial for a memory task; many of the images are different dimensions. My conditions file has two columns, xdim and ydim, that specify the width and height in pixels, respectively. I need to set the image component each repeat to use these dimensions.

What did you try to make it work?:
I tried creating a code component at the beginning of the “image” routine:

img_size = [xdim, ydim]

and then I set the image component size to $img_size

What specifically went wrong when you tried that?:
Unfortunately, this technique places the code from that component after img_stim.setSize(img_size)
In other words, the script is trying to set the size to img_size before img_size has been defined. Now, it’s easy (and it works) to simply compile the script and move the xdim, ydim line to above the setSize line. However, I like to have the flexibility to work mostly in builder, so it’s easier to make changes and not have to go back and forth between builder and coder. So my question is: is there a way to ensure that this line of code (which has to be repeated for each iteration of the loop) to be placed before setSize?


Hi, I’m not a PsychoPy expert, but I think I have some ideas of how you could solve this.
This first thing I would try is actually moving the code component above the text component in the PsychoPy builder window. I think that ordering of where the routines actually are in the window plays some role (plus its an easy thing to try).
If that doesn’t work, you can always create a separate routine, I like to call them “initialization” routines, where the only thing that happens in this routine is your custom code that defines this img_size variable, and other variables that are needed in the coming routine. If you just place this initialization routine in the same loop as the image presentation routine then it will continue to update each iteration.
Hope that helps!


Good thinking! That first solution worked. I’ve run into this dilemma several times, so glad there is a fix. Thanks a lot!