0

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.

3
  • 1
    my_array[1:-1,0:1] ? Commented Feb 17, 2016 at 12:10
  • @dnit13 You got me halfway to the answer! my_array[1:-1,0:1] gave me (4070, 1), so with a little logical deduction I tried [1:-1,0:4070]. Gave me (4070,4070). Thanks! (If you add this as the answer for a generic array of any size I will accept it!) Commented Feb 17, 2016 at 12:12
  • Since this question is not really astropy/fits related would you mind deleting these two tags? Commented Feb 17, 2016 at 12:36

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.