{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n=====================================================\nConvert a 3-color image (JPG) to separate FITS images\n=====================================================\n\nThis example opens an RGB JPEG image and writes out each channel as a separate\nFITS (image) file.\n\nThis example uses `pillow `_ to read the image,\n`matplotlib.pyplot` to display the image, and `astropy.io.fits` to save FITS files.\n\n-------------------\n\n*By: Erik Bray, Adrian Price-Whelan*\n\n*License: BSD*\n\n-------------------\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom PIL import Image\nfrom astropy.io import fits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set up matplotlib and use a nicer set of plot parameters\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nfrom astropy.visualization import astropy_mpl_style\nplt.style.use(astropy_mpl_style)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load and display the original 3-color jpeg image:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "image = Image.open('Hs-2009-14-a-web.jpg')\nxsize, ysize = image.size\nprint(\"Image size: {} x {}\".format(xsize, ysize))\nplt.imshow(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Split the three channels (RGB) and get the data as Numpy arrays. The arrays\nare flattened, so they are 1-dimensional:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r, g, b = image.split()\nr_data = np.array(r.getdata()) # data is now an array of length ysize*xsize\ng_data = np.array(g.getdata())\nb_data = np.array(b.getdata())\nprint(r_data.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reshape the image arrays to be 2-dimensional:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r_data = r_data.reshape(ysize, xsize)\ng_data = g_data.reshape(ysize, xsize)\nb_data = b_data.reshape(ysize, xsize)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write out the channels as separate FITS images\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "red = fits.PrimaryHDU(data=r_data)\nred.header['LATOBS'] = \"32:11:56\" # add spurious header info\nred.header['LONGOBS'] = \"110:56\"\nred.writeto('red.fits')\n\ngreen = fits.PrimaryHDU(data=g_data)\ngreen.header['LATOBS'] = \"32:11:56\"\ngreen.header['LONGOBS'] = \"110:56\"\ngreen.writeto('green.fits')\n\nblue = fits.PrimaryHDU(data=b_data)\nblue.header['LATOBS'] = \"32:11:56\"\nblue.header['LONGOBS'] = \"110:56\"\nblue.writeto('blue.fits')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Delete the files created\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nos.remove('red.fits')\nos.remove('green.fits')\nos.remove('blue.fits')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 0 }