
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "generated/examples/io/split-jpeg-to-fits.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_generated_examples_io_split-jpeg-to-fits.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_generated_examples_io_split-jpeg-to-fits.py:


=====================================================
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 <https://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*

.. GENERATED FROM PYTHON SOURCE LINES 20-25

.. code-block:: default


    import numpy as np
    from PIL import Image
    from astropy.io import fits








.. GENERATED FROM PYTHON SOURCE LINES 26-27

Set up matplotlib and use a nicer set of plot parameters

.. GENERATED FROM PYTHON SOURCE LINES 27-32

.. code-block:: default


    import matplotlib.pyplot as plt
    from astropy.visualization import astropy_mpl_style
    plt.style.use(astropy_mpl_style)








.. GENERATED FROM PYTHON SOURCE LINES 33-34

Load and display the original 3-color jpeg image:

.. GENERATED FROM PYTHON SOURCE LINES 34-41

.. code-block:: default


    image = Image.open('Hs-2009-14-a-web.jpg')
    xsize, ysize = image.size
    print(f"Image size: {ysize} x {xsize}")
    print(f"Image bands: {image.getbands()}")
    ax = plt.imshow(image)




.. image:: /generated/examples/io/images/sphx_glr_split-jpeg-to-fits_001.png
    :alt: split jpeg to fits
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Image size: 232 x 400
    Image bands: ('R', 'G', 'B')




.. GENERATED FROM PYTHON SOURCE LINES 42-44

Split the three channels (RGB) and get the data as Numpy arrays. The arrays
are flattened, so they are 1-dimensional:

.. GENERATED FROM PYTHON SOURCE LINES 44-51

.. code-block:: default


    r, g, b = image.split()
    r_data = np.array(r.getdata()) # data is now an array of length ysize*xsize
    g_data = np.array(g.getdata())
    b_data = np.array(b.getdata())
    print(r_data.shape)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    (92800,)




.. GENERATED FROM PYTHON SOURCE LINES 52-53

Reshape the image arrays to be 2-dimensional:

.. GENERATED FROM PYTHON SOURCE LINES 53-59

.. code-block:: default


    r_data = r_data.reshape(ysize, xsize) # data is now a matrix (ysize, xsize)
    g_data = g_data.reshape(ysize, xsize)
    b_data = b_data.reshape(ysize, xsize)
    print(r_data.shape)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    (232, 400)




.. GENERATED FROM PYTHON SOURCE LINES 60-62

Write out the channels as separate FITS images.
Add and visualize header info

.. GENERATED FROM PYTHON SOURCE LINES 62-81

.. code-block:: default


    red = fits.PrimaryHDU(data=r_data)
    red.header['LATOBS'] = "32:11:56" # add spurious header info
    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')

    from pprint import pprint
    pprint(red.header)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    SIMPLE  =                    T / conforms to FITS standard                      
    BITPIX  =                   64 / array data type                                
    NAXIS   =                    2 / number of array dimensions                     
    NAXIS1  =                  400                                                  
    NAXIS2  =                  232                                                  
    EXTEND  =                    T                                                  
    LATOBS  = '32:11:56'                                                            
    LONGOBS = '110:56  '                                                            




.. GENERATED FROM PYTHON SOURCE LINES 82-83

Delete the files created

.. GENERATED FROM PYTHON SOURCE LINES 83-87

.. code-block:: default

    import os
    os.remove('red.fits')
    os.remove('green.fits')
    os.remove('blue.fits')








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.157 seconds)


.. _sphx_glr_download_generated_examples_io_split-jpeg-to-fits.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: split-jpeg-to-fits.py <split-jpeg-to-fits.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: split-jpeg-to-fits.ipynb <split-jpeg-to-fits.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
