I have tried to use the code from QGis Save Raster as Rendered Image. i is a raster layer:

pipelayer = i
pipeextent = pipelayer.extent()
pipewidth, pipeheight = (pipelayer.width(),
piperenderer = pipelayer.renderer()
pipeprovider = pipelayer.dataProvider()
crs = pipelayer.crs().toWkt()
pipe = QgsRasterPipe()
pipedFile = os.path.join(tempfile.gettempdir(),
                         safeLayerName + '_pipe.tif')
print pipedFile
file_writer = QgsRasterFileWriter(pipedFile)

in_raster = pipedFile
prov_raster = os.path.join(tempfile.gettempdir(),
                           'json_' + safeLayerName +
out_raster = dataPath + '.png'
crsSrc = i.crs()
crsDest = QgsCoordinateReferenceSystem(4326)
xform = QgsCoordinateTransform(crsSrc, crsDest)
extentRep = xform.transform(i.extent())
extentRepNew = ','.join([unicode(extentRep.xMinimum()),
processing.runalg("gdalogr:warpreproject", in_raster,
                  i.crs().authid(), "EPSG:4326", "", 0, 1,
                  5, 2, 75, 6, 1, False, 0, False, "",
processing.runalg("gdalogr:translate", prov_raster, 100,
                  True, "", 0, "", extentRepNew, False, 0,
                  0, 75, 6, 1, False, 0, False, "",

It doesn't work, instead giving me the unstyled raster as final output. I really don't understand the problem, because the output of writeRaster (the file pipedFile) is styled, so the process from the answer linked to above is working. It's just that when I try to use the output image to run through GDAL's warpreproject and translate, it somehow reverts to the unstyled raster.

What have I done wrong?

UPDATE: The output of QgsRasterFileWriter is styled. The output of warpreproject is not styled, but the .tif is accompanied by a .aux.xml file which appears to have colour information in it:

  <PAMRasterBand band="1">
      <MDI key="STATISTICS_MEAN">203.31482680479</MDI>
      <MDI key="STATISTICS_STDDEV">63.158702013579</MDI>

The output of translate is not styled, and has no additional file. However, since it's a PNG not a GeoTIFF, that's perhaps the explanation.

I've tried looking at the expand option of translate, but that gives me image files which do not display in a web browser - I've not investigated why.

UPDATE 2: No, using expand results in no output file at all. Wondering about trying pct2rgb.

UPDATE 3: I'm wondering if file locking is causing the problems. Could it be?

  • The Processing calls are inherited code. Does warpreproject have completely the wrong arguments? If so, I don't know how it works at all. Jan 7, 2016 at 19:51
  • 1
    Just a guess but for gdalogr:translate, try replacing prov_raster with prov_raster['OUTPUT'].
    – Joseph
    Jan 8, 2016 at 13:35
  • No, prov_raster is just a string with the path to that file, so I get TypeError: string indices must be integers Jan 8, 2016 at 15:01

1 Answer 1


You should understand that when you save the file from the QgsRenderer as in your code:

piperenderer = pipelayer.renderer()

You are not saving the raster styled, instead you are saving the actual RGB file that represents that visualization, so it is not classified or styled, but it is like you've taken a print screen of the rendered image.

So gdalwarp and translate can't change this, as it is hardcoded in the image's bands, what must be happening is that they are clipping by a single band or so, I have to check it thorughly.


I can see in the xml file you provided that the raster has now only one band

<PAMRasterBand band="1">

There should be the tags:

<PAMRasterBand band="2">
<PAMRasterBand band="3">

So the problem is in gdalogr:warpreproject. I used the same code you provided in a raster I have but the output is RGB, I don't know what's going on, maybe you should give a sample of your raster file.


The xml has no style data, as I said the raster you firstly saved is not a styled layer, but an actual RGB image representing the styled raster.

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.