SPYBUG96
SPYBUG96

Reputation: 1117

How do you log stretch a FITS image and change its contrast?

I'm trying to use astropy 2.0.11 with python 2.7.15 to edit a fits image by applying a log stretch to it and change the contrast, and I have't been able to figure it out.

I've been trying to follow the tutorials on the astropy website for opening and manipulating fits files, but I'm wondering if the tutorials will only work for the latest version of astropy and on python 3?

Sorry about the organization of my code. This is prototype code and I'm just trying to test a few things and get this to work.

import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import astropy.visualization
from astropy.io import fits
from astropy.utils.data import download_file
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename


def main():

    #My own fits file
    #fitsImage = get_pkg_data_filename("C:\\20180807T000456.fits")

    fitsImage = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )

    hdu_list = fits.open(fitsImage)
    hdu_list.info()

    #norm = ImageNormalize(stretch=LogStretch())

    image_data = fits.getdata(fitsImage)
    print(type(image_data))
    print(image_data.shape)


    hdu_list.close()

    plt.figure()
    plt.imshow(image_data, cmap='gray', norm=LogNorm())
    plt.colorbar()

    # I chose the tick marks based on the histogram above
    cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])
    cbar.ax.set_yticklabels(['5,000','10,000','20,000'])

    time.sleep(10)

I am also unable to get the image to display with the plt.imshow()

Any insight would be helpful

Upvotes: 2

Views: 1986

Answers (1)

m13op22
m13op22

Reputation: 2337

You're so close! I ran your code in Python 2.7 and all you need to do is add

plt.show()

before time.sleep(10) (any reason you're including this?) and you get enter image description here

Also, I don't think you need to include the colorbar and yticklabels, plt.imshow automatically adds the colorbar with the lognorm scale (I commented that section out when I got the image).

Upvotes: 3

Related Questions