1

I'm trying to save a TIFF image stored as a raster layer. The image is fetched from a server so that its source (the url passed when creating the layer) is a HTTP url: the image should be saved to disk. All of this is done within a plugin in Python. QgsRasterFileWriter does allow me to the save the images, which I've done using code like this:

rlayer = QgsRasterLayer("http://some/path/testimg.tif", "testimg")

pipe = QgsRasterPipe()
pipe.set(rlayer.renderer().clone())
pipe.set(rlayer.dataProvider().clone())

file_writer = QgsRasterFileWriter('C:\\some\\path\\'+'testimg_written.tif')
file_writer.writeRaster(pipe, rlayer.width(), rlayer.height(), rlayer.extent(), rlayer.crs())

This creates a new file on the disk. However, when I open said file in QGIS, they seem visually identical, but core attributes have been modified. The new image has lost the original data type or bit depth (e.g. Float32) is instead saved as 'Byte - Eight bit unsigned integer'. Weirder still, Windows properties still recognizes the image as 32 bit. Original image on the left and processed on the right.

Original image on the left and processed on the right

The original images are also single band, but the process seems to force them to be multiband.

My question is this: How do I save a raster layer to disk in Python so that the original attributes stay intact? My goal is simply to save a version that's identical to the one stored as a layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.