
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorials/images.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

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

        :ref:`Go to the end <sphx_glr_download_tutorials_images.py>`
        to download the full example code.

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

.. _sphx_glr_tutorials_images.py:


.. redirect-from:: /tutorials/introductory/images

.. _image_tutorial:

==============
Image tutorial
==============

A short tutorial on plotting images with Matplotlib.

.. _imaging_startup:

Startup commands
===================

First, let's start IPython.  It is a most excellent enhancement to the
standard Python prompt, and it ties in especially well with
Matplotlib.  Start IPython either directly at a shell, or with the Jupyter
Notebook (where IPython as a running kernel).

With IPython started, we now need to connect to a GUI event loop.  This
tells IPython where (and how) to display plots.  To connect to a GUI
loop, execute the **%matplotlib** magic at your IPython prompt.  There's more
detail on exactly what this does at `IPython's documentation on GUI
event loops
<https://ipython.readthedocs.io/en/stable/interactive/reference.html#gui-event-loop-support>`_.

If you're using Jupyter Notebook, the same commands are available, but
people commonly use a specific argument to the %matplotlib magic:

.. sourcecode:: ipython

    In [1]: %matplotlib inline

This turns on inline plotting, where plot graphics will appear in your
notebook.  This has important implications for interactivity.  For inline plotting, commands in
cells below the cell that outputs a plot will not affect the plot.  For example,
changing the colormap is not possible from cells below the cell that creates a plot.
However, for other backends, such as Qt, that open a separate window,
cells below those that create the plot will change the plot - it is a
live object in memory.

This tutorial will use Matplotlib's implicit plotting interface, pyplot.  This
interface maintains global state, and is very useful for quickly and easily
experimenting with various plot settings.  The alternative is the explicit,
which is more suitable for large application development.  For an explanation
of the tradeoffs between the implicit and explicit interfaces see
:ref:`api_interfaces` and the :ref:`Quick start guide
<quick_start>` to start using the explicit interface.
For now, let's get on with the implicit approach:

.. GENERATED FROM PYTHON SOURCE LINES 54-60

.. code-block:: Python


    from PIL import Image

    import matplotlib.pyplot as plt
    import numpy as np








.. GENERATED FROM PYTHON SOURCE LINES 61-84

.. _importing_data:

Importing image data into Numpy arrays
======================================

Matplotlib relies on the Pillow_ library to load image data.

.. _Pillow: https://pillow.readthedocs.io/en/latest/

Here's the image we're going to play with:

.. image:: ../_static/stinkbug.png

It's a 24-bit RGB PNG image (8 bits for each of R, G, B).  Depending
on where you get your data, the other kinds of image that you'll most
likely encounter are RGBA images, which allow for transparency, or
single-channel grayscale (luminosity) images.  Download `stinkbug.png
<https://raw.githubusercontent.com/matplotlib/matplotlib/main/doc/_static/stinkbug.png>`_
to your computer for the rest of this tutorial.

We use Pillow to open an image (with `PIL.Image.open`), and immediately
convert the `PIL.Image.Image` object into an 8-bit (``dtype=uint8``) numpy
array.

.. GENERATED FROM PYTHON SOURCE LINES 84-88

.. code-block:: Python


    img = np.asarray(Image.open('../../doc/_static/stinkbug.png'))
    print(repr(img))





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

 .. code-block:: none

    array([[[104, 104, 104],
            [104, 104, 104],
            [104, 104, 104],
            ...,
            [109, 109, 109],
            [109, 109, 109],
            [109, 109, 109]],

           [[105, 105, 105],
            [105, 105, 105],
            [105, 105, 105],
            ...,
            [109, 109, 109],
            [109, 109, 109],
            [109, 109, 109]],

           [[107, 107, 107],
            [106, 106, 106],
            [106, 106, 106],
            ...,
            [110, 110, 110],
            [110, 110, 110],
            [110, 110, 110]],

           ...,

           [[112, 112, 112],
            [111, 111, 111],
            [110, 110, 110],
            ...,
            [116, 116, 116],
            [115, 115, 115],
            [115, 115, 115]],

           [[113, 113, 113],
            [113, 113, 113],
            [112, 112, 112],
            ...,
            [115, 115, 115],
            [114, 114, 114],
            [114, 114, 114]],

           [[113, 113, 113],
            [115, 115, 115],
            [115, 115, 115],
            ...,
            [114, 114, 114],
            [114, 114, 114],
            [113, 113, 113]]], shape=(375, 500, 3), dtype=uint8)




.. GENERATED FROM PYTHON SOURCE LINES 89-108

Each inner list represents a pixel.  Here, with an RGB image, there
are 3 values.  Since it's a black and white image, R, G, and B are all
similar.  An RGBA (where A is alpha, or transparency) has 4 values
per inner list, and a simple luminance image just has one value (and
is thus only a 2-D array, not a 3-D array).  For RGB and RGBA images,
Matplotlib supports float32 and uint8 data types.  For grayscale,
Matplotlib supports only float32.  If your array data does not meet
one of these descriptions, you need to rescale it.

.. _plotting_data:

Plotting numpy arrays as images
===================================

So, you have your data in a numpy array (either by importing it, or by
generating it).  Let's render it.  In Matplotlib, this is performed
using the :func:`~matplotlib.pyplot.imshow` function.  Here we'll grab
the plot object.  This object gives you an easy way to manipulate the
plot from the prompt.

.. GENERATED FROM PYTHON SOURCE LINES 108-111

.. code-block:: Python


    imgplot = plt.imshow(img)




.. image-sg:: /tutorials/images/sphx_glr_images_001.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_001.png, /tutorials/images/sphx_glr_images_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 112-130

You can also plot any numpy array.

.. _Pseudocolor:

Applying pseudocolor schemes to image plots
-------------------------------------------------

Pseudocolor can be a useful tool for enhancing contrast and
visualizing your data more easily.  This is especially useful when
making presentations of your data using projectors - their contrast is
typically quite poor.

Pseudocolor is only relevant to single-channel, grayscale, luminosity
images.  We currently have an RGB image.  Since R, G, and B are all
similar (see for yourself above or in your data), we can just pick one
channel of our data using array slicing (you can read more in the
`Numpy tutorial <https://numpy.org/doc/stable/user/quickstart.html
#indexing-slicing-and-iterating>`_):

.. GENERATED FROM PYTHON SOURCE LINES 130-134

.. code-block:: Python


    lum_img = img[:, :, 0]
    plt.imshow(lum_img)




.. image-sg:: /tutorials/images/sphx_glr_images_002.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_002.png, /tutorials/images/sphx_glr_images_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 135-138

Now, with a luminosity (2D, no color) image, the default colormap (aka lookup table,
LUT), is applied.  The default is called viridis.  There are plenty of
others to choose from.

.. GENERATED FROM PYTHON SOURCE LINES 138-141

.. code-block:: Python


    plt.imshow(lum_img, cmap="hot")




.. image-sg:: /tutorials/images/sphx_glr_images_003.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_003.png, /tutorials/images/sphx_glr_images_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 142-144

Note that you can also change colormaps on existing plot objects using the
:meth:`~matplotlib.cm.ScalarMappable.set_cmap` method:

.. GENERATED FROM PYTHON SOURCE LINES 144-148

.. code-block:: Python


    imgplot = plt.imshow(lum_img)
    imgplot.set_cmap('nipy_spectral')




.. image-sg:: /tutorials/images/sphx_glr_images_004.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_004.png, /tutorials/images/sphx_glr_images_004_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 149-168

.. note::

   However, remember that in the Jupyter Notebook with the inline backend,
   you can't make changes to plots that have already been rendered.  If you
   create imgplot here in one cell, you cannot call set_cmap() on it in a later
   cell and expect the earlier plot to change.  Make sure that you enter these
   commands together in one cell.  plt commands will not change plots from earlier
   cells.

There are many other colormap schemes available.  See the :ref:`list and images
of the colormaps<colormaps>`.

.. _`Color Bars`:

Color scale reference
------------------------

It's helpful to have an idea of what value a color represents.  We can
do that by adding a color bar to your figure:

.. GENERATED FROM PYTHON SOURCE LINES 169-173

.. code-block:: Python


    imgplot = plt.imshow(lum_img)
    plt.colorbar()




.. image-sg:: /tutorials/images/sphx_glr_images_005.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_005.png, /tutorials/images/sphx_glr_images_005_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 174-184

.. _`Data ranges`:

Examining a specific data range
---------------------------------

Sometimes you want to enhance the contrast in your image, or expand
the contrast in a particular region while sacrificing the detail in
colors that don't vary much, or don't matter.  A good tool to find
interesting regions is the histogram.  To create a histogram of our
image data, we use the :func:`~matplotlib.pyplot.hist` function.

.. GENERATED FROM PYTHON SOURCE LINES 184-187

.. code-block:: Python


    plt.hist(lum_img.ravel(), bins=range(256), fc='k', ec='k')




.. image-sg:: /tutorials/images/sphx_glr_images_006.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_006.png, /tutorials/images/sphx_glr_images_006_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 188-198

Most often, the "interesting" part of the image is around the peak,
and you can get extra contrast by clipping the regions above and/or
below the peak.  In our histogram, it looks like there's not much
useful information in the high end (not many white things in the
image).  Let's adjust the upper limit, so that we effectively "zoom in
on" part of the histogram.  We do this by setting *clim*, the colormap
limits.

This can be done by passing a *clim* keyword argument in the call to
``imshow``.

.. GENERATED FROM PYTHON SOURCE LINES 198-201

.. code-block:: Python


    plt.imshow(lum_img, clim=(0, 175))




.. image-sg:: /tutorials/images/sphx_glr_images_007.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_007.png, /tutorials/images/sphx_glr_images_007_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 202-207

This can also be done by calling the
:meth:`~matplotlib.cm.ScalarMappable.set_clim` method of the returned image
plot object, but make sure that you do so in the same cell as your plot
command when working with the Jupyter Notebook - it will not change
plots from earlier cells.

.. GENERATED FROM PYTHON SOURCE LINES 207-211

.. code-block:: Python


    imgplot = plt.imshow(lum_img)
    imgplot.set_clim(0, 175)




.. image-sg:: /tutorials/images/sphx_glr_images_008.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_008.png, /tutorials/images/sphx_glr_images_008_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 212-232

.. _Interpolation:

Array Interpolation schemes
---------------------------

Interpolation calculates what the color or value of a pixel "should"
be, according to different mathematical schemes.  One common place
that this happens is when you resize an image.  The number of pixels
change, but you want the same information.  Since pixels are discrete,
there's missing space.  Interpolation is how you fill that space.
This is why your images sometimes come out looking pixelated when you
blow them up.  The effect is more pronounced when the difference
between the original image and the expanded image is greater.  Let's
take our image and shrink it.  We're effectively discarding pixels,
only keeping a select few.  Now when we plot it, that data gets blown
up to the size on your screen.  The old pixels aren't there anymore,
and the computer has to draw in pixels to fill that space.

We'll use the Pillow library that we used to load the image also to resize
the image.

.. GENERATED FROM PYTHON SOURCE LINES 232-237

.. code-block:: Python


    img = Image.open('../../doc/_static/stinkbug.png')
    img.thumbnail((64, 64))  # resizes image in-place
    imgplot = plt.imshow(img)




.. image-sg:: /tutorials/images/sphx_glr_images_009.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_009.png, /tutorials/images/sphx_glr_images_009_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 238-242

Here we use the default interpolation ("nearest"), since we did not
give :func:`~matplotlib.pyplot.imshow` any interpolation argument.

Let's try some others. Here's "bilinear":

.. GENERATED FROM PYTHON SOURCE LINES 242-245

.. code-block:: Python


    imgplot = plt.imshow(img, interpolation="bilinear")




.. image-sg:: /tutorials/images/sphx_glr_images_010.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_010.png, /tutorials/images/sphx_glr_images_010_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 246-247

and bicubic:

.. GENERATED FROM PYTHON SOURCE LINES 247-250

.. code-block:: Python


    imgplot = plt.imshow(img, interpolation="bicubic")




.. image-sg:: /tutorials/images/sphx_glr_images_011.png
   :alt: images
   :srcset: /tutorials/images/sphx_glr_images_011.png, /tutorials/images/sphx_glr_images_011_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 251-253

Bicubic interpolation is often used when blowing up photos - people
tend to prefer blurry over pixelated.


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

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


.. _sphx_glr_download_tutorials_images.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: images.ipynb <images.ipynb>`

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

      :download:`Download Python source code: images.py <images.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: images.zip <images.zip>`


.. only:: html

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

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