I have a 3D numpy array, where I threat it like a 2D array with multiple layers. In the example below, I have 2 layer 2d array, with width 4 and height 3.
import numpy
mapTiles=numpy.array(([[1,1,1,1],
[1,1,1,1],
[1,1,1,1]],
[[2,0,2,2],
[0,0,2,2],
[0,0,0,2]]))
I need to transform this numpy array in a similar way to Gimp/Photoshop/Aseprite canvas. So using height, width, and vertical and horizontal offset values.
-
If the resulting array is bigger, keep dimensions aligned top/left and fill with zeroes.
-
If the resulting array is smaller, just cut.
-
If the offset falls outside of width and height limit, just cut. Fill empty spaces with zeroes.
I could only conceive this with lots of if
conditions, below is just one of the possible cases. Is there any already made specialized method for this? To create a new array and copy over, is there an alternative to just use for
?
mapResize(mapTiles, width,height,offsetx=0,offsety=0):
if(width-offsetx < len(mapTiles[0][0]) and height-offsety < len(mapTiles[0])):
x=min(max(offsetx,0),len(mapTiles[0][0]))
y=min(max(offsety,0),len(mapTiles[0]))
return mapTiles[:,y:(y+height),x:(x+width)]
2
Answers
I would create the new array then copy the data into it. This will require slices to be set up for both the source and the destination arrays.
I think this does it (next time try to show expected output):
Basically, make a big padded array and slice it to size.