MUFC_Rules
MUFC_Rules

Reputation: 35

Uploading a fits image with an odd shape

I'm trying to upload a fits image but it keeps showing the same error "Invalid dimensions for image data".

I found out that it's because the fits image has an odd shape (1, 40, 40). Is there a way to get around this and upload the actual image without using AplPy?

file1 = "Downloads/PVDiagramtest1.fits"
image_data = fits.getdata(file1)
print(image_data)
print(image_data.shape)

plt.figure()
plt.imshow(image_data)
plt.show()

Upvotes: 1

Views: 131

Answers (1)

Michael
Michael

Reputation: 2414

The image cube image_data is simply an numpy array, so you just need to access a slice of it to get a 2D shape. For instance, this will plot the 40x40 image:

plt.imshow(image_data[0,:,:])

Often images stored in FITS or HDF5 (or other formats) may come in a 3D shape even if there's only one image stored. This allows software to be written more generally when accessing this kind of data; it's easier to always be dealing with a 3D shape than to have to write code that separately handles the occasional 2D case.

edit: Reading your question a little more carefully, it looks like you may be using some API that wants the image data to be 2D? The same advice generally applies; slice or reshape the array and then save it that way.

Upvotes: 4

Related Questions