I have this function for modifying an array:
import numpy as np
import skimage.draw as dw
import skimage.segmentation as seg
def draw_territories(map_file,system_list,color_dict):
for size in range(0,120,1):
for system in system_list:
rr,cc = dw.circle_perimeter(system.x_coord,system.y_coord,size, method="andres", shape=(map_file.shape))
map_file[rr,cc] = np.where(map_file[rr,cc]==(255,255,255),color_dict[system.owner_ID],map_file[rr,cc])
## This is where I want to add code ##
borders = (seg.find_boundaries(map_file[:,:,0],mode='thick'))
map_file[borders] = 0
return None
The map_file
array represents a 4000x4000 map, with the third dimension for the color in RGB. It works by drawing increasingly larger colored circles (via the loop) around occupied positions (from system_list
) on the map, with the color depending on a dictionary color_dict
. It then draws borders around the territories. All of this is confirmed to work.
I'm now trying to add the following line where the hashes are in the above code block.
map_file = np.where(map_file==[254,254,254],[255,255,255],map_file)
The [254,254,254]
is a stand-in color for unoccupied points, and I want to revert it to the background white color before drawing the borders.
For some reason, the added line does nothing at all within the function, and the two following lines cease to work as well. Yet if I run them outside the function instead of within the function, all three work as expected. Why is this happening, and how should I fix it?
You're assigning to the local variable, not the caller's variable.
If you want to overwrite the array in place, assign to map_file[...]
.
map_file[...] = np.where(map_file==[254,254,254],[255,255,255],map_file)
See https://stackoverflow.com/a/48196246/1491895
But you could instead use return map_file
instead of return None
. Then change the callers to assign the result to the variable that they passed as the first argument.