[GRASS-user] d.rast in python script

Nikos Alexandris nik at nikosalexandris.net
Mon Oct 22 23:04:47 PDT 2018

* Laurent C. <lrntct at gmail.com> [2018-10-20 22:06:38 +0100]:

>Hello Frank,
>It is not very straightforward, but it is possible.
>Here is an example on how I do it:
>import os
>import grass.script as gscript
>from grass.pygrass.gis.region import Region
># Set general env
>os.environ['GRASS_RENDER_IMMEDIATE'] = "cairo"
>os.environ['GRASS_RENDER_FILE_READ'] = "TRUE"

For reasons I don't understand, some times, the above way to set an environment
variable, does not work for me!

Then, I also use:
os.putenv("GRASS_RENDER_FILE", render_file)

Else, as we know, one can set these variables in a bash environment (see also
for example:

and retrieve them via


># Set image size using the region
>region = Region()
>xr = region.cols
>yr = region.rows
>ratio = xr/yr
>height = int(WIDTH / ratio)  # Choose the width you like
>os.environ['GRASS_RENDER_WIDTH'] = str(WIDTH)
>os.environ['GRASS_RENDER_HEIGHT'] = str(height)
># Then you can draw
>os.environ['GRASS_RENDER_FILE'] = img_file_name
>gscript.run_command('d.rast', map=my_map, quiet=True)
>Be aware that every time you run your script, it will add layer on the file.
>So you might want to delete the png file between the runs.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20181023/b6d0f3e7/attachment.sig>

More information about the grass-user mailing list