Downloading Images

The ImageDownloadApi class in the AllenSDK provides a Python interface for accessing image data produced by the Allen Institute for Brain Science.


Reference Documentation:

Download this file in .ipynb format here.

In [1]:
from allensdk.api.queries.image_download_api import ImageDownloadApi
from allensdk.config.manifest import Manifest

import matplotlib.pyplot as plt
from import imread
import pandas as pd

import logging
import os

%matplotlib inline

We will want to look at the images we download in this notebook. Here is a simple function which does that:

In [2]:
def verify_image(file_path, figsize=(18, 22)):
    image = imread(file_path)

    fig, ax = plt.subplots(figsize=figsize)

Finally, we will need an instance of ImageDownloadApi

In [3]:
image_api = ImageDownloadApi()

Downloading a single Mouse Brain Section image

In this example, we will download a single section image from Mouse Brain Atlas experiment 71210895.

For now, we'll assume that we already know the id (unique integer identifier) of the image that we wish to download. In a later section, we'll use our ImageDownloadApi instance to find the ids of all the images from a particular experiment.

In [4]:
section_image_id = 70945123
file_path = '70945123.jpg'

The raw images can be quite large, so we will download downsampled versions. The downsample keyword argument reduces the size of the obtained image by a factor of $2^{\text{downsample}}$ along each axis.

In [5]:
downsample = 3

Now we download the image using our api object and view it in the notebook.

In [6]:
image_api.download_section_image(section_image_id, file_path, downsample=downsample)

2018-01-04 13:22:36,613 allensdk.api.api.retrieve_file_over_http INFO     Downloading URL:

Downloading a single Mouse Connectivity image

In this example, we will download a single section image from Mouse Brain Connectivity experiment 297225422.

In [7]:
section_image_id = 297225716
file_path = '297225716_connectivity.jpg'
downsample = 3

In order to clearly distinguish projection signal from background structure, it helps to window and rescale each channel. We have precomputed channelwise windows for each section data set in the connectivity atlas. You can obtain these window parameters using the ImageDownloadApi

In [8]:
ranges = image_api.get_section_image_ranges([section_image_id])[0]

Then simply pass them in via the range keyword argument.

In [9]:
image_api.download_projection_image(section_image_id, file_path, downsample=downsample, range=ranges)

2018-01-04 13:22:38,346 allensdk.api.api.retrieve_file_over_http INFO     Downloading URL:,825,0,943,0,4095