[Qgis-user] using python to render an image using the console

Richard Duivenvoorde rdmailings at duif.net
Sat Jan 14 12:29:57 PST 2012


On 01/13/2012 04:37 PM, Rhys A.D. Stewart wrote:
> Using the PyQGIS Developer Cookbook to help me save an image. however
> when I follow the instructions at
> http://www.qgis.org/pyqgis-cookbook/composer.html i get an error
> message saying QImage not defined. am not a QT guru by any means, so
> I'm not sure where to go. it doesn't seem to be an importable module
> as import QImage gave me an error message.
>
> Also how does one zoom to a feature using the console?

Hi Rhys,

What is it exactly that you want to save to an image?

I think indeed that you are on the right track by looking into
http://www.qgis.org/pyqgis-cookbook/index.html
but also have a look into the api:
http://qgis.org/api/
or maybe download some plugins which do almost similar things to what 
you want, and have a look in their python code.
There you see that to be able to use the Qt classes, you have to import eg:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
then you can try in the console:
QImage
which will show you that it is, an:
<class 'PyQt4.QtGui.QImage'>

If you want to zoom to a feature via the console, I think you have to go 
to the layer of the feature, then to the dataprovider of that feature 
and then find or walk over the feature to find the feature you want, get 
the xy or centre xy of that feature and let the map canvas center on 
that xy and maybe zoom to a certain scale (about getting to the 
geometries of features, eg have a look into the htmlimageplugin or the 
simplesvg plugin). There are other ways to get to a selected feature etc.

About making an image. It's one line of code to get an image of current 
mapcanvas:
   qgis.utils.iface.mapCanvas().saveAsImage('/tmp/test.png')
So if you manage to get to your feature, and then make an image of the 
canvas you are settled :-) ?

Hope this helps a little, there is some learning curve, but a lot is 
possible with QGIS and python!

Regards,

Richard Duivenvoorde



More information about the Qgis-user mailing list