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.
-
Would you mind marking the answer I wrote as accepted? meta.stackexchange.com/questions/5234/…Germán Carrillo– Germán Carrillo2015年01月13日 16:25:22 +00:00Commented Jan 13, 2015 at 16:25
-
1I thought I did. I've clicked the checkmark several times.gusbo– gusbo2015年01月14日 18:14:59 +00:00Commented Jan 14, 2015 at 18:14
1 Answer 1
Just pass "TIF" as argument. Try this from the QGIS python console:
iface.mapCanvas().saveAsImage("/path/to/file.tif",None,"TIF")
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– gusbo2015年01月03日 21:43:31 +00:00Commented 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– gusbo2015年01月04日 22:43:04 +00:00Commented 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.Germán Carrillo– Germán Carrillo2015年01月05日 00:30:59 +00:00Commented Jan 5, 2015 at 0:30
-
This probably has to do with the fact that QgsMapCanvas is a SIP-wrapped C++-class.gusbo– gusbo2015年01月06日 09:05:25 +00:00Commented Jan 6, 2015 at 9:05
-
Hello from the future... Here in the future, if you pass
None
forQPixmap
(in QGIS 2.19) you getNameError: 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, soQPixmap
is doing my head in.GT.– GT.2017年06月20日 22:57:20 +00:00Commented Jun 20, 2017 at 22:57