Environmental monitoring, agriculture, urban planning, disaster management, and climate change studies have all benefited from satellite imagery analysis.
We can acquire significant insights into our planet’s characteristics, changes, and dynamics by exploiting the massive amount of data recorded by Earth-observing satellites.
Python, a versatile and strong programming language, offers a rich ecosystem of libraries and tools for rapid and effective satellite imagery processing.
The use of satellite imagery analysis opens up a world of possibilities. It enables us to extract useful information from satellite photos, such as land cover categorization, vegetation health evaluation, tracking urban expansion, and shoreline mapping.
We can easily process, display, and analyze satellite images by exploiting Python’s versatility and modules such as rasterio, numpy, and matplotlib.
One of the primary benefits of using Python for satellite imagery analysis is its broad range of geospatial data processing modules.
Rasterio, for example, has a simple interface for reading and modifying raster information, making it suitable for working with satellite imagery files.
The numpy module provides sophisticated array operations for performing fast calculations and statistics on picture data. Matplotlib also enables us to generate relevant visuals to help in the interpretation and communication of analytic results.
In the following sections, we will look at actual examples and code snippets to show how Python may be used to analyze satellite photos.
We’ll go over how to open satellite photos, visualize imaging data, extract metadata, calculate band statistics, and run particular analysis like the Normalized Difference Vegetation Index (NDVI).
These examples will help you get started in the intriguing topic of satellite images analysis with Python.
First, we need to import some libraries to help us with our task.
import rasterio
import matplotlib.pyplot as plt
import numpy as np
1. Visualizing and Opening the Satellite Image
We will the Rasterio library in this section to access the satellite image supplied by the satellite_image_path.
The image file is opened using the rasterio.open() method, and the resultant object, satellite_image, represents the opened image.
For this task, I have used the image from this link: https://unsplash.com/photos/JiuVoQd-ZLk and saved it on my computer as “satellite.jpg”.
# Open the satellite image using rasterio
satellite_image_path = 'satellite.jpg'
satellite_image = rasterio.open(satellite_image_path)
After opening the picture, we read it as an array using the satellite_image object’s read() function. The pixel values for each band of the satellite pictures are stored in the image array.
# Read the image as an array
image_array = satellite_image.read()
To visually evaluate the satellite picture, we use the matplotlib.pyplot tool to generate a 10×10-inch figure.
The picture array is shown using the imshow() method. The transpose(1, 2, 0) operation is used to rearrange the picture array’s dimensions to fit the order anticipated by imshow().
Finally, axis(‘off’) hides the axis labels, giving in a clear view of the satellite picture.
# Visualize the image
plt.figure(figsize=(10, 10))
plt.imshow(image_array.transpose(1, 2, 0))
plt.axis('off')
plt.show()
2. Metadata Extraction
We extract vital metadata information about the satellite picture after opening and displaying it. This information assists us in understanding the image’s qualities and offers context for future investigation.
image_width = satellite_image.width
image_height = satellite_image.height
image_crs = satellite_image.crs
image_count = satellite_image.count
print("Image Width:", image_width)
print("Image Height:", image_height)
print("Coordinate Reference System:", image_crs)
print("Number of Bands:", image_count)
Using the width and height attributes of the satellite_image object, we extract the image width and height. The crs property is used to retrieve the image’s coordinate reference system (CRS).
The CRS gives information on the picture’s spatial reference system, allowing us to match image coordinates to real-world places.
Finally, we use the count attribute of the satellite_image object to calculate the number of bands in the image. This data is critical for later analysis because it allows us to obtain the pixel values for each band in the picture array.
3. Band Statistics Calculation
We calculate statistics for each band in the picture array in this part. A loop iterates across each band, and the numpy library’s min, max, mean, and std functions are used to compute these statistics.
A list of dictionaries stores the statistics for each band.
band_stats = []
for band in range(image_count):
band_data = image_array[band]
band_min = np.min(band_data)
band_max = np.max(band_data)
band_mean = np.mean(band_data)
band_std = np.std(band_data)
band_stats.append({'Band': band+1, 'Min': band_min, 'Max': band_max, 'Mean': band_mean, 'Std': band_std})
print("Band Statistics:")
for stats in band_stats:
print(stats)
The loop cycles across each band, with the variable band representing the band index. Using image_array[band], we extract the pixel values from the image array for each band.
Then, for the current band, the np.min(), np.max(), np.mean(), and np.std() functions are used to determine the minimum, maximum, mean, and standard deviation of the pixel values.
The calculated data for each band are saved in a dictionary with keys like ‘Band,’ ‘Min,’ ‘Max,’ ‘Mean,’ and ‘Std. Each dictionary is appended to the list of band stats. Finally, each band’s data are printed to the console.
4. NDVI (Normalized Difference Vegetation Index) Calculation
The NDVI is a popular measure for measuring the health of plants. In this section, we check to see if the picture includes at least four bands, which are required for NDVI computation.
red_band = None
nir_band = None
if image_count >= 4:
red_band = image_array[2] # assuming red band is at index 2
nir_band = image_array[3] # assuming near-infrared band is at index 3
if red_band is not None and nir_band is not None:
ndvi = (nir_band - red_band) / (nir_band + red_band)
# Visualize the NDVI
plt.figure(figsize=(10, 10))
plt.imshow(ndvi, cmap='RdYlGn')
plt.colorbar(label='NDVI')
plt.title('Normalized Difference Vegetation Index (NDVI)')
plt.axis('off')
plt.show()
else:
print("Error: The satellite image does not have the required bands for NDVI calculation.")
To begin, we set the red_band and nir_band variables to None. The image_count variable is then used to determine if the image contains at least four bands.
If so, we infer the red band is index 2 and the near-infrared (NIR) band is index 3. The matching bands from the picture array are assigned to the variables red_band and nir_band.
If both the red and NIR bands are accessible, the NDVI is calculated using the formula (NIR – Red) / (NIR + Red). The NDVI numbers that result indicate the vegetation index for each pixel in the picture.
We then see the NDVI by creating a new figure and displaying the NDVI array using imshow(). The colorbar() method adds a colorbar to the plot, giving the NDVI values a visual reference.
To focus entirely on the NDVI display, we additionally specify a caption for the plot and remove the axis labels with axis(‘off’). Finally, the plot is shown with plt.show().
An error message is written to the console if the picture lacks the requisite bands for NDVI computation (i.e., less than four bands).
5. Bringing the Satellite Image to a Close
It is best practice to use the close() function to close the satellite picture file after conducting the analysis and viewing. This frees any system resources linked to the picture file.
satellite_image.close()
Here is my solution:
That’s it!
Final Notes
Python’s role in aiding the study of these massive datasets is becoming increasingly important as the availability and resolution of satellite images increase.
The ability to use Python to access, process, analyze, and show satellite images offers the path for creative applications and insights that may drive good change and greater knowledge of our world.
Remember to explore the large selection of resources, tutorials, and libraries available to increase your knowledge and abilities as you continue on your adventure in satellite imagery analysis using Python.
Continue to be interested, explore, and use Python’s abilities to uncover the mysteries buried inside satellite pictures.

Leave a Reply