AttributeError: 'DatasetReader' object has no attribute 'write'

Hello everyone !

I get an issue while downloading an processed image. Here is my code with the process I apply :

import rasterio
import numpy as np
import matplotlib.pyplot as plt

#Define the paths to the TIFF files

path_highpass = ‘cube_s2_highpass.tiff’
path_horizontal = ‘cube_s2_horizontal.tiff’
path_vertical = ‘cube_s2_vertical.tiff’

#Load the data from the TIFF files

with rasterio.open(path_highpass) as src:
cube_s2_highpass = src.read(1)
with rasterio.open(path_horizontal) as src:
cube_s2_horizontal = src.read(1)
with rasterio.open(path_vertical) as src:
cube_s2_vertical = src.read(1)

#Normalize the pixel values of the Sobel and highpass filter outputs

sobel_v_norm = (cube_s2_vertical - np.min(cube_s2_vertical)) / (np.max(cube_s2_vertical) - np.min(cube_s2_vertical))
sobel_h_norm = (cube_s2_horizontal - np.min(cube_s2_horizontal)) / (np.max(cube_s2_horizontal) - np.min(cube_s2_horizontal))
highpass_norm = (cube_s2_highpass - np.min(cube_s2_highpass)) / (np.max(cube_s2_highpass) - np.min(cube_s2_highpass))

#Combine the normalized outputs into an RGB image

combined_img = np.dstack((highpass_norm, sobel_h_norm, sobel_v_norm))

When I plot the resulting combined_img I get the right image (see below) :
image

The thing is that when I try to download it I get the following error :

import rasterio
#Save the combined image as a GeoTIFF file
with rasterio.open(‘combined_img_rgb.tif’) as dst:
dst.write(combined_img)
----> 4 dst.write(combined_img)
AttributeError: ‘DatasetReader’ object has no attribute ‘write’

Any ideas to fix this ?

Thanks for your answers

I’m not sure if you are raising this issue in the right spot, here in the openEO platform forum, because I don’t see any openEO specifics in your description.

However, from a quick look I guess you have to add mode="w" to rasterio.open() when you try to write this TIFF file:

with rasterio.open(‘combined_img_rgb.tif’) as dst:
      dst.write(combined_img)

In fact, I am using openeo data. But sorry I thought this forum was also for other issues on python.

I edited the code as you said but there is a new problem ;

with rasterio.open(‘combined_img.tiff’, ‘w’, driver=‘GTiff’, width=combined_img.shape[1], >height=combined_img.shape[0], count=3, dtype=combined_img.dtype) as dst:
dst.write(combined_img, 1)

ValueError: Source shape (1, 1359, 3412, 3) is inconsistent with given indexes 1

Thanks for your answer