How to save jpegs of FITS files?

I have a program that follows closely the link above and I want to saves the FITS matplotlib image as a jpeg file so when I work with my real FITS files later on, I can save them as jpegs.

I tried to save it the usual way:


However, after doing this, I find that the output does not look anything like the image
[![It is a completely blank image][1]][1]

Any suggestions would be greatly appreciated.

Probably is clearing the image. Try changing the order to call savefig first.

I tried both solutions in the checked answer in the link you posted, but no luck.

import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style

from import get_pkg_data_filename
from import fits

image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')

image_data = fits.getdata(image_file, ext=0)


plt.savefig("output.jpg") #save as jpg
plt.imshow(image_data, cmap='gray')

plt.imshow(image_data, cmap='gray')
plt.savefig("output.jpg") #save as jpg

This order (without plt.figure()) works here. What are you using to run it? If it’s something like Jupyter, maybe it does implicit and clears it?


I used Jupyter Notebook to run it. Excluding plt.figure() resolved the issue.

Do you know how to make the output file larger and the axes more readable? I tried

figure = plt.gcf()

figure.set_size_inches(20, 15)

plt.imshow(image_data, cmap='gray')
plt.savefig("output.jpg", dpi=300) #save as jpg

But that seems to make the axes labels so small

1 Like