Downloading full resolution images using AllenSDK

Hello All,

This is my first post to the forum, so apologies if I am posting in the wrong category or with the wrong tags.

I am trying to download full-scale nissl stain images of the human brain atlas (Experiment Detail :: BrainSpan: Atlas of the Developing Human Brain)

I’ve downloaded the allenSDK, and I am using code that I found here: image_download

I’ve removed Pillow’s built in image size limits, and I am attempting to download the image using no downsampling. According to what I can tell from the documentation, either not specifying a downsample argument or setting the value to 0 should in theory provide me with the full res image.

However, this is not working, and is giving me an error. The code I am using is below, and the error messages are below that.

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
from pathlib import Path

import logging
import os
from base64 import b64encode

from IPython.display import HTML, display
%matplotlib inline

from PIL import Image
Image.MAX_IMAGE_PIXELS = None

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))

image_api = ImageDownloadApi()
svg_api = SvgApi()

output_dir = 'my/dir/'
atlas_image_id = 112282603
downsample = 0
annotation = False
file_path = Path(output_dir) / '112282603_nissl.jpg'
#I also tried using 112282603_nissl.tif to no avail

image_api.download_atlas_image(atlas_image_id, file_path, annotation=annotation, downsample=downsample)
verify_image(file_path)

Here are the errors I am receiving:
When using .tif extension

TiffFileError: not a TIFF file b'Cont'

When using .jpg extension

RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: D:\a\SimpleITK\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx:99:sitk::ERROR: Unable to determine ImageIO reader for "F:\Test\112282603_nissl.jpg"

Thank you for any advice!

Hi mpj, Thanks for your question and making us aware of this issue. We are not able to address this issue at this time due to lack of resources but plan to in the future.

Hi Eitan,

Thank you for the response.

I had another question I was hoping you could help me with. When using the atlas_image_query to retrieve the full list of image IDs, I get a list of 106 entries that correspond to the slices found here:

However, when looking at the reference information, I see that there are significantly more slices that can be found and downloaded here:
https://www.brainspan.org/ish/experiment/show?id=100149965
Is it possible to retrieve a list of the full image IDs for all the slices found on the “Experiment Detail” page?
Thank you very much!
Best

How can you remove the built in image size limits? It seems that when I try to download an image that is larger than around 450MB, the system downloads a very small file instead that is not the image (for example, when using this download link: http://api.brain-map.org/api/v2/atlas_image_download/112282859?downsample=0&annotation=false )

I have tried using jupyter notebooks but the same problem persists. Thank you!

Hi!
I believe this is the same issue I am having. I think the Pillow max size and the download issue are separate. Removing Pillows max image size is just done with the following bit of code:

from PIL import Image
Image.MAX_IMAGE_PIXELS = None

But the image being ~2kb and seemingly empty is the issue I was having when the downsample was set to 0. I believe it has something to do with the file type on the end of the download API, but I can’t confirm as I haven’t been able to solve it.

If you end up figuring something out please let me know!

@eitan.kaplan I was also wondering if you had a chance to look at my second question in my reply? Thank you so much in advance!

Hi there, I’m not certain, but you may be able to get this information in the XML link at the bottom of the page, and maybe possible via the API.