Builder: NoiseStim to ImageStim

If this template helps then use it. If not then just delete and start from scratch.

Mac OSX 10.15
PsychoPy version2020.1.2
**Standard Standalone? (Y) If not then what?:Standalone
**What are you trying to achieve?: Create a NoiseStim (with NoiseComponent) and use the output as the input for a ImageStim Component. The noise will be a phase scrambled version of an image. The original image must be a power of 2 (E.G. 512 X 512). I can create that either with builder (preferred) or short code segment. But then I would like to crop the output of that to 416 x 315 pixels. In code (or builder) I can do something like this:

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)
distractor.buildNoise()

But then how do I crop it and present it. If there was a buildnoise.save function I could save a cropped file (if there was a .crop) and present it later since I want the same phase scrambled “distractor” on each trial. I do not know how to crop it or same it. There is a PIL crop save functions but I do not know how to get an noiseStim a PIL stim or visa veras.

Thank you.

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

Becca,
Thank you so much for your cropping function. I was able to put it in my (mostly builder) experiment without any trouble. Sorry it took so long to acknowledge your help. It works like a charm so now I can create noise that’s a power2, and then make it any size you want.
BP

Ah fantastic, no worries, pleased to have helped :slight_smile: !