GCien
GCien

Reputation: 2349

How to slice off 1 pixel layer from an image data array with numpy or similar

I have a fits image of specified dimensions, in pixels, and I wish to slice off one pixel off the top and one on the bottom. I have attempted to use:

sliced_array = my_array[1:-1,1:0]

However, this gives me, when I query the shape of the newly sliced array using print(sliced_array.shape):

(4070, 0)

How would this be achieved?

N.B. I'm working within Python, and the numpy and astropy modules.

Upvotes: 0

Views: 721

Answers (2)

MSeifert
MSeifert

Reputation: 152647

You can always omit the "stop" value if you want to slice your array without stop (so until it reaches the end). Just as you omitted the "step" value for all axis.

Following that logic you can use:

sliced_array = my_array[1:-1,1:]

and you should get the right result.

Maybe as an example if you wanted to slice one row from the top and not the bottom you could also omit the start value and only give an end value:

sliced_array = my_array[1:-1,:-1]

One remark though: Depending on your image I'm not sure if you got horizontal and vertical right. With FITS images the x-axis is axis 1 and the y-axis is axis 0. Not sure if this affects you though.

If this would be the case you would need to change it to:

sliced_array = my_array[:-1,1:-1]

Upvotes: 1

dnit13
dnit13

Reputation: 2496

You can slice top most layer and bottom most layer like this

my_array[1:-1,:] 

preserving all the columns and excluding top most row and bottom most row

Upvotes: 2

Related Questions