# BufferImageStim¶

## Attributes¶

 BufferImageStim(win[, buffer, rect, …]) Take a “screen-shot”, save as an ImageStim (RBGA object). BufferImageStim.win The Window object in which the BufferImageStim.mask The alpha mask that can be used to control the outer shape of the stimulus BufferImageStim.units Units to use when drawing. BufferImageStim.pos The position of the center of the stimulus in the stimulus units BufferImageStim.ori The orientation of the stimulus (in degrees). BufferImageStim.size The size (width, height) of the stimulus in the stimulus units BufferImageStim.contrast A value that is simply multiplied by the color. BufferImageStim.color Alternative way of setting foreColor. BufferImageStim.colorSpace The name of the color space currently being used BufferImageStim.opacity Determines how visible the stimulus is relative to background. BufferImageStim.interpolate Whether to interpolate (linearly) the texture in the stimulus. BufferImageStim.name The name (str) of the object to be using during logged messages about this stim. BufferImageStim.autoLog Whether every change in this stimulus should be auto logged. Draws the BufferImage on the screen, similar to ImageStim .draw(). BufferImageStim.autoDraw Determines whether the stimulus should be automatically drawn on every frame flip.

## Details¶

class psychopy.visual.BufferImageStim(win, buffer='back', rect=(- 1, 1, 1, - 1), sqPower2=False, stim=(), interpolate=True, flipHoriz=False, flipVert=False, mask='None', pos=(0, 0), name=None, autoLog=None)[source]

Take a “screen-shot”, save as an ImageStim (RBGA object).

The screen-shot is a single collage image composed of static elements that you can treat as being a single stimulus. The screen-shot can be of the visible screen (front buffer) or hidden (back buffer).

BufferImageStim aims to provide fast rendering, while still allowing dynamic orientation, position, and opacity. It’s fast to draw but slower to init (same as an ImageStim).

You specify the part of the screen to capture (in norm units), and optionally the stimuli themselves (as a list of items to be drawn). You get a screenshot of those pixels. If your OpenGL does not support arbitrary sizes, the image will be larger, using square powers of two if needed, with the excess image being invisible (using alpha). The aim is to preserve the buffer contents as rendered.

Checks for OpenGL 2.1+, or uses square-power-of-2 images.

Example:

# define lots of stimuli, make a list:
mySimpleImageStim = ...
myTextStim = ...
stimList = [mySimpleImageStim, myTextStim]

# draw stim list items & capture (slow; see EXP log for times):
screenshot = visual.BufferImageStim(myWin, stim=stimList)

# render to screen (very fast, except for the first draw):
while <conditions>:
screenshot.draw()  # fast; can vary .ori, .pos, .opacity
other_stuff.draw() # dynamic
myWin.flip()


See coder Demos > stimuli > bufferImageStim.py for a demo, with timing stats.

Author
• 2010 Jeremy Gray, with on-going fixes

Parameters
buffer :

the screen buffer to capture from, default is ‘back’ (hidden). ‘front’ is the buffer in view after win.flip()

rect :

a list of edges [left, top, right, bottom] defining a screen rectangle which is the area to capture from the screen, given in norm units. default is fullscreen: [-1, 1, 1, -1]

stim :

a list of item(s) to be drawn to the back buffer (in order). The back buffer is first cleared (without the win being flip()ed), then stim items are drawn, and finally the buffer (or part of it) is captured. Each item needs to have its own .draw() method, and have the same window as win.

interpolate :

whether to use interpolation (default = True, generally good, especially if you change the orientation)

sqPower2 :
• False (default) = use rect for size if OpenGL = 2.1+

• True = use square, power-of-two image sizes

flipHoriz :

horizontally flip (mirror) the captured image, default = False

flipVert :

vertically flip (mirror) the captured image; default = False

_calcPosRendered()

DEPRECATED in 1.80.00. This functionality is now handled by _updateVertices() and verticesPix.

_calcSizeRendered()

DEPRECATED in 1.80.00. This functionality is now handled by _updateVertices() and verticesPix

_getDesiredRGB(rgb, colorSpace, contrast)

Convert color to RGB while adding contrast. Requires self.rgb, self.colorSpace and self.contrast

_getPolyAsRendered()

DEPRECATED. Return a list of vertices as rendered.

_selectWindow(win)

Switch drawing to the specified window. Calls the window’s _setCurrent() method which handles the switch.

_set(attrib, val, op='', log=None)

DEPRECATED since 1.80.04 + 1. Use setAttribute() and val2array() instead.

_updateList()

The user shouldn’t need this method since it gets called after every call to .set() Chooses between using and not using shaders each call.

_updateListNoShaders()

The user shouldn’t need this method since it gets called after every call to .set() Basically it updates the OpenGL representation of your stimulus if some parameter of the stimulus changes. Call it if you change a property manually rather than using the .set() command.

_updateListShaders()

The user shouldn’t need this method since it gets called after every call to .set() Basically it updates the OpenGL representation of your stimulus if some parameter of the stimulus changes. Call it if you change a property manually rather than using the .set() command

_updateVertices()

Sets Stim.verticesPix and ._borderPix from pos, size, ori, flipVert, flipHoriz

autoDraw

Determines whether the stimulus should be automatically drawn on every frame flip.

Value should be: True or False. You do NOT need to set this on every frame flip!

autoLog

Whether every change in this stimulus should be auto logged.

Value should be: True or False. Set to False if your stimulus is updating frequently (e.g. updating its position every frame) and you want to avoid swamping the log file with messages that aren’t likely to be useful.

property backColor

Alternative way of setting fillColor

property borderColor
clearTextures()

Clear all textures associated with the stimulus.

As of v1.61.00 this is called automatically during garbage collection of your stimulus, so doesn’t need calling explicitly by the user.

property color

Alternative way of setting foreColor.

property colorSpace

The name of the color space currently being used

Value should be: a string or None

For strings and hex values this is not needed. If None the default colorSpace for the stimulus is used (defined during initialisation).

Please note that changing colorSpace does not change stimulus parameters. Thus you usually want to specify colorSpace before setting the color. Example:

# A light green text
stim = visual.TextStim(win, 'Color me!',
color=(0, 1, 0), colorSpace='rgb')

# An almost-black text
stim.colorSpace = 'rgb255'

# Make it light green again
stim.color = (128, 255, 128)

contains(x, y=None, units=None)

Returns True if a point x,y is inside the stimulus’ border.

Can accept variety of input options:
• two separate args, x and y

• one arg (list, tuple or array) containing two vals (x,y)

• an object with a getPos() method that returns x,y, such

as a Mouse.

Returns True if the point is within the area defined either by its border attribute (if one defined), or its vertices attribute if there is no .border. This method handles complex shapes, including concavities and self-crossings.

Note that, if your stimulus uses a mask (such as a Gaussian) then this is not accounted for by the contains method; the extent of the stimulus is determined purely by the size, position (pos), and orientation (ori) settings (and by the vertices for shape stimuli).

See Coder demos: shapeContains.py See Coder demos: shapeContains.py

property contrast

A value that is simply multiplied by the color.

Value should be: a float between -1 (negative) and 1 (unchanged).

Operations supported.

Set the contrast of the stimulus, i.e. scales how far the stimulus deviates from the middle grey. You can also use the stimulus opacity to control contrast, but that cannot be negative.

Examples:

stim.contrast =  1.0  # unchanged contrast
stim.contrast =  0.5  # decrease contrast
stim.contrast =  0.0  # uniform, no contrast
stim.contrast = -0.5  # slightly inverted
stim.contrast = -1.0  # totally inverted


Setting contrast outside range -1 to 1 is permitted, but may produce strange results if color values exceeds the monitor limits.:

stim.contrast =  1.2  # increases contrast
stim.contrast = -1.2  # inverts with increased contrast

depth

DEPRECATED, depth is now controlled simply by drawing order.

draw(win=None)[source]

Draws the BufferImage on the screen, similar to ImageStim .draw(). Allows dynamic position, size, rotation, mirroring, and opacity. Limitations / bugs: not sure what happens with shaders and self._updateList()

property fillColor

Set the fill color for the shape.

flipHoriz

If set to True then the image will be flipped horizontally (left-to-right). Note that this is relative to the original image, not relative to the current state.

flipVert

If set to True then the image will be flipped vertically (left-to-right). Note that this is relative to the original image, not relative to the current state.

property foreColor

Foreground color of the stimulus

Value should be one of:

When color is specified using numbers, it is interpreted with respect to the stimulus’ current colorSpace. If color is given as a single value (scalar) then this will be applied to all 3 channels.

Examples

For whatever stim you have:

stim.color = 'white'
stim.color = 'RoyalBlue'  # (the case is actually ignored)
stim.color = '#DDA0DD'  # DDA0DD is hexadecimal for plum
stim.color = [1.0, -1.0, -1.0]  # if stim.colorSpace='rgb':
# a red color in rgb space
stim.color = [0.0, 45.0, 1.0]  # if stim.colorSpace='dkl':
# DKL space with elev=0, azimuth=45
stim.color = [0, 0, 255]  # if stim.colorSpace='rgb255':
# a blue stimulus using rgb255 space
stim.color = 255  # interpreted as (255, 255, 255)
# which is white in rgb255.


Operations work as normal for all numeric colorSpaces (e.g. ‘rgb’, ‘hsv’ and ‘rgb255’) but not for strings, like named and hex. For example, assuming that colorSpace=’rgb’:

stim.color += [1, 1, 1]  # increment all guns by 1 value
stim.color *= -1  # multiply the color by -1 (which in this
# space inverts the contrast)
stim.color *= [0.5, 0, 1]  # decrease red, remove green, keep blue


You can use setColor if you want to set color and colorSpace in one line. These two are equivalent:

stim.setColor((0, 128, 255), 'rgb255')
# ... is equivalent to
stim.colorSpace = 'rgb255'
stim.color = (0, 128, 255)

image

The image file to be presented (most formats supported).

interpolate

Whether to interpolate (linearly) the texture in the stimulus.

If set to False then nearest neighbour will be used when needed, otherwise some form of interpolation will be used.

property lineColor

Alternative way of setting borderColor.

mask

The alpha mask that can be used to control the outer shape of the stimulus

• None, ‘circle’, ‘gauss’, ‘raisedCos’

• or the name of an image file (most formats supported)

• or a numpy array (1xN or NxN) ranging -1:1

maskParams

Various types of input. Default to None.

This is used to pass additional parameters to the mask if those are needed.

• For ‘gauss’ mask, pass dict {‘sd’: 5} to control

standard deviation.

• For the ‘raisedCos’ mask, pass a dict: {‘fringeWidth’:0.2},

where ‘fringeWidth’ is a parameter (float, 0-1), determining the proportion of the patch that will be blurred by the raised cosine edge.

name

The name (str) of the object to be using during logged messages about this stim. If you have multiple stimuli in your experiment this really helps to make sense of log files!

If name = None your stimulus will be called “unnamed <type>”, e.g. visual.TextStim(win) will be called “unnamed TextStim” in the logs.

property opacity

Determines how visible the stimulus is relative to background.

The value should be a single float ranging 1.0 (opaque) to 0.0 (transparent). Operations are supported. Precisely how this is used depends on the Blend Mode.

ori

The orientation of the stimulus (in degrees).

Should be a single value (scalar). Operations are supported.

Orientation convention is like a clock: 0 is vertical, and positive values rotate clockwise. Beyond 360 and below zero values wrap appropriately.

overlaps(polygon)

Returns True if this stimulus intersects another one.

If polygon is another stimulus instance, then the vertices and location of that stimulus will be used as the polygon. Overlap detection is typically very good, but it can fail with very pointy shapes in a crossed-swords configuration.

Note that, if your stimulus uses a mask (such as a Gaussian blob) then this is not accounted for by the overlaps method; the extent of the stimulus is determined purely by the size, pos, and orientation settings (and by the vertices for shape stimuli).

See coder demo, shapeContains.py

pos

The position of the center of the stimulus in the stimulus units

value should be an x,y-pair. Operations are also supported.

Example:

stim.pos = (0.5, 0)  # Set slightly to the right of center
stim.pos += (0.5, -1)  # Increment pos rightwards and upwards.
Is now (1.0, -1.0)
stim.pos *= 0.2  # Move stim towards the center.
Is now (0.2, -0.2)


Tip: If you need the position of stim in pixels, you can obtain it like this:

from psychopy.tools.monitorunittools import posToPix
posPix = posToPix(stim)

setAutoDraw(value, log=None)

Sets autoDraw. Usually you can use ‘stim.attribute = value’ syntax instead, but use this method to suppress the log message.

setAutoLog(value=True, log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setBackColor(color, colorSpace=None, operation='', log=None)
setBorderColor(color, colorSpace=None, operation='', log=None)

Hard setter for fillColor, allows suppression of the log message, simultaneous colorSpace setting and calls update methods.

setColor(color, colorSpace=None, operation='', log=None)
setContrast(newContrast, operation='', log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message

setDKL(newDKL, operation='')

DEPRECATED since v1.60.05: Please use the color attribute

setDepth(newDepth, operation='', log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message

setFillColor(color, colorSpace=None, operation='', log=None)

Hard setter for fillColor, allows suppression of the log message, simultaneous colorSpace setting and calls update methods.

setFlipHoriz(newVal=True, log=None)[source]

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setFlipVert(newVal=True, log=None)[source]

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setForeColor(color, colorSpace=None, operation='', log=None)

Hard setter for foreColor, allows suppression of the log message, simultaneous colorSpace setting and calls update methods.

setImage(value, log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setLMS(newLMS, operation='')

DEPRECATED since v1.60.05: Please use the color attribute

setLineColor(color, colorSpace=None, operation='', log=None)
setMask(value, log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setOpacity(newOpacity, operation='', log=None)

Hard setter for opacity, allows the suppression of log messages and calls the update method

setOri(newOri, operation='', log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message

setPos(newPos, operation='', log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message.

setRGB(newRGB, operation='', log=None)

DEPRECATED since v1.60.05: Please use the color attribute

setSize(newSize, operation='', units=None, log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message

setUseShaders(value=True, log=None)

Usually you can use ‘stim.attribute = value’ syntax instead, but use this method if you need to suppress the log message

size

The size (width, height) of the stimulus in the stimulus units

Value should be x,y-pair, scalar (applies to both dimensions) or None (resets to default). Operations are supported.

Sizes can be negative (causing a mirror-image reversal) and can extend beyond the window.

Example:

stim.size = 0.8  # Set size to (xsize, ysize) = (0.8, 0.8)
print(stim.size)  # Outputs array([0.8, 0.8])
stim.size += (0.5, -0.5)  # make wider and flatter: (1.3, 0.3)


Tip: if you can see the actual pixel range this corresponds to by looking at stim._sizeRendered

texRes

Power-of-two int. Sets the resolution of the mask and texture. texRes is overridden if an array or image is provided as mask.

Operations supported.

units

Units to use when drawing.

Possible options are: None, ‘norm’, ‘cm’, ‘deg’, ‘degFlat’, ‘degFlatPos’, or ‘pix’.

If None then the current units of the Window will be used. See Units for the window and stimuli for explanation of other options.

Note that when you change units, you don’t change the stimulus parameters and it is likely to change appearance.

Example:

# This stimulus is 20% wide and 50% tall with respect to window
stim = visual.PatchStim(win, units='norm', size=(0.2, 0.5)

# This stimulus is 0.2 degrees wide and 0.5 degrees tall.
stim.units = 'deg'

updateColors()

Placeholder method to update colours when set externally, for example updating the pallette attribute of a textbox

updateOpacity()

Placeholder method to update colours when set externally, for example updating the pallette attribute of a textbox.

useShaders

Should shaders be used to render the stimulus (typically leave as True)

If the system support the use of OpenGL shader language then leaving this set to True is highly recommended. If shaders cannot be used then various operations will be slower (notably, changes to stimulus color or contrast)

property verticesPix

This determines the coordinates of the vertices for the current stimulus in pixels, accounting for size, ori, pos and units

win
The Window object in which the

stimulus will be rendered by default. (required)

Example, drawing same stimulus in two different windows and display simultaneously. Assuming that you have two windows and a stimulus (win1, win2 and stim):

   stim.win = win1  # stimulus will be drawn in win1
stim.draw()  # stimulus is now drawn to win1
stim.win = win2  # stimulus will be drawn in win2
stim.draw()  # it is now drawn in win2
win1.flip(waitBlanking=False)  # do not wait for next
# monitor update
win2.flip()  # wait for vertical blanking.

Note that this just changes **default** window for stimulus.

You could also specify window-to-draw-to when drawing::

stim.draw(win1)
stim.draw(win2)