Skip to content

Instantly share code, notes, and snippets.

@controversial
Created December 19, 2015 18:19
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 controversial/00b98ddb293412a30801 to your computer and use it in GitHub Desktop.
Save controversial/00b98ddb293412a30801 to your computer and use it in GitHub Desktop.
satellite.py
import urllib2
import json
import time
from io import BytesIO
from PIL import Image
URL="https://api.nasa.gov/planetary/earth/imagery?lon={}&lat={}&date={}&api_key={}"
API_KEY="ymjXp9qz2jJDR5tNEb0qa5YekynvAcVLJvKVcxYH"
def get_satellite_image(location=None,date=None):
if location is None:
location=(41.7475067,-74.0968971) #Coordinates of New Paltz
if date is None:
date=time.strftime("%Y-%m-%d")
lat,lon=location
req_url=URL.format(str(lon),str(lat),date,API_KEY)
req=json.loads(urllib2.urlopen(req_url).read())
image_url=req["url"]
print "Image at "+image_url
image = urllib2.urlopen(image_url)
b=BytesIO(image.read())
return b
def get_stitched_satellite_image(l=(41.7475067,-74.0968971),date=None):
d=0.025
coords = [l,#Original
(l[0]+d,l[1]),(l[0],l[1]+d),(l[0]-d,l[1]),(l[0],l[1]-d),#Horizontally adjacent
(l[0]+d,l[1]+d),(l[0]+d,l[1]-d),(l[0]-d,l[1]-d),(l[0]-d,l[1]+d)]#Diagonally adjacent
coords.sort()
image_files=[get_satellite_image(c,date) for c in coords]
images=[Image.open(i) for i in image_files]
imsize=512
canvas=Image.new('RGB',(imsize*3,imsize*3))
index=0
for y in range(2,-1,-1):
for x in range(3):
coords=(x*imsize,y*imsize)
canvas.paste(images[index],coords)
index+=1
return canvas
def save_satellite_image(location=None,date=None,filename='out.png'):
Image.open(get_satellite_image(location,date)).save(filename)
def save_stitched_satellite_image(location=(41.7475067,-74.0968971),date=None,filename='out.png'):
get_stitched_satellite_image(location,date).save(filename)
if __name__ == "__main__":
save_stitched_satellite_image((40.7,-74.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment