{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from pathlib import Path\n", "import numpy as np\n", "import libtiff" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `float32` to `uint16`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load raster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "infile_path = Path('../Data/phase1_rasters/DSM_C12/UH17c_GEF051_TR.tif')\n", "outfile_path = Path('../Res/dem_u8.tif')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "infile = libtiff.TIFF.open(infile_path)\n", "raster = infile.read_image()\n", "raster.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(raster)\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove extremum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raster.max(), raster[raster != raster.max()].max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raster[raster == raster.max()] = raster[raster != raster.max()].max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(raster)\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scale to new representation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rep = np.iinfo(np.uint8)\n", "\n", "raster -= raster.min() - rep.min\n", "raster *= (rep.max - rep.min) / (raster.max() - raster.min())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(raster)\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.iinfo(np.uint16).max, raster.max(), raster.astype(np.uint16).max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cast to new type" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "raster = raster.astype(np.uint8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(raster)\n", "plt.colorbar()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save raster" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "outfile = libtiff.TIFFimage(raster, description='TRISKELE raster')\n", "outfile.write_file(outfile_path)#, compression='lzw')\n", "outfile = None" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 2 }