54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from PIL import Image
|
|
import requests
|
|
from sat7_pointer import *
|
|
|
|
# Load Landsat 7 band 1, 2, 3 TIF images and create a composite image
|
|
# from the three bands.
|
|
#
|
|
# The composite image is a greyscale image with a red, green and blue
|
|
# channel.
|
|
#
|
|
# The red channel is the red channel of band 3, the green channel is the
|
|
# green channel of band 2 and the blue channel is the blue channel of band 1.
|
|
#
|
|
# The composite image is saved as a PNG file.
|
|
|
|
band1 = Image.open('LE07_L1TP_177025_20210723_20210818_02_T1_B1.TIF')
|
|
band2 = Image.open('LE07_L1TP_177025_20210723_20210818_02_T1_B2.TIF')
|
|
band3 = Image.open('LE07_L1TP_177025_20210723_20210818_02_T1_B3.TIF')
|
|
|
|
composite = Image.merge('RGB', (band3, band2, band1))
|
|
|
|
# Load corner coordinates of the image.
|
|
#
|
|
# The coordinates are stored in a MTL text file.
|
|
|
|
mtl_data = load_metadata('LE07_L1TP_177025_20210723_20210818_02_T1_MTL.txt')
|
|
|
|
# Fetch coordinates of a city.
|
|
#
|
|
# The coordinates of a city are fetched from the OpenStreetMap API.
|
|
#
|
|
# The coordinates are used to calculate the corner coordinates of the
|
|
# composite image.
|
|
#
|
|
# # City is Belgorod, Russia.
|
|
|
|
url = 'http://nominatim.openstreetmap.org/search?q=Belgorod,+Russia&format=json'
|
|
response = requests.get(url)
|
|
data = response.json()
|
|
|
|
# Convert bounding box coordinates to image coordinates.
|
|
|
|
x0, y1 = lat_lot_to_pixel(data[0]['boundingbox'][0], data[0]['boundingbox'][2], mtl_data)
|
|
x1, y0 = lat_lot_to_pixel(data[0]['boundingbox'][1], data[0]['boundingbox'][3], mtl_data)
|
|
|
|
print(x0, y0)
|
|
print(x1, y1)
|
|
print(composite.size)
|
|
# Crop the image to the bounding box coordinates.
|
|
|
|
cropped = composite.crop((x0, y0, x1, y1))
|
|
|
|
cropped.save('cropped.png')
|