Skip to content

Instantly share code, notes, and snippets.

@wkruijne
Created March 26, 2015 18:15
Show Gist options
  • Save wkruijne/0aea7064b6907261164a to your computer and use it in GitHub Desktop.
Save wkruijne/0aea7064b6907261164a to your computer and use it in GitHub Desktop.
### Image loading...
exp.imfolder = os.path.join(exp.experiment_path, '__pool__')
exp.all_img = [scipy.misc.imread( os.path.join(exp.imfolder,im) )
for im in os.listdir(exp.imfolder) if '.jpg' in im]
### "cut out center" of these images:
img = exp.all_img[0]
xc,yc= img.shape[0]//2, img.shape[0]//2
rr,cc = skimage.draw.circle(yc,xc, 45)
#### turn it into a PIL image:
for i in range(len(exp.all_img)):
exp.all_img[i][rr,cc] = 255
exp.all_img[i] = Image.fromarray(exp.all_img[i])
########## colors:
hs = np.linspace(0.00, 1.00, 7)
cols = []
for h in hs[0:-1:2]:
# cols += [colorsys.hsv_to_rgb(h, 1.0, 0.5)]
rgb = np.array(colorsys.hsv_to_rgb(h,1,.5))
cols += [rgb] # for psychopy colors, : cols *2-1
exp.cols = cols
################################# And I'm doing the same thing with the masks #################################
# note that It's kind of misleading, because from the perspective of PIL.Image.composite
exp.maskpath = exp.experiment_path
exp.imsz = imsz = (768, 1024)
# for n in xrange(exp.n_unique_masks):
for n in range(5): # yields 5 x 4 x 2 = 40 unique masks
msk = genmask(imsz)
# create all permutations of it
msk = msk[:, :, np.newaxis]
mskset = np.concatenate((msk,
np.flipud(msk), np.fliplr(msk), np.flipud(np.fliplr(msk)) ), axis=2)
# and their 'negatives':
mskset = np.concatenate((mskset, 1-mskset), axis=2)
# try stacking it:
try:
allmsk = np.concatenate((allmsk, mskset), axis=2)
except NameError, e: # because it doesn't exist yet.
allmsk = mskset
np.save( exp.maskpath+'msks'+ str(exp.subject_nr), allmsk)
# Turn the masks into images:
exp.maskbank = []
cols = [exp.col2uint8(c) for c in exp.cols]
for i in range(allmsk.shape[-1]):
trial_mask = allmsk[:,:,i]
mskIm = (trial_mask [:,:,np.newaxis] * cols[0] +
(1-trial_mask[:,:,np.newaxis]) * cols[1]).astype('uint8')
exp.maskbank.append(Image.fromarray(mskIm))
############## and the bg is defined simply as ....
exp.bgim = Image.new('RGB', (imsz[1],imsz[0]), (0,0,0) )
################# Then all you have to do in the prep-phase of a trial is...
trial_mask = exp.maskbank[exp.get('mask_idx')]
img = exp.all_img[exp.get('img_idx')]
out = Image.composite(exp.bgim, trial_mask, img )
# create a canv out of it
stim = ImageStim(win=win, image=out)
cnv = canvas(exp)
cnv.stim_list.append(stim)
exp.imcnv = cnv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment