| Reference | Downloads | Github

Reading imagestim.size in JS returns undefined

URL of experiment:

Description of the problem:

I have a series of portrait images of bodies in a sequence where participants use a slider to move through the image sequence.

The size of each .png is 576x1024. I want to be able to rescale these images for different size screens whilst maintaining their aspect ratio. Using the advice of the forum I have turned to using height units.

I want to display the images using a size that takes up around .8 of the height of the screen, but maintains the image’s aspect ratio. In Builder I can input one value in size, but of course that affects both dimensions (i.e. the image becomes stretched).

In Python I was able to create an ImageStim, assign an image to it, read the size of it in height units using ImageStim.size, and rescale it to take up .8 of the height of the screen, but automatically assign the corresponding width to maintain the ratio using:

resizedWidth = (newHeight / oldHeight) * oldWidth

However, when I used that method online I discovered that even if you assign an image to an ImageStim and then read its .size attribute, it returns ‘undefined’.

I may be approaching this all wrong, but is there a way to rescale images dependent on the screen whilst maintaining their aspect ratio?

Use height units of (1.422,.8)

Hi Wake,

Thanks for the quick reply there. Sadly those units end up really stretching it out width-wise. Am i right in thinking the x value needs to be lower than 0.8 to keep it portrait?

Can you give me your formula for this (mostly so I can learn the calculations)?

Cheers for your promptness!


Sorry I wrote the formula upside down. You want .8 * 576/1024 as the width which will be about.4

1 Like