-1

how can I access the image subsample from a FITS file?

I could not tell where exactly the data is in the file! The file headers show that it's in the second header and that it is of dimension 1024*1024! But when I try to access the second header I don't get the image array, instead I get a "nonetype" file! I believe there is something wrong am doing here!

import matplotlib.pyplot as plt
from astropy.io import fits
import cv2
headerList=fits.open('AIA20100630_0000_0211.fits')
#Load table data as image data
#imgData = headerList[1].data 
imgData = headerList[0].data 

hdu=headerList[1]
print('shape :',hdu.shape) #shape is 1024*1024

#show image
plt.figure()
plt.imshow(imgData)
plt.show()
Iguananaut
  • 15,675
  • 4
  • 43
  • 50

2 Answers2

2

Even though you access the second HDU here:

hdu=headerList[1]

you don't display the data from that HDU, but you chose imgData from the first HDU:

imgData = headerList[0].data
... 
plt.imshow(imgData)

The fix would be simply to display hdu.data:

plt.imshow(hdu.data)
MSeifert
  • 118,681
  • 27
  • 271
  • 293
0

Instead of using fits.open(), you might want to look into using fits.getheader('img.fits') and fits.getdata('img.fits'). These methods are generally more convinient if all you need is the header or the image data.

In your case

from astropy.io import fits
imgData = fits.getdata('AIA20100630_0000_0211.fits', 1)

should load the image data directly without going through the hdu.

Zack Parsons
  • 773
  • 1
  • 7
  • 7
  • 1
    "These methods are faster than opening the entire fits file if all you need is the header or the image data." I mean, it's not "faster" time-wise, it's just a shortcut for `hdul = fits.open(filename); data = hdul[0].data` (or `hdul[1].data` if the first HDU is empty as common for some formats). This is fine for very basic use and often convenient, but one should still learn the full object-oriented API for anything more sophisticated. – Iguananaut Aug 08 '17 at 09:31