| |
| """ |
| ===================================================== |
| Convert a 3-color image (JPG) to separate FITS images |
| ===================================================== |
| |
| This example opens an RGB JPEG image and writes out each channel as a separate |
| FITS (image) file. |
| |
| This example uses `pillow <http://python-pillow.org>`_ to read the image, |
| `matplotlib.pyplot` to display the image, and `astropy.io.fits` to save FITS files. |
| |
| ------------------- |
| |
| *By: Erik Bray, Adrian Price-Whelan* |
| |
| *License: BSD* |
| |
| ------------------- |
| |
| """ |
|
|
| import numpy as np |
| from PIL import Image |
| from astropy.io import fits |
|
|
| |
| |
|
|
| import matplotlib.pyplot as plt |
| from astropy.visualization import astropy_mpl_style |
| plt.style.use(astropy_mpl_style) |
|
|
| |
| |
|
|
| image = Image.open('Hs-2009-14-a-web.jpg') |
| xsize, ysize = image.size |
| print("Image size: {} x {}".format(xsize, ysize)) |
| plt.imshow(image) |
|
|
| |
| |
| |
|
|
| r, g, b = image.split() |
| r_data = np.array(r.getdata()) |
| g_data = np.array(g.getdata()) |
| b_data = np.array(b.getdata()) |
| print(r_data.shape) |
|
|
| |
| |
|
|
| r_data = r_data.reshape(ysize, xsize) |
| g_data = g_data.reshape(ysize, xsize) |
| b_data = b_data.reshape(ysize, xsize) |
|
|
| |
| |
|
|
| red = fits.PrimaryHDU(data=r_data) |
| red.header['LATOBS'] = "32:11:56" |
| red.header['LONGOBS'] = "110:56" |
| red.writeto('red.fits') |
|
|
| green = fits.PrimaryHDU(data=g_data) |
| green.header['LATOBS'] = "32:11:56" |
| green.header['LONGOBS'] = "110:56" |
| green.writeto('green.fits') |
|
|
| blue = fits.PrimaryHDU(data=b_data) |
| blue.header['LATOBS'] = "32:11:56" |
| blue.header['LONGOBS'] = "110:56" |
| blue.writeto('blue.fits') |
|
|
| |
| |
| import os |
| os.remove('red.fits') |
| os.remove('green.fits') |
| os.remove('blue.fits') |
|
|