각 이미지에 대해 WCS (World Coordinate System)를 만들려고합니다. 이것은 기본적으로 픽셀 좌표와 하늘 좌표 (RA 및 Dec) 간의 매핑입니다. http://astrometry.net과 같은 도구를 사용하여 이미지에 표시되는 별 모양을 기반으로 이미지를 자동으로 해결할 수 있습니다. 이렇게하면 이미지에 대한 WCS가 생성됩니다.
astrometry.net 솔버 (적합한 종속성을 설치 한 경우)는 알려진 천체가 표시된 이미지의 png 버전을 생성 할 수 있습니다. 이것은 당신의 목적에 충분할 수도 있지만 그렇지 않다면 astropy.wcs 패키지를 사용하여 파이썬에 이미지 WCS를 읽어 이미지의 방향을 결정한 다음 원하는대로 이미지를 마크 업할 수 있습니다.
import math
import subprocess
import astropy.units as u
import astropy.fits as fits
## Solve the image using the astrometry.net solve-field tool.
## You'll want to look over the options for solve-field and adapt this call
## to your images.
output = subprocess.check_output(['solve-field', filename])
## Read Header of Image (assumes you are working off a fits file with a WCS)
## If not, you can probably read the text header output my astrometry.net in
## a similar fashion.
hdulist = fits.open(solvedFilename)
header = hdulist[0].header
hdulist.close()
CD11 = float(header['CD1_1'])
CD12 = float(header['CD1_2'])
CD21 = float(header['CD2_1'])
CD22 = float(header['CD2_2'])
## This is my code to interpet the CD matrix in the WCS and determine the
## image orientation (position angle) and flip status. I've used it and it
## seems to work, but there are some edge cases which are untested, so it
## might fail in those cases.
## Note: I'm using astropy units below, you can strip those out if you keep
## track of degrees and radians manually.
if (abs(CD21) > abs(CD22)) and (CD21 >= 0):
North = "Right"
positionAngle = 270.*u.deg + math.degrees(math.atan(CD22/CD21))*u.deg
elif (abs(CD21) > abs(CD22)) and (CD21 < 0):
North = "Left"
positionAngle = 90.*u.deg + math.degrees(math.atan(CD22/CD21))*u.deg
elif (abs(CD21) < abs(CD22)) and (CD22 >= 0):
North = "Up"
positionAngle = 0.*u.deg + math.degrees(math.atan(CD21/CD22))*u.deg
elif (abs(CD21) < abs(CD22)) and (CD22 < 0):
North = "Down"
positionAngle = 180.*u.deg + math.degrees(math.atan(CD21/CD22))*u.deg
if (abs(CD11) > abs(CD12)) and (CD11 > 0): East = "Right"
if (abs(CD11) > abs(CD12)) and (CD11 < 0): East = "Left"
if (abs(CD11) < abs(CD12)) and (CD12 > 0): East = "Up"
if (abs(CD11) < abs(CD12)) and (CD12 < 0): East = "Down"
if North == "Up" and East == "Left": imageFlipped = False
if North == "Up" and East == "Right": imageFlipped = True
if North == "Down" and East == "Left": imageFlipped = True
if North == "Down" and East == "Right": imageFlipped = False
if North == "Right" and East == "Up": imageFlipped = False
if North == "Right" and East == "Down": imageFlipped = True
if North == "Left" and East == "Up": imageFlipped = True
if North == "Left" and East == "Down": imageFlipped = False
print("Position angle of WCS is {0:.1f} degrees.".format(positionAngle.to(u.deg).value))
print("Image orientation is North {0}, East {1}.".format(North, East))
if imageFlipped:
print("Image is mirrored.")
## Now you have position angle and flip status and can mark up your image
:
다음은 목적에 적응하려고 할 수있는 몇 가지 신속하고 더러운 코드입니다