Builder: NoiseStim to ImageStim

Hi There,

You can feed set an alpha mask to crop the image and then save the window to save the image.

Try:


#import modules
from psychopy import core, visual 
import numpy as np

# Create a window to draw in
win = visual.Window(size=(600, 600), color='black', monitor='testMonitor')

#function to crop np array 
def crop_center(img,cropx,cropy):
    y,x = img.shape
    startx = x//2-(cropx//2)
    starty = y//2-(cropy//2)    
    img[starty:starty+cropy,startx:startx+cropx]=1
    return img

# create Noise image <---- copied from original post
distractor = visual.NoiseStim(
win=win, name='distractor',units='pix',
noiseImage='Stimuli/cowLeftCropped.jpg', mask='none',
ori=0, pos=[0,0], size=(512,512), sf=None,
phase=0.0,
color=[1,1,1], colorSpace='rgb', opacity=1, blendmode='avg', contrast=1.0,
texRes=128, filter=None,
noiseType='Image', noiseElementSize=0.0625,
noiseBaseSf=8.0, noiseBW=1,
noiseBWO=30, noiseOri=0.0,
noiseFractalPower=0.0,noiseFilterLower=1.0,
noiseFilterUpper=8.0, noiseFilterOrder=0.0,
noiseClip=3.0, imageComponent='Phase', interpolate=False, depth=-2.0)

#build the noise 
distractor.buildNoise()

#generate custom alpha mask using np array (values range from -1 to 1)
x1, y1 = 512, 512 #original dimensions
x2, y2 = 416, 315 #new dimensions
a = 0-np.ones([x1, y1])
custom_mask = crop_center(a, x2, y2)

#apply the mask
distractor.setMask(custom_mask)

#draw the image
distractor.draw()
win.flip()

#save the current screen
win.getMovieFrame()
win.saveMovieFrames("Stimuli/noiseStim.png")

core.wait(1)

Hopefully does what you need :slight_smile:
Becca