Downloading Images

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

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

import logging
import os
from base64 import b64encode

from IPython.display import HTML, display
%matplotlib inline

We will want to look at the images we download in this notebook. Here are a couple of functions that do this:

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

    fig, ax = plt.subplots(figsize=figsize)
    ax.imshow(image)
    
    
def verify_svg(file_path, width_scale, height_scale):
    # we're using this function to display scaled svg in the rendered notebook.
    # we suggest that in your own work you use a tool such as inkscape or illustrator to view svg
    
    with open(file_path, 'rb') as svg_file:
        svg = svg_file.read()
    encoded_svg = b64encode(svg)
    decoded_svg = encoded_svg.decode('ascii')
    
    st = r'<img class="figure" src="data:image/svg+xml;base64,{}" width={}% height={}%></img>'.format(decoded_svg, width_scale, height_scale)
    display(HTML(st))

Finally, we will need an instance of ImageDownloadApi and an instance of SvgApi:

In [3]:
image_api = ImageDownloadApi()
svg_api = SvgApi()

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)

verify_image(file_path)
2018-07-27 13:51:39,103 allensdk.api.api.retrieve_file_over_http INFO     Downloading URL: http://api.brain-map.org/api/v2/section_image_download/70945123?downsample=3

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)

verify_image(file_path)
2018-07-27 13:51:42,496 allensdk.api.api.retrieve_file_over_http INFO     Downloading URL: http://api.brain-map.org/api/v2/projection_image_download/297225716?downsample=3&range=0,825,0,943,0,4095