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

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.

  • I'm seeing similar problems with single band rasters being written out as multi band. Did you solve this? Feb 13, 2020 at 10:17

1 Answer 1


Simple to fix, but took me too long to find this out. Do not set the renderer, i.e., remove this line:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.