Add lab 2

This commit is contained in:
Inex Code 2021-12-03 23:39:22 +03:00
parent 06af1dbb7c
commit cf14da7612
7 changed files with 521 additions and 0 deletions

145
.gitignore vendored Normal file
View file

@ -0,0 +1,145 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# End of https://www.toptal.com/developers/gitignore/api/python

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,300 @@
GROUP = LANDSAT_METADATA_FILE
GROUP = PRODUCT_CONTENTS
ORIGIN = "Image courtesy of the U.S. Geological Survey"
DIGITAL_OBJECT_IDENTIFIER = "https://doi.org/10.5066/P9TU80IG"
LANDSAT_PRODUCT_ID = "LE07_L1TP_177025_20210723_20210818_02_T1"
PROCESSING_LEVEL = "L1TP"
COLLECTION_NUMBER = 02
COLLECTION_CATEGORY = "T1"
OUTPUT_FORMAT = "GEOTIFF"
FILE_NAME_BAND_1 = "LE07_L1TP_177025_20210723_20210818_02_T1_B1.TIF"
FILE_NAME_BAND_2 = "LE07_L1TP_177025_20210723_20210818_02_T1_B2.TIF"
FILE_NAME_BAND_3 = "LE07_L1TP_177025_20210723_20210818_02_T1_B3.TIF"
FILE_NAME_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_B4.TIF"
FILE_NAME_BAND_5 = "LE07_L1TP_177025_20210723_20210818_02_T1_B5.TIF"
FILE_NAME_BAND_6_VCID_1 = "LE07_L1TP_177025_20210723_20210818_02_T1_B6_VCID_1.TIF"
FILE_NAME_BAND_6_VCID_2 = "LE07_L1TP_177025_20210723_20210818_02_T1_B6_VCID_2.TIF"
FILE_NAME_BAND_7 = "LE07_L1TP_177025_20210723_20210818_02_T1_B7.TIF"
FILE_NAME_BAND_8 = "LE07_L1TP_177025_20210723_20210818_02_T1_B8.TIF"
FILE_NAME_QUALITY_L1_PIXEL = "LE07_L1TP_177025_20210723_20210818_02_T1_QA_PIXEL.TIF"
FILE_NAME_QUALITY_L1_RADIOMETRIC_SATURATION = "LE07_L1TP_177025_20210723_20210818_02_T1_QA_RADSAT.TIF"
FILE_NAME_GROUND_CONTROL_POINT = "LE07_L1TP_177025_20210723_20210818_02_T1_GCP.txt"
FILE_NAME_ANGLE_COEFFICIENT = "LE07_L1TP_177025_20210723_20210818_02_T1_ANG.txt"
FILE_NAME_ANGLE_SENSOR_AZIMUTH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_VAA.TIF"
FILE_NAME_ANGLE_SENSOR_ZENITH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_VZA.TIF"
FILE_NAME_ANGLE_SOLAR_AZIMUTH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_SAA.TIF"
FILE_NAME_ANGLE_SOLAR_ZENITH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_SZA.TIF"
FILE_NAME_METADATA_ODL = "LE07_L1TP_177025_20210723_20210818_02_T1_MTL.txt"
FILE_NAME_METADATA_XML = "LE07_L1TP_177025_20210723_20210818_02_T1_MTL.xml"
DATA_TYPE_BAND_1 = "UINT8"
DATA_TYPE_BAND_2 = "UINT8"
DATA_TYPE_BAND_3 = "UINT8"
DATA_TYPE_BAND_4 = "UINT8"
DATA_TYPE_BAND_5 = "UINT8"
DATA_TYPE_BAND_6_VCID_1 = "UINT8"
DATA_TYPE_BAND_6_VCID_2 = "UINT8"
DATA_TYPE_BAND_7 = "UINT8"
DATA_TYPE_BAND_8 = "UINT8"
DATA_TYPE_QUALITY_L1_PIXEL = "UINT16"
DATA_TYPE_QUALITY_L1_RADIOMETRIC_SATURATION = "UINT16"
DATA_TYPE_ANGLE_SENSOR_AZIMUTH_BAND_4 = "INT16"
DATA_TYPE_ANGLE_SENSOR_ZENITH_BAND_4 = "INT16"
DATA_TYPE_ANGLE_SOLAR_AZIMUTH_BAND_4 = "INT16"
DATA_TYPE_ANGLE_SOLAR_ZENITH_BAND_4 = "INT16"
END_GROUP = PRODUCT_CONTENTS
GROUP = IMAGE_ATTRIBUTES
SPACECRAFT_ID = "LANDSAT_7"
SENSOR_ID = "ETM"
WRS_TYPE = 2
WRS_PATH = 177
WRS_ROW = 025
DATE_ACQUIRED = 2021-07-23
SCENE_CENTER_TIME = "07:26:38.9955869Z"
STATION_ID = "NSG"
CLOUD_COVER = 0.00
CLOUD_COVER_LAND = 0.00
IMAGE_QUALITY = 9
SATURATION_BAND_1 = "Y"
SATURATION_BAND_2 = "Y"
SATURATION_BAND_3 = "Y"
SATURATION_BAND_4 = "Y"
SATURATION_BAND_5 = "Y"
SATURATION_BAND_6_VCID_1 = "N"
SATURATION_BAND_6_VCID_2 = "N"
SATURATION_BAND_7 = "Y"
SATURATION_BAND_8 = "N"
SUN_AZIMUTH = 126.84545120
SUN_ELEVATION = 49.90781811
EARTH_SUN_DISTANCE = 1.0159141
SENSOR_MODE = "BUMPER"
SENSOR_MODE_SLC = "OFF"
SENSOR_ANOMALIES = "NONE"
END_GROUP = IMAGE_ATTRIBUTES
GROUP = PROJECTION_ATTRIBUTES
MAP_PROJECTION = "UTM"
DATUM = "WGS84"
ELLIPSOID = "WGS84"
UTM_ZONE = 37
GRID_CELL_SIZE_PANCHROMATIC = 15.00
GRID_CELL_SIZE_REFLECTIVE = 30.00
GRID_CELL_SIZE_THERMAL = 30.00
PANCHROMATIC_LINES = 15081
PANCHROMATIC_SAMPLES = 16981
REFLECTIVE_LINES = 7541
REFLECTIVE_SAMPLES = 8491
THERMAL_LINES = 7541
THERMAL_SAMPLES = 8491
ORIENTATION = "NORTH_UP"
CORNER_UL_LAT_PRODUCT = 51.28241
CORNER_UL_LON_PRODUCT = 34.82816
CORNER_UR_LAT_PRODUCT = 51.35561
CORNER_UR_LON_PRODUCT = 38.48012
CORNER_LL_LAT_PRODUCT = 49.25322
CORNER_LL_LON_PRODUCT = 35.00205
CORNER_LR_LAT_PRODUCT = 49.32136
CORNER_LR_LON_PRODUCT = 38.50186
CORNER_UL_PROJECTION_X_PRODUCT = 209100.000
CORNER_UL_PROJECTION_Y_PRODUCT = 5689500.000
CORNER_UR_PROJECTION_X_PRODUCT = 463800.000
CORNER_UR_PROJECTION_Y_PRODUCT = 5689500.000
CORNER_LL_PROJECTION_X_PRODUCT = 209100.000
CORNER_LL_PROJECTION_Y_PRODUCT = 5463300.000
CORNER_LR_PROJECTION_X_PRODUCT = 463800.000
CORNER_LR_PROJECTION_Y_PRODUCT = 5463300.000
END_GROUP = PROJECTION_ATTRIBUTES
GROUP = LEVEL1_PROCESSING_RECORD
ORIGIN = "Image courtesy of the U.S. Geological Survey"
DIGITAL_OBJECT_IDENTIFIER = "https://doi.org/10.5066/P9TU80IG"
REQUEST_ID = "P700y95yl7543_00333"
LANDSAT_SCENE_ID = "LE71770252021204NSG00"
LANDSAT_PRODUCT_ID = "LE07_L1TP_177025_20210723_20210818_02_T1"
PROCESSING_LEVEL = "L1TP"
COLLECTION_CATEGORY = "T1"
OUTPUT_FORMAT = "GEOTIFF"
DATE_PRODUCT_GENERATED = 2021-08-18T17:58:16Z
PROCESSING_SOFTWARE_VERSION = "LPGS_15.5.0"
FILE_NAME_BAND_1 = "LE07_L1TP_177025_20210723_20210818_02_T1_B1.TIF"
FILE_NAME_BAND_2 = "LE07_L1TP_177025_20210723_20210818_02_T1_B2.TIF"
FILE_NAME_BAND_3 = "LE07_L1TP_177025_20210723_20210818_02_T1_B3.TIF"
FILE_NAME_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_B4.TIF"
FILE_NAME_BAND_5 = "LE07_L1TP_177025_20210723_20210818_02_T1_B5.TIF"
FILE_NAME_BAND_6_VCID_1 = "LE07_L1TP_177025_20210723_20210818_02_T1_B6_VCID_1.TIF"
FILE_NAME_BAND_6_VCID_2 = "LE07_L1TP_177025_20210723_20210818_02_T1_B6_VCID_2.TIF"
FILE_NAME_BAND_7 = "LE07_L1TP_177025_20210723_20210818_02_T1_B7.TIF"
FILE_NAME_BAND_8 = "LE07_L1TP_177025_20210723_20210818_02_T1_B8.TIF"
FILE_NAME_QUALITY_L1_PIXEL = "LE07_L1TP_177025_20210723_20210818_02_T1_QA_PIXEL.TIF"
FILE_NAME_QUALITY_L1_RADIOMETRIC_SATURATION = "LE07_L1TP_177025_20210723_20210818_02_T1_QA_RADSAT.TIF"
FILE_NAME_GROUND_CONTROL_POINT = "LE07_L1TP_177025_20210723_20210818_02_T1_GCP.txt"
FILE_NAME_ANGLE_COEFFICIENT = "LE07_L1TP_177025_20210723_20210818_02_T1_ANG.txt"
FILE_NAME_ANGLE_SENSOR_AZIMUTH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_VAA.TIF"
FILE_NAME_ANGLE_SENSOR_ZENITH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_VZA.TIF"
FILE_NAME_ANGLE_SOLAR_AZIMUTH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_SAA.TIF"
FILE_NAME_ANGLE_SOLAR_ZENITH_BAND_4 = "LE07_L1TP_177025_20210723_20210818_02_T1_SZA.TIF"
FILE_NAME_METADATA_ODL = "LE07_L1TP_177025_20210723_20210818_02_T1_MTL.txt"
FILE_NAME_METADATA_XML = "LE07_L1TP_177025_20210723_20210818_02_T1_MTL.xml"
FILE_NAME_CPF = "LE07CPF_20210701_20210930_02.03"
DATA_SOURCE_ELEVATION = "GLS2000"
GROUND_CONTROL_POINTS_VERSION = 5
GROUND_CONTROL_POINTS_MODEL = 1102
GEOMETRIC_RMSE_MODEL = 4.495
GEOMETRIC_RMSE_MODEL_Y = 3.328
GEOMETRIC_RMSE_MODEL_X = 3.022
EPHEMERIS_TYPE = "DEFINITIVE"
END_GROUP = LEVEL1_PROCESSING_RECORD
GROUP = LEVEL1_MIN_MAX_RADIANCE
RADIANCE_MAXIMUM_BAND_1 = 293.700
RADIANCE_MINIMUM_BAND_1 = -6.200
RADIANCE_MAXIMUM_BAND_2 = 300.900
RADIANCE_MINIMUM_BAND_2 = -6.400
RADIANCE_MAXIMUM_BAND_3 = 234.400
RADIANCE_MINIMUM_BAND_3 = -5.000
RADIANCE_MAXIMUM_BAND_4 = 241.100
RADIANCE_MINIMUM_BAND_4 = -5.100
RADIANCE_MAXIMUM_BAND_5 = 47.570
RADIANCE_MINIMUM_BAND_5 = -1.000
RADIANCE_MAXIMUM_BAND_6_VCID_1 = 17.040
RADIANCE_MINIMUM_BAND_6_VCID_1 = 0.000
RADIANCE_MAXIMUM_BAND_6_VCID_2 = 12.650
RADIANCE_MINIMUM_BAND_6_VCID_2 = 3.200
RADIANCE_MAXIMUM_BAND_7 = 16.540
RADIANCE_MINIMUM_BAND_7 = -0.350
RADIANCE_MAXIMUM_BAND_8 = 243.100
RADIANCE_MINIMUM_BAND_8 = -4.700
END_GROUP = LEVEL1_MIN_MAX_RADIANCE
GROUP = LEVEL1_MIN_MAX_REFLECTANCE
REFLECTANCE_MAXIMUM_BAND_1 = 0.467724
REFLECTANCE_MINIMUM_BAND_1 = -0.009874
REFLECTANCE_MAXIMUM_BAND_2 = 0.525664
REFLECTANCE_MINIMUM_BAND_2 = -0.011181
REFLECTANCE_MAXIMUM_BAND_3 = 0.498370
REFLECTANCE_MINIMUM_BAND_3 = -0.010631
REFLECTANCE_MAXIMUM_BAND_4 = 0.729914
REFLECTANCE_MINIMUM_BAND_4 = -0.015440
REFLECTANCE_MAXIMUM_BAND_5 = 0.696029
REFLECTANCE_MINIMUM_BAND_5 = -0.014632
REFLECTANCE_MAXIMUM_BAND_7 = 0.659156
REFLECTANCE_MINIMUM_BAND_7 = -0.013948
REFLECTANCE_MAXIMUM_BAND_8 = 0.597591
REFLECTANCE_MINIMUM_BAND_8 = -0.011554
END_GROUP = LEVEL1_MIN_MAX_REFLECTANCE
GROUP = LEVEL1_MIN_MAX_PIXEL_VALUE
QUANTIZE_CAL_MAX_BAND_1 = 255
QUANTIZE_CAL_MIN_BAND_1 = 1
QUANTIZE_CAL_MAX_BAND_2 = 255
QUANTIZE_CAL_MIN_BAND_2 = 1
QUANTIZE_CAL_MAX_BAND_3 = 255
QUANTIZE_CAL_MIN_BAND_3 = 1
QUANTIZE_CAL_MAX_BAND_4 = 255
QUANTIZE_CAL_MIN_BAND_4 = 1
QUANTIZE_CAL_MAX_BAND_5 = 255
QUANTIZE_CAL_MIN_BAND_5 = 1
QUANTIZE_CAL_MAX_BAND_6_VCID_1 = 255
QUANTIZE_CAL_MIN_BAND_6_VCID_1 = 1
QUANTIZE_CAL_MAX_BAND_6_VCID_2 = 255
QUANTIZE_CAL_MIN_BAND_6_VCID_2 = 1
QUANTIZE_CAL_MAX_BAND_7 = 255
QUANTIZE_CAL_MIN_BAND_7 = 1
QUANTIZE_CAL_MAX_BAND_8 = 255
QUANTIZE_CAL_MIN_BAND_8 = 1
END_GROUP = LEVEL1_MIN_MAX_PIXEL_VALUE
GROUP = LEVEL1_RADIOMETRIC_RESCALING
RADIANCE_MULT_BAND_1 = 1.1807E+00
RADIANCE_MULT_BAND_2 = 1.2098E+00
RADIANCE_MULT_BAND_3 = 9.4252E-01
RADIANCE_MULT_BAND_4 = 9.6929E-01
RADIANCE_MULT_BAND_5 = 1.9122E-01
RADIANCE_MULT_BAND_6_VCID_1 = 6.7087E-02
RADIANCE_MULT_BAND_6_VCID_2 = 3.7205E-02
RADIANCE_MULT_BAND_7 = 6.6496E-02
RADIANCE_MULT_BAND_8 = 9.7559E-01
RADIANCE_ADD_BAND_1 = -7.38071
RADIANCE_ADD_BAND_2 = -7.60984
RADIANCE_ADD_BAND_3 = -5.94252
RADIANCE_ADD_BAND_4 = -6.06929
RADIANCE_ADD_BAND_5 = -1.19122
RADIANCE_ADD_BAND_6_VCID_1 = -0.06709
RADIANCE_ADD_BAND_6_VCID_2 = 3.16280
RADIANCE_ADD_BAND_7 = -0.41650
RADIANCE_ADD_BAND_8 = -5.67559
REFLECTANCE_MULT_BAND_1 = 1.8803E-03
REFLECTANCE_MULT_BAND_2 = 2.1136E-03
REFLECTANCE_MULT_BAND_3 = 2.0039E-03
REFLECTANCE_MULT_BAND_4 = 2.9345E-03
REFLECTANCE_MULT_BAND_5 = 2.7979E-03
REFLECTANCE_MULT_BAND_7 = 2.6500E-03
REFLECTANCE_MULT_BAND_8 = 2.3982E-03
REFLECTANCE_ADD_BAND_1 = -0.011754
REFLECTANCE_ADD_BAND_2 = -0.013294
REFLECTANCE_ADD_BAND_3 = -0.012635
REFLECTANCE_ADD_BAND_4 = -0.018374
REFLECTANCE_ADD_BAND_5 = -0.017430
REFLECTANCE_ADD_BAND_7 = -0.016598
REFLECTANCE_ADD_BAND_8 = -0.013952
END_GROUP = LEVEL1_RADIOMETRIC_RESCALING
GROUP = LEVEL1_THERMAL_CONSTANTS
K1_CONSTANT_BAND_6_VCID_1 = 666.09
K2_CONSTANT_BAND_6_VCID_1 = 1282.71
K1_CONSTANT_BAND_6_VCID_2 = 666.09
K2_CONSTANT_BAND_6_VCID_2 = 1282.71
END_GROUP = LEVEL1_THERMAL_CONSTANTS
GROUP = LEVEL1_PROJECTION_PARAMETERS
MAP_PROJECTION = "UTM"
DATUM = "WGS84"
ELLIPSOID = "WGS84"
UTM_ZONE = 37
GRID_CELL_SIZE_PANCHROMATIC = 15.00
GRID_CELL_SIZE_REFLECTIVE = 30.00
GRID_CELL_SIZE_THERMAL = 30.00
ORIENTATION = "NORTH_UP"
RESAMPLING_OPTION = "CUBIC_CONVOLUTION"
SCAN_GAP_INTERPOLATION = 2.0
END_GROUP = LEVEL1_PROJECTION_PARAMETERS
GROUP = PRODUCT_PARAMETERS
CORRECTION_GAIN_BAND_1 = "CPF"
CORRECTION_GAIN_BAND_2 = "CPF"
CORRECTION_GAIN_BAND_3 = "CPF"
CORRECTION_GAIN_BAND_4 = "CPF"
CORRECTION_GAIN_BAND_5 = "CPF"
CORRECTION_GAIN_BAND_6_VCID_1 = "CPF"
CORRECTION_GAIN_BAND_6_VCID_2 = "CPF"
CORRECTION_GAIN_BAND_7 = "CPF"
CORRECTION_GAIN_BAND_8 = "CPF"
CORRECTION_BIAS_BAND_1 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_2 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_3 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_4 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_5 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_6_VCID_1 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_6_VCID_2 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_7 = "INTERNAL_CALIBRATION"
CORRECTION_BIAS_BAND_8 = "INTERNAL_CALIBRATION"
GAIN_BAND_1 = "L"
GAIN_BAND_2 = "L"
GAIN_BAND_3 = "L"
GAIN_BAND_4 = "L"
GAIN_BAND_5 = "L"
GAIN_BAND_6_VCID_1 = "L"
GAIN_BAND_6_VCID_2 = "H"
GAIN_BAND_7 = "L"
GAIN_BAND_8 = "L"
GAIN_CHANGE_BAND_1 = "LL"
GAIN_CHANGE_BAND_2 = "LL"
GAIN_CHANGE_BAND_3 = "LL"
GAIN_CHANGE_BAND_4 = "LL"
GAIN_CHANGE_BAND_5 = "LL"
GAIN_CHANGE_BAND_6_VCID_1 = "LL"
GAIN_CHANGE_BAND_6_VCID_2 = "HH"
GAIN_CHANGE_BAND_7 = "LL"
GAIN_CHANGE_BAND_8 = "LL"
GAIN_CHANGE_SCAN_BAND_1 = 0
GAIN_CHANGE_SCAN_BAND_2 = 0
GAIN_CHANGE_SCAN_BAND_3 = 0
GAIN_CHANGE_SCAN_BAND_4 = 0
GAIN_CHANGE_SCAN_BAND_5 = 0
GAIN_CHANGE_SCAN_BAND_6_VCID_1 = 0
GAIN_CHANGE_SCAN_BAND_6_VCID_2 = 0
GAIN_CHANGE_SCAN_BAND_7 = 0
GAIN_CHANGE_SCAN_BAND_8 = 0
END_GROUP = PRODUCT_PARAMETERS
END_GROUP = LANDSAT_METADATA_FILE
END

76
sat.py
View file

@ -51,3 +51,79 @@ print(composite.size)
cropped = composite.crop((x0, y0, x1, y1))
cropped.save('cropped.png')
###############################################################################
# LAB 2
###############################################################################
# Load landsat band 4 TIF image.
# Band 4 is near infrared.
band4 = Image.open('LE07_L1TP_177025_20210723_20210818_02_T1_B4.TIF')
# Claculate the NDVI.
#
# The NDVI is calculated by dividing
# difference of the red and near infrared band 4 by the sum of the
# near infrared and near infrared band 4.
#
# The NDVI is calculated for each pixel in the cropped image.
#
# The NDVI is saved as a PNG image.
ndvi = band4.copy().crop((x0, y0, x1, y1))
red = band3.copy().crop((x0, y0, x1, y1))
result = cropped.copy()
print(red.getpixel((0, 0)))
print(ndvi.getpixel((0, 0)))
# Colors for the NDVI.
def get_color(value):
if value < 0.033:
return (255, 255, 255)
elif value < 0.066:
return (196, 184, 168)
elif value < 0.1:
return (180, 150, 108)
elif value < 0.133:
return (164, 130, 76)
elif value < 0.166:
return (148, 114, 60)
elif value < 0.2:
return (124, 158, 44)
elif value < 0.25:
return (148, 182, 20)
elif value < 0.3:
return (116, 170, 4)
elif value < 0.35:
return (100, 162, 4)
elif value < 0.4:
return (84, 150, 4)
elif value < 0.45:
return (60, 134, 4)
elif value < 0.5:
return (28, 114, 4)
elif value < 0.6:
return (4, 96, 4)
elif value < 0.7:
return (4, 74, 4)
elif value < 0.8:
return (4, 56, 4)
elif value < 0.9:
return (4, 40, 4)
else:
return (0, 0, 0)
for x in range(ndvi.size[0]):
for y in range(ndvi.size[1]):
r = red.getpixel((x, y))
nir = ndvi.getpixel((x, y))
if nir + r == 0:
result.putpixel((x, y), get_color(0))
else:
result.putpixel((x, y), get_color((nir - r) / (nir + r)))
result.save('ndvi.png')