Dec-03-2021, 04:32 AM
I'm working on a project where I'm using numpy for generative art, and I've got a question with how to filter an array without copying the data (so the original array will still be edited by a change to the filtered version). It works great for index slices, but apparently copies the data if you slice using another array as a mask?
I'm hoping this is actually super simple, and I just don't know what word numpy calls it.
I'm hoping this is actually super simple, and I just don't know what word numpy calls it.
import numpy as np
from PIL import Image
# small image, all black
df = np.zeros((100, 100, 3), dtype=np.uint8)
mask = np.zeros((100, 100), 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] = True
use_working_method = False
if use_working_method:
# red diagonal line
df[mask, 0] = 255
else:
view = df[mask, :]
# ...sometime later
view[:, 0] = 255
# for reference, this does work:
view1 = df[45:55, 45:55, :]
view1[:, :, 2] = 255 # blue, centered
im = Image.fromarray(df)
im.show()
