psychopy.org | Reference | Downloads | Github

Images lose quality when resizing

images

#1

Hello everyone,

I’m coding my experiment using PsychoPy v1.85.6 in a MacOS High Sierra v10.13.6 .

I’m using ImageStim to draw some images (.png) using degrees of visual angle. The problem is that when I try to rescale the images they lose quality (see snapshots attached).

Here is an example code:

# define parameters
refresh_rate = 60
mon_size = [1920, 1200]
mon_width = 53.1
mon_distance = 60

# define monitor
my_monitor = monitors.Monitor('testMonitor',
                              width=mon_width,
                              distance=mon_distance)

# create window
win = visual.Window(monitor=my_monitor,
                    units='deg',
                    size=mon_size,
                    fullscr=True,
                    allowGUI=False)

# create stimuli
panel = visual.ImageStim(win,
                         image='/somepath/Slide-01.jpg',
                         units='deg')

# resize stimuli
panel.size /= 1.5

# draw stimuli
for i in range(144):
    panel.draw()
    win.flip()

When leaving size of images untouched, or when resizing images manually outside of PsychoPy, they don’t lose resolution.

Perhaps there is something wrong in my code that you can help me with?

I’ve tried converting the images to different formats (.png .jpg .tiff .bmp), but results remained the same. I’ve also tried using .eps but got an error saying that the file didn’t exist (while I’m sure the file was in the path indicated).

I’ve also tried rescaling the images using:

panel = visual.ImageStim(win,
                         image='/somepath/Slide-01.jpg',
                         units='deg',
                         size=[25,30])

…but images still lose quality.

This is an example of the original image (dimensions = 886 × 903):

This is an example of the image presented without resizing:

And this is an example of the pixelated image when resizing:

Thank you!


#2

Re-sizing a bitmap isn’t a trivial issue: there are various algorithms that can be applied, leading to different results. This is often most obvious when using line art like your images, where there are single-pixel thickness lines (i.e. the issues are less apparent with naturalistic photographic images). It is probably best that you resize the images in a way that you are happy with, and then use PsychoPy to present them at their native (re-sized) dimensions, i.e. with one pixel in the image corresponding to one pixel on the screen.


#3

Hello,

Try adding the interpolate=True argument when initializing your ImageStim object. This will enable linear texture filtering which may improve image quality when resizing. If not, you will need to resample the image as Michael suggested.


#4

You learn something new everyday: thanks for that, Matt.


#5

Thank you! As long as I’m not rescaling too much, this works perfectly.

Thanks for the explanation. Next time I’ll keep this in mind.