Search code examples
pythondictionaryrasterio

'DatasetReader' object is not subscriptable?


how can i iterate over image pixels by pixels readed by rasterio?

img = rasterio.open("FalseColorImages.jpg", masked=True)
profile = img.profile

i try this but it took me to error


for i in np.nditer(img.shape[0]):
    for j in np.nditer(img.shape[1]):
                    print(img[i][j])

and i try this also

for i in range(img.shape[0]):
          for j in range(img.shape[1]):
                    print(img[i][j])

and i try this also

for i in range(img.shape[0]):
          for j in range(img.shape[1]):
                    print(list(img[i][j]))

Solution

  • What you did was just open an image with rasterio. Now you need to read this image by using

    img.read(channel_number_to_read)
    

    in this way, you will have a matrix that represents the image channel. So you can iterate over it.

    Note: an image can have different channels. You can check how many channels it has by looking at count in the output of the code below:

    image.profile