I've got it working :)
The mask maintains all data, and you need to specifically filter out the masked values when altering the view. It means I'll need to rewrite the rest of my code to work with masks, but at least now I can do what I wanted :)
Working code to select a diagonal bar of the image, and paint just one channel of the selection:
The mask maintains all data, and you need to specifically filter out the masked values when altering the view. It means I'll need to rewrite the rest of my code to work with masks, but at least now I can do what I wanted :)
Working code to select a diagonal bar of the image, and paint just one channel of the selection:
import numpy as np import numpy.ma as ma from PIL import Image def get_array(): # small image, all black df = np.zeros((100, 100, 3), dtype=np.uint8) # convert to masked array, without masking anything return ma.array(df) def select_data(df): mask = np.ones(df.shape[0:2], dtype=bool) band = 10 for rndx, row in enumerate(mask): for cndx, col in enumerate(mask[rndx]): if abs(cndx - rndx) <= band: mask[rndx, cndx] = False mask = np.repeat(mask[:, :, np.newaxis], 3, 2) view = ma.MaskedArray(df, mask=mask) return view def alter_data(df): # only change red channels = df.mask.copy() channels[:, :, :] = True channels[:, :, 0] = False view = ma.MaskedArray(df, mask=channels) view[~view.mask] = 255 def show_image(df): im = Image.fromarray(df) im.show() def main(): df = get_array() work_set = select_data(df) alter_data(work_set) show_image(df) if __name__ == "__main__": main()This was needed, because I'm working on a generative algorithm that combines selectors together, and then makes changes to the selected regions. For example, I might combine a Diagonal selector with a Blinds selector to get rectangular boxes in a diagonal pattern, evenly spaced, and equal widths, that I then paint over. It's really just an excuse for me to play with numpy, lol