top bar

Using the Micro-Manager python library

Revision as of 17:07, 9 June 2014 by Radioxoma (Talk | contribs)

MMCorePy is a wrapper that allows you to control microscope hardware from python interactive session or scripts. It's support Windows, Mac and Linux.

Micromanager's main parts:

  • CMMCore - basic module, written in C++. Script languages like python just wrap it by swig.
  • Device adapters - various libraries that allow support for various hardware. If you want to built one and extend MM devise support, feel free to use SDK.
  • MMCorePy - python wrapper. MM build scripts are support both python 2 and 3, but windows version ships with python 2 bindings only.
  • MMCoreJ - java wrapper
  • MMStudio - Micromanager GUI (technically it is ImageJ plugin).

Environment setup

You must install python2 and numpy. Windows users may prefer using an python distribution instead manual separate installation.


  • python 2.7.x (python2 is default for windows now)
  • numpy 1.7.x Micormanager represent imaging data as multidimensional numpy arrays.

Using python distributions

It's convenient to install a distribution which includes Python, numpy, scientific libraries, GUI frameworks and IDEs. All distributions have a free version, some of them have extended paid version, but you can request free academic license.

Useful libraries

  • Scipy - scientific algorithms, multidimensional image processing toolbox.
  • Matplotlib - fastest way to show your image data.
  • Opencv - computer vision and image processing library. Sometimes faster than scipy.
  • Pillow - very basic image processing. Scipy uses it for image loading and writing.
  • Scikit-image - "pythonic" scientific-oriented image processing algorithms collection.
  • IPython - improved interactive python environment

Micromanager installation

Windows & Mac

Download and install Micro-Manager on your computer. Add Micromanager installation folder to PYTHONPATH (i.e. "C:\Program Files\Micro-Manager-1.4", it should contain and _MMCorePy.pyd files). Create variable if it not exist. At now you can import MMCorePy without an error.


MM package from your distribution repository, in most cases, ships with CMMCore, MMCorePy (python 2 or 3 wrapper) and MMCoreJ, but without GUI. At least your system variables should be already configured properly. Python and Numpy would be installed by your package manager as dependency.

Using Python API

Familiarize yourself with Micro-Manager and learn how to connect it to your hardware by MMStudio GUI.

Images returned by calls to an instance of the pythonized Micro-Manager core class (CMMCore) are stored in numpy arrays for convenience, and can be easily displayed by pylab (matplotlib) commands. Here is a short example (using matplotlib) that demonstrates use of the python wrapper to acquire and display an image.

First steps

Next code snippets aims to be most generic. We use numpy and matplotlib as is from pure python interactive shell, but it's convenient to use IPython with nice autocompletion capabilities.

Start python interactive session. Import `MMCorePy` and make sure everything is working properly.

   >>> import MMCorePy
   >>> mmc = MMCorePy.CMMCore()  # Instance micromanager core
   >>> mmc.getVersionInfo()
   'MMCore version 2.3.2'
   >>> mmc.getAPIVersionInfo()
   'Device API version 59, Module API version 10'

We just get some basic information about current Micromanager installation. If there an `ImportError`, check your PYTHONPATH variable.

Device loading

You can get all needed parameters names from Micromanager configuration file, generated by MMStudio.

Let's take step closer to hardware. Micromanager have couple of dummy devices, suitable for learning purposes. Load DemoCamera:

   # Demo camera example, continuation of previous listing
   >>> mmc.loadDevice('Camera', 'DemoCamera', 'DCam')
   >>> mmc.initializeAllDevices()
   >>> mmc.setCameraDevice('Camera')

Snapping single image


   >>> mmc.snapImage()
   >>> img = mmc.getImage()  # img - it's just numpy array
   >>> img
   array([[12, 12, 13, ..., 11, 12, 12],
          [12, 12, 13, ..., 11, 12, 12],
          [12, 13, 13, ..., 12, 12, 12],
          [22, 22, 22, ..., 22, 22, 22],
          [22, 22, 22, ..., 22, 22, 22],
          [22, 22, 22, ..., 22, 22, 22]], dtype=uint8)

By default it's grayscale 8-bit image, presented as two-dimensional numpy array. Let's see him with matplotlib.

   >>> import matplotlib.pyplot as plt
   >>> plt.imshow(img, cmap='gray')
   >>>  # And window will appear


Of course, color image is more suitable for optical microscopy purposes. So take one:

   >>> mmc.setProperty('Camera', 'PixelType', '32bitRGB')  # Change pixel type
   >>> rgb32 = mmc.getImage()
   >>> rgb32
   array([[1250067, 1250067, 1315860, ..., 1250067, 1250067, 1250067],
          [1250067, 1315603, 1315860, ..., 1250067, 1250067, 1250067],
          [1250067, 1315859, 1315860, ..., 1250067, 1250067, 1250067],
          [1246483, 1246483, 1246483, ..., 1181204, 1246740, 1246484],
          [1246483, 1246483, 1246483, ..., 1246740, 1246740, 1246483],
          [1246483, 1246483, 1312019, ..., 1246740, 1246740, 1246483]], dtype=uint32)

Interesting output isn't it? We expect something like 3-dimensional RGB array, but get bunch of uints in 2-D shape.

Numpy array

Now we should look at RGB32 data structure.

   low memory address    ---->      high memory address
   | pixel | pixel | pixel | pixel | pixel | pixel |...

Let's numpy handle that.

   >>> import numpy as np
   >>> rgb32.view(dtype=np.uint8).reshape(
           rgb32.shape[0], rgb32.shape[1], 4)[...,2::-1]

View RGB32 as RGB array (no copy).

Continuous acquisition

Don't run this code directly. It's a partial sample.
   while True:
       rgb32 = mmc.getLastImage()
       if mmc.getRemainingImageCount() > 0:
           rgb32 = mmc.getLastImage()
           # or rgb32 = mmc.popNextImage()
           # Efficient conversion without data copying.
           bgr = rgb32.view(dtype=np.uint8).reshape(
               rgb32.shape[0], rgb32.shape[1], 4)[..., :3]

A longer example script,, is available in the Micro-Manager root directory.

Also check out micromanager-samples repo for python code samples (live video acquisition, property discovery etc).

Further reading

Micromanager code

Written by Eugene Dvoretsky -- Radioxoma (talk) 17:07, 9 June 2014 (PDT)

© Micro-Manager : Vale Lab, UCSF 2006-2011 | All Rights Reserved | Contact