Skip to content

Instantly share code, notes, and snippets.

@bkj
Created February 12, 2020 16:39
Show Gist options
  • Save bkj/0253330183c0b028649a7a32cf56c75f to your computer and use it in GitHub Desktop.
Save bkj/0253330183c0b028649a7a32cf56c75f to your computer and use it in GitHub Desktop.
import ee
import urllib
import numpy as np
from skimage import io
from PIL import Image
from zipfile import ZipFile
from matplotlib import pyplot as plt
ee.Initialize()
# --
# Download image
area = [
[105.4084512289977,12.960956032145036],
[105.46544280614614,12.960956032145036],
[105.46544280614614,13.006454200439705],
[105.4084512289977,13.006454200439705],
[105.4084512289977,12.960956032145036],
]
area = ee.Geometry.Polygon(area)
collection = (
ee.ImageCollection("COPERNICUS/S2")
.filterBounds(area)
.filterDate("2018-01-01","2019-01-10")
.filterMetadata("CLOUDY_PIXEL_PERCENTAGE","less_than",10)
.select(['B4', 'B8'])
)
n_frames = collection.size().getInfo()
print(n_frames)
def get_ndvi(img):
return ee.Image(img.normalizedDifference(['B8', 'B4'])).rename(["ndvi"])
img = collection.map(get_ndvi).median()
img = img.rename(['result'])
url = img.clip(area).getDownloadURL(params={'scale' : 10})
print(url)
urllib.request.urlretrieve(url, 'tmp.zip')
ZipFile('tmp.zip').extractall('./')
# --
# Read w/ PIL (garbage)
z = np.array(Image.open('download.tif'))
_ = plt.imshow(z)
plt.show()
# --
# Read w/ skimage (good)
z = io.imread('download.tif')
_ = plt.imshow(z)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment