Skip to main content
naming; removed helpme and please
Source Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

Can't display a satellite tiffTIFF image in Spyder using Python

I wish to create a pythonPython 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 that this error doesn't appear when it is a single band image and the image displays without any problem. Please take a look at my code and help me figure out what'sWhat's the matter. with my code?

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()

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 that this error doesn't appear when it is a single band image and the image displays without any problem. Please take a look at 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()

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 that this error doesn't appear when it is a single band image and the image displays without any problem. What's the matter with my code?

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()
added 100 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

Can't display a satellite tiff image in SPYDERSpyder 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""TypeError: Invalid shape (3, 1301, 1950) for image data". Note thanthat this error doesn't appear when it is a single band image and the image displays without any problem. Please take a look onat 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()

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()

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()

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 that this error doesn't appear when it is a single band image and the image displays without any problem. Please take a look at 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()
Source Link

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()