I want to create a processing script that saves the QGIS MapCanvas exactly as it is seen on the screen and then Zip up the tifw and a created PRJ. The existing QgsMapCanvas.saveAsImage()-method does exactly what I want. Saving to PNG works, but I want TIF. The third argument takes a QString which I'm supposed to provide like QString("TIF").

However I can't seem to work out how to create a QString. It seems the present wrappers around Qt has removed this class. Just sending in a string or a unicode string ( eg u"TIF") does not work.

Nor does the solution in this thread work: QGIS Map tools and Map canvas--QString errors

I'm clueless as to how to get around this problem without rewriting the logic in saveAsImage().

UPDATE: I got it to work by passing None as per gcarillo's solution, however I have since moved on by using the code from Martin Dobias in this thread: http://osgeo-org.1560.x6.nabble.com/How-to-use-QPixmap-argument-in-saveAsImage-td4112041.html. This is a better solution (although more complicated) as you can set the output resolution.


1 Answer 1


Just pass "TIF" as argument. Try this from the QGIS python console:


It works on QGIS 2.6.0

If you're working on a plugin, you also have the iface object at your disposal.

  • Thanks! This indeed works. I was fooled by the documentation leading me to believe that I could omit the second argument: iface.mapCanvas().saveAsImage("/tmp/img.tif","TIF") Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: QgsMapCanvas.saveAsImage(QString, QPixmap QPixmap=None, QString="PNG"): argument 2 has unexpected type 'str'
    – gusbo
    Commented Jan 3, 2015 at 21:43
  • When I come to think of it this is standard Python-behavior. If I wanted to omit the second argument I think I would have to specify the third arguments as a keyword argument. I don't know if this is possible on the QgsMapCanvas-class.
    – gusbo
    Commented Jan 4, 2015 at 22:43
  • Right, one should be able to provide a key/value for the third argument and omit the second one. I attempted to do so with no success. No idea why. Commented Jan 5, 2015 at 0:30
  • This probably has to do with the fact that QgsMapCanvas is a SIP-wrapped C++-class.
    – gusbo
    Commented Jan 6, 2015 at 9:05
  • Hello from the future... Here in the future, if you pass None for QPixmap (in QGIS 2.19) you get NameError: global name nullptr is not defined in Python. The image still saves as a TIFF, but if you're doing more than about 20 of them, your iterator will gag and the Python error box will stop everything (when you close it, the process will continue for another ~20). I'm doing a loop through 990 features, so QPixmap is doing my head in.
    – GT.
    Commented Jun 20, 2017 at 22:57

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.