Reading and Visualizing GeoTiff | Satellite Images with Python | by Mohit Kaushik | Towards Data Science

Reading images is super easy in python, as there are many libraries available for reading, editing, and visualizing different formats of images. Examples of these libraries are Matplotlib, OpenCV, Pillow, etc. These libraries work perfectly well with popular image formats such as .png, .tiff, .jpg, .jpeg, etc. but they don’t seem to work for GeoTiff images.
The reason can be easily guessed if we understand how GeoTiff format is different from other image formats. A simple definition would be something given below.
A GeoTIFF is a standard .tif or image file format that includes additional spatial (georeferencing) information embedded in the .tif file as tags.
By tags, it is meant that it has some additional metadata like Spatial extent, CRS, Resolution, etc. along with the pixel values. It is a popular distribution format for satellite and aerial photography imagery.
This article discusses different ways of reading and visualizing these images with python using a jupyter notebook. The libraries used are GDAL, rasterio, georaster, and Matplotlib(for visualization). These libraries will help us in converting those images to simple numpy array format from there we can also perform other image transformations using numpy or TensorFlow although I have not discussed transformations in this article.
Installation steps are mentioned in the documentation of these libraries, I have provided their links in the references section, please go through them.
RasterIO is implemented by Mapbox and it provides python API for reading geo-referenced dataset. Also, the documentation mentions that unlike GDAL python bindings, it doesn't suffer from dangling C pointer and other pointer issues that can crash your program.
import rasteriofrom rasterio.plot import showfp = r'GeoTiff_Image.tif'img = rasterio.open(fp)show(img)
Don’t confuse yourself with the x and y-axis scale values, they are just longitude and latitude values. If you want to read individual bands use the below code.
fp = r'GeoTiff_Image.tif'img = rasterio.open(fp) # mention band no. in read() method starting from 1 not 0show(img.read(3))
# No. of Bands and Image resolutiondataset.count, dataset.height, dataset.width# Coordinate Reference Systemdataset.crs
The version installed with pip for georaster is 1.25. This version may give you some issues, if you face any, just go to your georaster installation directory first. The code below will tell you the installation directory path.
import georasterprint(georaster.__file__)
Now, find the georaster.py file there and change its content with content in this georaster.py file.
Below is the code for visualizing the image having more than one band (or channels).
import georasterimport matplotlib.pyplot as plt# Use SingleBandRaster() if image has only one bandimg = georaster.MultiBandRaster('GeoTiff_Image.tif')# img.r gives the raster in [height, width, band] format # band no. starts from 0plt.imshow(img.r[:,:,2])
We can directly use the plot method instead of selecting and visualizing individual bands.
This is the most popular library when dealing with GeoTiff images but it is sometimes difficult to install and takes lots of effort to be finally able to use it. GDAL has most of its methods and classes implemented in C++, we are using its python bindings here. Most of the libraries like georaster utilize GDAL and provides a nice and simple python interface to it.
from osgeo import gdalimport matplotlib.pyplot as pltdataset = gdal.Open('GeoTiff_Image.tif', gdal.GA_ReadOnly) # Note GetRasterBand() takes band no. starting from 1 not 0band = dataset.GetRasterBand(1)arr = band.ReadAsArray()plt.imshow(arr)
# For no. of bands and resolutionimg.RasterCount, img.RasterXSize, img.RasterYSize# stats about imageimg.GetStatistics( True, True )