Image size change with mouse movement

Hi everyone,

I’m new to coding and need to create an approach-avoidance-task on Psychopy:

Participants should drag images towards them or push them away depending on the format of the image (landscape vs. portrait). There’s an old thread about a similar question but they weren’t using different formats, so unfortunatley it didn’t help me to solve my problem.

If I’m using the following code, it kind of works:
( I’f written the paramter formatheight (with (0.4, 0.5) for landscape and (0.5, 0.4 for portrait) into the xlxs-file, where the stimuli are defined and defined the size of the image as $formatheight)

#Start Routine
TestMousePosition = 0

TestMouse.setPos((0, 0))

ImageSizeX = formatheight[0]
ImageSizeY = formatheight[1]

ImageRatio = ImageSizeX / ImageSizeY

#Each Frame
TestMousePosition = TestMouse.getPos()[1]

if ImageRatio < 1:
if TestMousePosition > 0:
ImageSizeX += .008
ImageSizeY += .01
elif TestMousePosition < 0:
ImageSizeX -= .008
ImageSizeY -= .01

elif ImageRatio > 1:
if TestMousePosition > 0:
ImageSizeY += .008
ImageSizeX += .01
elif TestMousePosition < 0:
ImageSizeY -= .008
ImageSizeX -= .01

if (ImageSizeY >= 0) and (ImageSizeX >= 0):
TestImage.size = (ImageSizeX, ImageSizeY)
else:
TestImage.size = (0, 0)

Like this, the images do change their size with mouse-movements on the y-axis, they keep their formats while changing sizes and they don’t get flipped around, when the size-paramters turn negative.

I’d like to advance the code so that the size change of the image ist proportional to the amount of mouse movement. I’ve tried the following code, but it doesn’t provide me with any size changes (i’ve tried running it through visual study code and there I get the result I want):

#Begin Routine
TestMousePosition = 0

TestMouse.setPos((0, 0))

ImageSizeX = formatheight[0]
ImageSizeY = formatheight[1]

ImageRatio = ImageSizeX / ImageSizeY

MouseAnchor = 0

#Each frame:

TestMousePosition = TestMouse.getPos()[1]
MouseDifference = int(round(abs(MouseAnchor - TestMousePosition) / 0.1))

if ImageRatio < 1:
if TestMousePosition > 0:
for n in range(MouseDifference):
ImageSizeX += .004
ImageSizeY += .005
elif TestMousePosition < 0:
for n in range(MouseDifference):
ImageSizeX -= .004
ImageSizeY -= .005

elif ImageRatio > 1:
if TestMousePosition > 0:
for n in range(MouseDifference):
ImageSizeY += .004
ImageSizeX += .005
elif TestMousePosition < 0:
for n in range(MouseDifference):
ImageSizeY -= .004
ImageSizeX -= .005

if (ImageSizeY >= 0) and (ImageSizeX >= 0):
TestImage.size = (ImageSizeX, ImageSizeY)
else:
TestImage.size = (0, 0)

MouseAnchor = TestMousePosition

I’d be really greatful for help!

Laura

Why are you creating an int of MouseDifference? I suspect that it never reaches 1 in a single frame. You could just multiply the size change by MouseDifference

Woohooo, thanks! That was a very helpful thought. This is how I solved it:

#Begin Routine:

TestMouse.setPos((0, 0))

TestMousePosition = TestMouse.getPos()[1]

ImageSizeX = formatheight[0]
ImageSizeY = formatheight[1]

ImageRatio = ImageSizeX / ImageSizeY

#Each Frame:

TestMousePosition = TestMouse.getRel()[1]

if ImageRatio < 1:
    ImageSizeY += TestMousePosition
    ImageSizeX += TestMousePosition * 0.8 

elif ImageRatio > 1:
    ImageSizeX += TestMousePosition
    ImageSizeY += TestMousePosition * 0.8

if (ImageSizeY >= 0) and (ImageSizeX >= 0):
    TestImage.size = (ImageSizeX, ImageSizeY)
else:
    TestImage.size = (0, 0)

With:

  • size = $ formatheight, set every repeat in the image-settings
  • and formatheight as a parameter in the xlxs-file for the conditions, defining the format of the stimuli in height-units (with (0.4, 0.5) for landscape and (0.5, 0.4) for portrait → if the ratio of short and long side is different, you need to adjust the factor of the size change, in my case 0.8)

Thanks a lot for your help, wakecarter, I was really stuck there.

Laura