Forum

Author Topic: Console script to import orthomosaic and export it in tiles with certain size  (Read 1665 times)

slarti

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hello,
I'm new to scripting and don't really now where to start... I've tried reading the Metashape Python Reference.pdf but can't really say that it helps me get started.

What I want to do is the following:

Place a geotiff file and a script file in the same folder, then run the script by double clicking it. The script should do the following:
1. Create project in current folder
2. import orthomosaic from current folder
3. export orthomosaic in tiles with specific settings to same folder och subfolder
4. preferably delete project and associated files, so all that I have left is the original geotiff and the resulting geotiff split in tiles.

Is this possible to acheive? Anyone here that could help me get started?  :)

Thanks in advance

Paulo

  • Hero Member
  • *****
  • Posts: 803
    • View Profile
slarti,

you could look at https://agisoft.freshdesk.com/support/solutions/articles/31000133141-how-to-run-the-script-in-headless-mode-from-the-command-line for info about how to run Metashape in headless mode from command line.

so you could do (Windows):

metashape.exe -r <myscript.py> [argument1]

where  argument1 contains the path to your orthomosaic

in myscript.py should contain something like:

Code: [Select]
import Metashape as ps
import sys
doc = ps.app.document
dir_path = sys.argv[1].rsplit('\\',1)[0]
doc.save(path = dir_path + '\\test.psx')
chunk = doc.chunk
chunk.importRaster(path=sys.argv[1],raster_type=Metashape.OrthomosaicData)
chunk.exportRaster(...)

look at Python API reference for details on parameters you need to use to correctly do export operation...

I think this can get you started....
« Last Edit: August 15, 2021, 09:03:26 PM by Paulo »
Best Regards,

Paul Pelletier

slarti

  • Newbie
  • *
  • Posts: 6
    • View Profile
Thanks for the great help!

I got it running with a simple test through the Metashape UI and the run script dialog. However if I try to run the same script through cmd I get this error:


chunk.importRaster(path=sys.argv[1],raster_type=Metashape.OrthomosaicData)
AttributeError: 'NoneType' object has no attribute 'importRaster'


any ideas one why it behaves different and how I can fix it?

Regards

Paulo

  • Hero Member
  • *****
  • Posts: 803
    • View Profile
Ok it seems in headless mode, the saved document has no chunk so just adding addChunk command fixes it as:
Code: [Select]
import Metashape as ps
import sys
doc = ps.app.document
dir_path = sys.argv[1].rsplit('\\',1)[0]
doc.save(path = dir_path +'\\test.psx')
doc.addChunk()
chunk = doc.chunk
chunk.importRaster(path=sys.argv[1],raster_type=Metashape.OrthomosaicData)
chunk.exportRaster(...)

Hope it works now,
Best Regards,

Paul Pelletier

slarti

  • Newbie
  • *
  • Posts: 6
    • View Profile
Thank you, seems to work fine now!

Regards!