psychopy.org | Reference | Downloads | Github

ImageStim with a numpy array having unexpected results

Hi,

I’m hoping that I’m missing something very obvious. We’ve been happily making checkerboard stimuli using numpy arrays for several months. Recently, though, my students noticed a very strange behavior when we started messing with the colors of the squares. Below is a minimum example. Expected behavior is a dark red / blue checkerboard on a dark red background. Instead, we get a black/blue checkerboard on a dark red background. Changing the color to pure red (1.0) value causes it to work fine. Changing the opacity attribute of the stimulus doesn’t have any effect.

Thanks in advance

from psychopy import visual, core, event, monitors
import numpy as np

BLUE =[-1.0, -1.0, 1.0]
LIGHT_RED =[-0.25,-1.0,-1.0]

win = visual.Window([1000,800], color=LIGHT_RED, colorSpace='rgb', units='pix')

img = np.array([[BLUE, LIGHT_RED], [LIGHT_RED, BLUE]]) # Checkerboard bitmap 

stimulus = visual.ImageStim(win=win, image=img, colorSpace='rgb', size=(400, 400), pos=(0, 0)) 

while True:
    stimulus.draw()
    win.flip()          #update the screen

    #handle key presses each frame
    for keys in event.getKeys():
        if keys in ['escape','q']:
            core.quit()
~                            

Just as an update - it really seems like the red channel is being blended. If I change light red to be, [0.75, -1.0, -1.0], for example, then I get dark red squares on a light red background. But what I want are background-colored squares. (And since they have the same RGB value, I don’t know why the squares are a different color.)

One last update that suggests this may actually be a bug. If I use stimulus = visual.GratingStim(win=win, tex=img, colorSpace='rgb', size=(400, 400), pos=(0, 0)) instead of ImageStim, it works as expected.

Hi - did you resolve this problem? I’m finding the same problem for colour values from np.arrays using ImageStim. I’ve copied details of array values and screen shots of the resulting images using ImageStim and GratingSitm below. Only the latter produces on screen colour values corresponding to those in the array. ImageStim works fine with image files (.png) generated from the same np.arrays using PIL.

I wonder if this has something to do with the ‘blendMode’ property? This can take two values: ‘avg’, which means that when colours with an opacity of 1 are overlaid they do not mix; and ‘add’, which means that colours will be blended (https://www.psychopy.org/general/rendering.html#blendmode). For the GratingStim object, ‘blendMode’ is listed as an attribute with a default value of ‘avg’, so colours will not be blended. Whereas for the ImageStim and ElementArrayStim objects, ‘blendMode’ is not listed as an attribute and it is unclear what will happen when colours are overlaid. My understanding of this is limited, but since the ‘tex’ and ‘color’ properties of visual objects both have RGB values, these objects are effectively overlaying two colours.

The documentation (https://www.psychopy.org/general/rendering.html#blendmode) seems to indicate that ‘blendMode’ generally has a default value of ‘avg’, but the behaviour of ImageStim and ElementArrayStim objects does not seem consistent with this.

See also: RGB colour space limits differ in visual.ImageStim

See also: ElementArrayStim - updating and passing lists of parameters / stimuli?

I can verify that the blendMode setting did not fix the problem.

How did you test this?