Introduction to Image Handling with Python

Course Description

  • Who is this course for?

The course is designed for users who are new to image processing with Python, but who already have electron microscopy (EM) data, or are happy to use dummy data available at We suggest these users go through each of the three introductory workbooks in order. They cover the basics of image analysis using Python.

For those new to Python entirely, we do not give explicit instructions on general Python features and functionalities (there are plenty of resources online!) but we hope that the user will gain some understanding from the pre-written code and its output.

For those new to image analysis or EM, we assume some level of understanding of how electron microscopy images are acquired, though some discussion is given to general ideas and methodology.

Users familiar with either Python or image processing using other software may find the workbooks too basic and should skip to the specific applications.

  • What is the aim of this course?

The aim of this course is to provide an introduction to image processing using Python, an increasingly popular programming language amongst electron microscopists due to it being powerful, versatile, and open source.

The course consists of a series of introductory workbooks, which can help build a basic understanding. This will enable the user to understand and use the more complex scripts found in the library.

We have endeavored to use real in situ microscopy data rather than relying on simulations or ‘perfect data’. The datasets provided are not perfect, but are real examples of in situ TEM and STEM that are available as examples and for anyone to practice their coding skills on.

  • How to run the course workbooks?

All of the scripts in the course are written as Jupyter Notebooks. These are easy to read, and allow text comments in between coding windows, which is ideal for teaching purposes. The workbooks can be run either online via google colab, or they can be downloaded and run on your local pc.

Using Google Colab

The workbooks are linked to google colab, and you can therefore access and run the code yourself, by clicking the colab symbol at the top of each workbook (some browsers require ctrl click or shift click). This will redirect you to the google colab environment, where you have to log in with a valid google account. The colab environment has an active kernel so you can alter and execute the workbooks, which gives a better feel for the role of each line of code and an excellent hands on experience. You can always reset the code, by reloading it from the LiveNano website, or save your own version of an altered workbook to your google account.

As the kernel is online, it does not require a local version of python, but some of the packages used in the scripts, has to be installed each time the code is reopened in colab. This is accounted for with the top few lines of code in the workbooks.

However, google colab lacks some of the capabilities of a local python installation, and some features are therefore not available. These include the interactive plots in hyperspy, which are particularly useful. To enable these functionalities, install python and the necessary packages on your local drive, by following the guide below and download the workbooks from links provided above each of the workbooks.

Using a local Python installation

The workbooks can also be downloaded to your local drive, using the links above each of the workbooks. This requires a local python installation on your own machine. We recommend the anaconda distribution, which is available for download here: The download options can be foudn by scrolling to the bottom of the anaconda package.

Some of the scripts will require python libraries or packages, which are not included in the basic python installation e.g. openCV and HyperSpy. These packages can be installed by writing the following commands in the anaconda prompt (available once the anaconda distribution has been installed).

conda install hyperspy -c conda-forge

conda install -c conda-forge opencv

Course Structure

Workbook 1:

    • Introduction to numpy and working with electron microscopy data as numpy arrays

    • Datatypes (uint8, float16, etc.)

    • Understanding multidimensional images from singlular entries (pixels), 1D arrays (linescans), 2D arrays (images) and 3D arrays (image stacks)

    • Presenting image data in histogram format

    • Basic numpy operations: indexing, averaging/summing along specific axes

        • Averaging along rows of atoms to condense 2D array to 1D

        • Averaging along time axis to condense 3D array to 2D

    • Plotting features: display values (vmin, vmax), colourbars, annotations

    • Saving data and figures

Workbook 2:

    • Loading microscopy data in tif format

    • Reading exif metadata

    • Loading microscopy data in native format with hyperspy

    • Using hyperspy axes manager and video plotting

    • Reading metadata with hyperspy

    • Saving videos using moviepy

    • Example: dose calculations and plotting cumulative dose rate with time

Workbook 3:

    • Basic image filters: Gaussian, Median, Wiener (kernel size and how filtering changes data within arrays)

    • Thresholding: manual and automatic (mean, isodata, adaptive)

    • Background subtraction: smoothing, rolling structure

    • Fourier analysis: FFT, inverse FFT, masking FFT for Fourier filter

    • Emphasis on how to apply these across image stacks