Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Medical image processing in Python

Project Description

MedPy is a library and script collection for medical image processing in Python, providing basic functionalities for reading, writing and manipulating large images of arbitrary dimensionality. Its main contributions are n-dimensional versions of popular image filters, a collection of image feature extractors, ready to be used with scikit-learn, and an exhaustive n-dimensional graph-cut package.

Troubles? Feel free to write me with any questions / comments / suggestions:

Found a bug?

Too many depoendencies? Try our docker images (release) and (development)

Installing MedPy the fast way (Ubuntu and derivatives)


sudo apt-get install python-pip python-numpy python-scipy libboost-python-dev build-essential


sudo pip install nibabel pydicom medpy

Done. More installation instructions can be found in the documentation.

Using Python 3?


sudo pip install nibabel pydicom
sudo pip install

Getting started with the library

If you already have one, whose format is support (see in the documentation.), then good. Otherwise navigate to, click on the Download Now button, unpack and look for the inia19-t1.nii file. Open it in your favorite medical image viewer (I personally fancy itksnap) and beware a the INIA19 primate brain atlas.

Load the image

>>> from import load
>>> image_data, image_header = load('/path/to/')

The data is stored in a numpy ndarray, the header is an object containing additional metadata, such as the voxel-spacing. No lets take a look at some of the image metadata

>>> image_data.shape
(168, 206, 128)
>>> image_data.dtype

And the header gives us

>>> from import header
>>> header.get_pixel_spacing(image_header)
(0.5, 0.5, 0.5)
>>> header.get_offset(image_header)
(0.0, 0.0, 0.0)

Now lets apply one of the MedPy filter, more exactly the Otsu thresholding, which can be used for automatic background removal

>>> from medpy.filter import otsu
>>> threshold = otsu(image_data)
>>> output_data = image_data > threshold

And save the binary image, marking the foreground

>>> from import save
>>> save(output_data, '/path/to/', image_header)

After taking a look at it, you might want to dive deeper with the documentation.

Getting started with the scripts

Get an image as described above. Now: /path/to/

will give you some details about the image. With: /path/to/ /path/to/

you can compare two image. And: /path/to/ /path/to/

lets you apply an edge preserving anisotropic diffusion filter. For a list of all scripts, see the documentation.

Read/write support for medical image formats

MedPy builds on 3rd party modules to load and save images. Currently implemented are the usages of

  • NiBabel
  • PyDicom
  • ITK

, each of which supports the following formats.

NiBabel enables support for:

  • NifTi - Neuroimaging Informatics Technology Initiative (.nii, nii.gz)
  • Analyze (plain, SPM99, SPM2) (.hdr/.img, .img.gz)
  • and some others more (

PyDicom enables support for:

  • Dicom - Digital Imaging and Communications in Medicine (.dcm, .dicom)

ITK enables support for:

  • NifTi - Neuroimaging Informatics Technology Initiative (.nii, nii.gz)
  • Analyze (plain, SPM99, SPM2) (.hdr/.img, .img.gz)
  • Dicom - Digital Imaging and Communications in Medicine (.dcm, .dicom)
  • Itk/Vtk MetaImage (.mhd, .mha/.raw)
  • Nrrd - Nearly Raw Raster Data (.nhdr, .nrrd)
  • and many others more (

For some functionalities, which are collected in the medpy.itkvtk package ITK is also required.


You can find our sources and single-click downloads:

Tutorials and API Documentation


MedPy comes with a number of dependencies and optional functionality that can require you to install additional packages.


  • scipy >= 0.9.0
  • numpy >= 1.6.1
  • nibabel >= 1.3.0 (enables support for NIfTI and Analyze image formats)
  • pydicom >= 0.9.7 (enables support for DICOM image format)

Optional functionalities

  • compilation with max-flow/min-cut (enables the GraphCut functionalities)
  • itk >= 3.16.0 with WrapITK (enables support for a large number of image formats)


MedPy is distributed under the GNU General Public License, a version of which can be found in the LICENSE.txt file.

Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
MedPy-0.3.0.tar.gz (163.5 kB) Copy SHA256 Checksum SHA256 Source Sep 20, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting