Last active
September 29, 2015 21:37
-
-
Save deleted/700ef156aad971028302 to your computer and use it in GitHub Desktop.
Create animated gifs from a given Planet Labs imaging strip using the public API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from functools import partial | |
from io import BytesIO | |
import requests | |
from requests.auth import HTTPBasicAuth | |
import imageio | |
import scipy | |
from scipy.misc import imread | |
ENDPOINT = 'https://api.planet.com/v0/' | |
API_KEY = 'YOUR_KEY_HERE' | |
get = partial(requests.get, auth=(API_KEY, '')) | |
def get_strip_for(scene_id): | |
url = ENDPOINT+'scenes/ortho/%s' | |
resp = get(url % scene_id) | |
strip_id = resp.json()['properties']['strip_id'] | |
# import ipdb; ipdb.set_trace() | |
resp = get(ENDPOINT+'scenes/ortho/?strip_id.eq=%f&count=1000' % strip_id) | |
scenes = resp.json()['features'] | |
print "Found %s scenes for strip %f" % (len( scenes ), strip_id) | |
print set(scene['properties']['strip_id'] for scene in scenes) | |
assert(all(scene['properties']['strip_id'] == strip_id for scene in scenes)) | |
return scenes | |
def thumb_urls(scenes): | |
for scene in scenes: | |
url = ENDPOINT+'scenes/ortho/%s/square-thumb?size=lg&format=jpg' % scene['id'] | |
print url | |
yield url | |
def get_bytes(url): | |
resp = get(url, stream=True) | |
return BytesIO(resp.content) | |
def iter_images(urls): | |
for url in urls: | |
print "reading %s" % url | |
yield imageio.imread(get_bytes(url), format='jpg') | |
def build_gif(filename, scenes): | |
urls = thumb_urls(scenes) | |
files = iter_images(urls) | |
imageio.mimwrite(filename, files) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('scene_id') | |
parser.add_argument('-o', dest='outfile', default='out.gif') | |
args = parser.parse_args() | |
scenes = get_strip_for(args.scene_id) | |
# print list(thumb_urls(scenes)) | |
build_gif(args.outfile, scenes) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment