Image components don’t have an innate method to set gaussian blur, but you can do it! What you’ll need to do is read in your image as a PIL
image, convert it to a numpy
array, then apply a gaussian transformation to it. So, at the start of your experiment, you’d do this:
from PIL import Image
# Get image as a PIL
imgPil = Image.open("C:\\Users\\Work\\Downloads\\man.jpg")
# Convert to a numpy array
imgData = np.flip(np.asarray(imgPil), 0)
# Split into red, green and blue channels and convert to rgb1 color space
r = imgData[:, :, 0] / 255
g = imgData[:, :, 1] / 255
b = imgData[:, :, 2] / 255
From there you have two options: You can either do the gaussian blurring each frame according to the slider value, making it fully continuous but computationally intensive, or you can create several different blurred images at the beginning and then access them according to the slider value, making it discrete but fast. So the first option might look like this:
Each Frame
# Blur each color channel
rBlurred = scipy.ndimage.gaussian_filter(r, slider.markerPos)
gBlurred = scipy.ndimage.gaussian_filter(g, slider.markerPos)
bBlurred = scipy.ndimage.gaussian_filter(b, slider.markerPos)
# Apply to image component
img_1.image = np.dstack((rBlurred, gBlurred, bBlurred))
(you’ll need to add import scipy
to your Begin Experiment tab in the code component)
However, as this is calculating gaussians each frame, it’s going to be pretty slow unless you have a very good computer or a very small image. Instead, you could do this:
Before Experiment
# Make a dict of pre-blurred images
imgBlurred = {}
for sigma in np.array(range(0, 11)): # to do it every .1, do np.array(range(0, 101)) / 10
# Blur each color channel
rBlurred = scipy.ndimage.gaussian_filter(r, sigma)
gBlurred = scipy.ndimage.gaussian_filter(g, sigma)
bBlurred = scipy.ndimage.gaussian_filter(b, sigma)
# Store blurred image
imgBlurred[sigma] = np.dstack((rBlurred, gBlurred, bBlurred))
Each Frame
# Set image from dict
obj.image = imgBlurred[slider.markerPos]
So you’re essentially doing the blurring at the beginning then just using the slider value to access pre-blurred images. Be sure to set the granularity of your slider to match the granularity of your blurs dict though, otherwise you’ll get indexing errors!
You’ll end up with something like this: