Skip to main content
1 of 3

Can't display a satellite tiff image in SPYDER using Python

I wish to create a python script that opens a multiband satellite tiff image, then selects the bands the RGB image will be displayed with and then from this point and then I can collect my data from the image. However I keep getting this error: "TypeError: Invalid shape (3, 1301, 1950) for image data". Note than this error doesn't appear when it is a single band image and the image displays without any problem. Please take a look on my code and help me figure out what's the matter.

import gdal import matplotlib.pyplot as plt

Open the Sentinel-2 image using GDAL

sentinel_ds = gdal.Open(r'C:\Users\GNR\Desktop\file_example_TIFF_10MB (2).tiff')

Get the band count and band descriptions

band_count = sentinel_ds.RasterCount band_descriptions = [sentinel_ds.GetRasterBand(i+1).GetDescription() for i in range(band_count)]

Print the band count and descriptions

print(f'The image has {band_count} bands:') for i, desc in enumerate(band_descriptions): print(f'Band {i+1}: {desc}')

Select the Red, Green, and Blue bands (bands 4, 3, and 2)

red_band = sentinel_ds.GetRasterBand(4).ReadAsArray() green_band = sentinel_ds.GetRasterBand(3).ReadAsArray() blue_band = sentinel_ds.GetRasterBand(2).ReadAsArray()

Create an RGB image from the bands

rgb_image = plt.imshow([red_band, green_band, blue_band])

Display the RGB image

plt.show()