Automatically adjust size of images from excel file

OS: macOS
PsychoPy version: 2023.1.0
Standalone installation: Yes
Run online: No


What are you trying to achieve?

I am creating a double-stimulus subjective image quality experiment in PsychoPy Builder.

In each trial, two images (A and B) are shown simultaneously. Their:

  • file paths

  • on-screen positions (in pixels)

are read from an Excel conditions file via a loop.

The images have different native resolutions, so I want to control their displayed size per trial using resolution value stored in the Excel file e.g., (w, h).

To verify the setup, I test some images with fixed size, first used constant sizes in the Image components. In that case, everything works as expected:

  • image paths load correctly from the file

  • positions from Excel are applied correctly

  • both images appear on screen


What specifically went wrong?

When I change the Image Size field from a constant value to values read from the Excel file (e.g., $reso where rest =(width, height) with “set every repeat” enabled), the images no longer appear on screen.

There are no runtime errors — the routine continues normally, but the image locations are blank.

The width and height values in Excel are the same values that work when manually entered as constants.

Positions from the same Excel file continue to work, so the loop itself is being applied correctly.


Additional details

  • Image units are set to pix.

  • The loop surrounds the routine correctly.

  • The width/height values print correctly when tested in a Code component with type tuple

  • Switching back to constant sizes immediately makes the images visible again.


Question

Is this expected behavior in PsychoPy Builder 2023.1.0, or could this be a bug or limitation when setting Image Size dynamically from an Excel conditions file?

Is there a recommended Builder-only workflow for specifying per-trial image dimensions from a conditions file?

I don’t recognise this code structure and I doubt PsychoPy does either.

If you have columns width and height in your spreadsheet then simply set the size to $[width, height]

What I usually do in this case is have the aspect ratio in the spreadsheet instead (width/height) so I can resize all images to the same height, e.g. $[aspectRatio * height, height]

1 Like

In my case this was the fix:

:one: Image component settings

For imageA:

  • Units: pix

  • Size: (100, 100) ← placeholder

  • Set every repeat: :cross_mark: leave OFF

Then I override in code as following:



:two: Code component → Begin Routine

Paste exactly:

# target display height (must fit screen)
targetHeight = 600

# read Excel values (already available)
w = float(width)
h = float(height)

# scale to fixed height
scale = targetHeight / h
dispW = w * scale
dispH = targetHeight

# apply to image
imageA.size = (dispW, dispH)

Is your code component below your image component?