代码之家  ›  专栏  ›  技术社区  ›  user7345804

如何查看此内容。是否在不调整大小和颜色的情况下适合图像?

  •  1
  • user7345804  · 技术社区  · 8 年前

    我正在尝试从 '.fits' 文件但是,当与相应的 '.gif' 图像,其颜色和大小似乎有错误。

    如何在适当的维度上查看真实的彩色图像?

    例如,可以选择 '。适合' 文件和对应文件 '。gif' 文件 located at the top of this webpage 。我的示例代码使用 APLPY 模块,如下所示。

    def from_fits_to_image(color_scheme, rloc, rname='synop_Ml_0.2104', rext='.fits', cmap=None):
        """ 
        color_scheme : 'rgb', 'grayscale', or 'false color'; color scheme of image to be shown
        rloc         : type <str>; location of file to be read
        rname        : type <str>; name of file to be read
        rext         : type <str>; extension of file to be read
        cmap         : None or type <str>; colormap
        """
        rpath = rloc + rname + rext
        if color_scheme == 'rgb':
            pic = aplpy.FITSFigure(rpath)
            # pic.show_rgb(alt_filename) # what filename is supposed to go here?
        else:
            pic = aplpy.FITSFigure(rpath)
            if color_scheme == 'grayscale':
                pic.show_grayscale()
            elif color_scheme == 'false color':
                if cmap is None:
                    pic.show_colorscale()
                else:
                    pic.show_colorscale(cmap=cmap)
        # plt.savefig(...)
        plt.show()
    

    只要提供适当的 rloc (下载的 '。适合' 文件)和 color_scheme ,将运行上述代码。

    调用下面的函数将显示正确尺寸的空绘图。为了使其非空,我必须提供另一个现有的文件名,尽管我不清楚具体应该是什么。

    from_fits_to_image(color_scheme='rgb', rloc=rloc) 
    

    enter image description here

    下面的每个函数调用都会显示一个已调整为较小大小的绘图。虽然 color_scheme='grayscale' 似乎可以正确地为绘图着色,但其他方法无法正确地为图像着色。

    from_fits_to_image('grayscale', rloc=rloc)
    

    enter image description here

    from_fits_to_image('false color', rloc=rloc)
    

    enter image description here

    from_fits_to_image('false color', rloc=rloc, cmap='plasma')
    

    enter image description here

    为了进行比较 '。gif' 图片如下。理想情况下,输出将与下图完全相同。

    编辑:

    我试过使用 astropy ,则, PIL pyfits 未成功。任何帮助都将不胜感激。

    enter image description here

    编辑2:

    下面是使用 fits 从…起 astropy.io

    from astropy.io import fits
    
    def reada(rloc, rname='synop_Ml_0.1998', rext='.fits'):
        """ """
        rpath = rloc + rname + rext
        # hdu_list = fits.open(rpath)
        # hdu_list.info()
        pic = fits.getdata(rpath)
        plt.imshow(pic)
        plt.show()
    
    reada(rloc=rloc)
    

    我玩过 vmin vmax kwargs,但没有成功。此外,使用 皮菲茨 打开文件会导致以下错误,即使使用 pyfits.open(rpath, uint=True, do_not_scale_image_data=True) :

    TypeError: Image data can not convert to float
    

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  2
  •   Vlas Sokolov    8 年前

    从gif来看,这幅图像似乎是假彩色的,这是一个选择正确颜色贴图的问题。我不能说python中是否有与您链接的颜色对应的颜色映射,但我们可以找到非常接近的颜色映射,重新创建图像中的所有功能:

    fig = aplpy.FITSFigure('synop_Ml_0.2104.fits')
    fig.show_colorscale(vmin=-60, vmax=60, cmap='hot')
    

    这显示了以下内容(注意 aplpy 不理解此坐标系,因此它以像素坐标绘制图形): enter image description here

    问题的第二部分比较棘手,我无法完全回答。首先,您需要将卡林顿时间转换为经度,将正弦纬度转换为度,然后绘制轴标签的新值,而不是旧标签,或者将新值绘制为旧标签旁边的寄生轴(您可以参考寄生轴示例 here )。

    现在看起来卡林顿时间自1853年11月9日以来只旋转了几度,x轴的跨度正好是360,所以我假设转换只是偏移757079.95,即左边缘的x轴值。通过查看地图的像素跨度与坐标跨度的对应关系,我们可以在世界坐标上再次检查它:

    In [88]: fig._wcs.wcs_pix2world(fig._ax1.get_xlim(), fig._ax1.get_ylim(), origin=1)
    Out[88]: [array([757439.95, 757079.95]), array([-1.,  1.])]
    

    xaxis边的值757079.95和757439.95的差值正好是360度。

    那么我们可以用一些 matplotlib 手动偏移坐标值以使其从零变为360,并使X轴与gif图像匹配的技巧:

    # using hidden attributes is non-pythonic but aplpy does not leave us other options
    ax = fig._ax1
    x_range = ax.get_xlim()
    new_ticklabels = np.arange(60, 361, 60)
    new_tick_positions = new_ticklabels / 360. * x_range[1] + x_range[0]
    ax.set_xticks(new_tick_positions)
    ax.set_xticklabels(new_ticklabels)
    fig.axis_labels.set_xtext('Carrington Longitude')
    

    enter image description here

    请记住 aplpy公司 是一个用于绘制天体坐标(而非太阳坐标)的库,因此使轴变换正常工作可能是一个相当痛苦的过程。另一种可能更好的方法是使用 sunpy ,一条巨蟒 library 太阳物理学。然而,我从未使用过它,它似乎为这个特定的fits文件抛出了一个错误。看起来您需要修改fits文件的标题以正确读取坐标。也许你可以联系 阳光明媚 社区是否要使用该库?

    推荐文章