1. chchrsc
  2. TuiView

Wiki

Clone wiki

TuiView / Saving Images From Python

Saving images from Python

The 'save current display' menu option is all very well for one off images, but sometimes it is handy to be able to script the loading of data into TuiView and saving the result as a .png. For example you may need to create thumbnails of a whole list of images, or easily re-create figures when updated data is available.

Here is a simple example:

import sys
from PyQt4.QtGui import QApplication
from tuiview import geolinkedviewers

...

# have to create one of these before any windows to init the Qt toolkit
app = QApplication(sys.argv)

# set these to get the saved default stretch - saved under 'tuiview' in the settings
# don't need to set if you are going to override the stretch (see below)
app.setApplicationName('tuiview')
app.setOrganizationName('TuiView')

# create a container for geolinked viewers
viewers = geolinkedviewers.GeolinkedViewers()
# start a viewer 
viewer = viewers.newViewer()

# open a file
viewer.addRasterInternal(imgFile)

# save as .png, .jpg etc
# also saves .wld file
viewer.saveCurrentViewInternal(outFile)

Setting the stretch

addRasterInternal can take an extra optional parameter - this is an instance of viewerstretch.ViewerStretch:

from tuiview.viewerstretch import ViewerStretch
...
stretch = ViewerStretch()
stretch.setBands((1, 2, 3))
stretch.setRGB()
stretch.setStdDevStretch()
....
viewer.addRasterInternal(imgFile, stretch)

You can also read in a saved stretch with the fromTextFileWithLUT or fromGDALFileWithLUT calls:

stretch = ViewerStretch.fromTextFileWithLUT(fileWithStretch)

Setting the size

You can call resize() on the viewer instance, but that will set the entire size of the window including menu and frame etc. So resize so that the widget is resized to the requested dimensions (this will be size it is saved) use the resizeForWidgetSize() call:

viewer.resizeForWidgetSize(500, 500)

You may also wish to call viewer.zoomFullExtent() so the whole image fits in the new window.

Adding vectors

Use the addVectorInternal call:

viewer.addVectorInternal(vectorFile)

To change the properties (colour, line width etc) do this:

# add the vector
viewer.addVectorInternal(vectorFile)
# now retrieve the 'layer' that represents the vector
vecLayer = viewer.viewwidget.layers.getTopVectorLayer()
# set any properties
vecLayer.setLineWidth(2)
vecLayer.setColor([50, 50, 50, 255]) # red, green, blue, alpha
vecLayer.setSQL(sql)
# lastly, do this to update window
vecLayer.getImage()
viewer.viewwidget.update()

Zooming to a specific location

from tuiview.viewerwidget import GeolinkInfo
...
# zoom to area of interest
easting = 1749945
northing = 5426100
metresperimgpix = 10 # metres per pixel on the screen
# first param always 0 - tells TuiView this is external request
obj = GeolinkInfo(0, easting, northing, metresperimgpix)
viewers.onMove(obj)

Clearing the window

Call viewer.removeLayer() for each layer you have added and you should be ready to add and save the next image.

Call viewers.closeAll() to close all the open windows.

Updated