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 as src:
cube_s2_highpass =
with as src:
cube_s2_horizontal =
with as src:
cube_s2_vertical =

#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) :

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‘combined_img_rgb.tif’) as dst:
----> 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 when you try to write this TIFF file:

with‘combined_img_rgb.tif’) as dst:

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‘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