How to crop a FITS image in Python in a given shape?

1.1k Views Asked by At

I want to crop a FITS image area between two circles. For example, I want to crop out an area in the FITS image between circles of radii of R1 and R2. How do I go about it?

1

There are 1 best solutions below

0
saimn On

You will probably find what you're looking for in the regions package: https://astropy-regions.readthedocs.io/en/latest/

It can create regions with pixel or sky coordinates, combine them, and create pixel masks:

In [1]: from regions import CirclePixelRegion, PixCoord                                                  

In [2]: c1 = CirclePixelRegion(center=PixCoord(6, 6), radius=3)                                          

In [3]: c2 = CirclePixelRegion(center=PixCoord(9, 9), radius=3)                                          

In [4]: (c1 & c2).to_mask().data                                                                         
Out[4]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

In [5]: (c1 | c2).to_mask().data                                                                         
Out[5]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])