Created
October 22, 2017 02:53
-
-
Save vincentsarago/ecb5c15a1718c72b83aefedbcb2d7226 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from rio_tiler import aws\n", | |
"from rio_tiler.utils import linear_rescale\n", | |
"from remotepixel.utils import get_colormap\n", | |
"from PIL import Image\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ndvi = 'data/landsat/LC81230362017063LGN00_NDVI.tif'\n", | |
"bucket = 'remotepixel-us-west-2'\n", | |
"z = 12\n", | |
"x = 3368\n", | |
"y = 1618\n", | |
"\n", | |
"\n", | |
"cmap = list(np.array(get_colormap()).flatten())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"tile = aws.tile(bucket, ndvi, x, y, z, 1, 256)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(256, 256)\n", | |
"P\n", | |
"RGB\n", | |
"RGBA\n" | |
] | |
} | |
], | |
"source": [ | |
"nodata = -1\n", | |
"mask = np.where(tile != nodata, 255, 0).astype(np.uint8)\n", | |
"print(mask.shape)\n", | |
"\n", | |
"#rescale ndvi value between 0 and 255\n", | |
"arr = np.where(tile > -1, linear_rescale(tile, in_range=[-1, 1], out_range=[0, 255]), 0).astype(np.uint8)\n", | |
"\n", | |
"# Fill image rescaled tile\n", | |
"img = Image.fromarray(arr, 'P')\n", | |
"img.putpalette(cmap)\n", | |
"print(img.mode)\n", | |
"\n", | |
"# Convert 1band image to RGB\n", | |
"img = img.convert('RGB')\n", | |
"print(img.mode)\n", | |
"\n", | |
"# Add Alpha band\n", | |
"a_channel = Image.fromarray(mask, 'L') # 'L' 8-bit pixels, black and white\n", | |
"img.putalpha(a_channel)\n", | |
"print(img.mode)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment