* Laurent C. <lrn...@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_COMPRESSION'] = "9"
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
https://grass.osgeo.org/grass74/manuals/variables.html#setting-shell-environment-variables
https://grass.osgeo.org/grass74/manuals/variables.html#grass-related-files),
for example:
```bash
export GRASS_RENDER_IMMEDIATE=cairo
```

and retrieve them via
```python
RENDER_PATH = os.getenv(GRASS_RENDER_PATH)
```

Nikos

# 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.

Cheers,
Laurent

Attachment: signature.asc
Description: PGP signature

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to