psychopy.org | Reference | Downloads | Github

Reading in image size or aspect ratio in PsychoJS

I have some images that have different sizes & aspect ratios. In my experiment, I want to present these images in their native aspect ratio. I also want to randomize their positions within a certain window on the screen, so for that I also need to know their sizes (since the range from which the center positions must be drawn is different for each image and depends on its size).

This was all rather straightforward to code up in Python as I could either use a package like PIL to read in the image sizes, or I could create an ImageStim in PsychoPy without specifying a size, and that would load the image in its original size. I could then query the .size property of the stimulus object to get the information I needed. I implemented the latter method, thinking that it would be compatible with PsychoJS because it didn’t require any external packages like PIL. However, I have since learned that PsychoJS apparently doesn’t allow you to create an ImageStim without specifying its size. Or at least, if you do so, the .size property will be left undefined.

Is there any way around this that will achieve the two goals I laid out at the start? Can PsychoJS load an image in its native size/aspect ratio? And if so, does it store the size information anywhere? Or is there some other way to read in image size in JS (or ideally, a method that works in Python and can be auto-translated to JS in PsychoPy)?

(I have considered hard-coding the image sizes into the script, but that would be a last resort as I may want to change the images in the future or add many more, so a flexible solution would be far preferable.)

What I’ve sometimes done is code the aspect ratio in the spreadsheet.

Thanks for taking the time to respond (and for your excellent crib sheet!). That’s an interesting suggestion. I’ve not been using spreadsheets so far but rather generating random sequences for my trials online. But looking into it now, I guess I could just have a code component that co-opts psychopy.data.importConditions to load arbitrary spreadsheet data (assuming this gets auto-translated well). If on top of that I write a helper function to create this spreadsheet from the images (locally, before syncing to Pavlovia), that might be close to what I’d ideally want (which would be full automation).

I guess from your reply that you aren’t aware of a way to read in image sizes in PsychoJS (which would be even better)?

Correct.

It is certainly possible to import conditions from a spreadsheet in code online – I think I have an example on my code snippets page, though I usually use a loop with a code component to append (see my independent randomisation demo). I’m sure that you would find people interested in a utility that can search a directory and create a PsychoPy ready spreadsheet with image names and sizes.

1 Like