【问题标题】:How to convert (or scale) a FITS image with Astropy如何使用 Astropy 转换(或缩放)FITS 图像
【发布时间】:2017-07-25 13:46:00
【问题描述】:

使用 Astropy 库,我创建了一个 FITS 图像,它是通过从 2 个实际 FITS 图像(它们被缩放为“int16”,我使用的软件的正确格式:Maxim DL )。

但是这张图片的比例是float64而不是int16。而且任何天文处理软件都无法读取(FITS Liberator除外)

您知道如何进行吗?我们可以通过更改标题中的“BITPIX”来转换FITS图像吗?

我试过了:(按照这个方法:Why is an image containing integer data being converted unexpectedly to floats?

from astropy.io import fits

hdu1=fits.open('mypicture.fit')
image=hdu1[0]
print(image.header['BITPIX'])  # it gives : -64

image.scale('int16')
data=image.data
data.dtype
print(image.header['BITPIX']) # it gives : 16
hdu1.close()

但是,当我检查新修改的“mypicture.fit”比例时,它仍然显示-64! 没有保存和应用任何更改!

【问题讨论】:

  • 如果我没记错的话,图像数据是一个 numpy 数组。不能对数组进行转换,另存为新的.fits文件吗?
  • 是的,你是对的。因此,您建议使用 fit.writeto() 创建一个新的 .fits 文件?
  • 这就是我过去所做的。
  • 好的。 @Jacobadtr:如果我很好理解,我只在脚本末尾添加这一行:fits.writeto('mypicture_converted.fit',fits.getdata(mypicture.fit'), None, 'ignore')
  • 但是不行……格式还是float64而不是int16

标签: python image scale astropy fits


【解决方案1】:

如果我正确理解您的问题,这应该可以。

from astropy.io import fits
import numpy as np

# create dummy fits file
a = np.array([[1,2,3],
              [4,5,6],
              [7,8,9]],dtype=np.float64)

hdu = fits.PrimaryHDU()
hdu.data = a

# looking at the header object confirms BITPIX = -64
hdu.header

# change data type
hdu.data = np.int16(hdu.data)

# look again to confirm BITPIX = 16
hdu.header

【讨论】:

  • 非常感谢@jm22b!函数“np.int16(hdu.data)”就是我要找的!
猜你喜欢
  • 2022-11-21
  • 2011-11-12
  • 2020-03-03
  • 1970-01-01
  • 2019-08-02
  • 2016-12-07
  • 2015-06-25
  • 2014-05-18
  • 1970-01-01
相关资源
最近更新 更多