Skip to content

Instantly share code, notes, and snippets.

@carolth
Created November 29, 2022 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carolth/783d2e3e48c1d8cf423a78dc704438f5 to your computer and use it in GitHub Desktop.
Save carolth/783d2e3e48c1d8cf423a78dc704438f5 to your computer and use it in GitHub Desktop.
Download all images from an imageseries from Allen Developing Mouse Brain Atlas - python 3
import urllib.request, json
import os
#
# Example python code to download images from an image series
#
# Find the image series you want from the Allen Brain Atlas portal
# Take a note of the dataset id
#
# Specify output directory for images
output_directory = "temp/dataset"
# Specify downsample factor; recommend using 1 for developing mouse brain images @ younger ages
downsample = 1
# Copy the dataset id here
dataset_id = 100045958
# make output directory
if not os.path.exists( output_directory ) :
os.makedirs( output_directory )
# RMA query to get information for images in the dataset
query_url = "http://api.brain-map.org/api/v2/data/query.json?"
query_url += "criteria=model::SectionImage"
query_url += ",rma::criteria,[data_set_id$eq%d]" % (dataset_id)
query_url += ",rma::options[num_rows$eqall]"
response = urllib.request.urlopen (query_url)
images = json.loads(response.read())['msg']
for i in images :
print (i['section_number'], i['id'])
image_url = "http://api.brain-map.org/api/v2/section_image_download/%d?downsample=%d" % (i['id'],downsample)
image_path = os.path.join( output_directory, '%04d_%d.jpg' % (i['section_number'],i['id']) )
urllib.request.urlretrieve(image_url, image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment