Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: slarti on August 14, 2021, 12:28:06 PM

Title: Console script to import orthomosaic and export it in tiles with certain size
Post by: slarti on August 14, 2021, 12:28:06 PM
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
Title: Re: Console script to import orthomosaic and export it in tiles with certain size
Post by: Paulo on August 14, 2021, 07:21:27 PM
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....
Title: Re: Console script to import orthomosaic and export it in tiles with certain size
Post by: slarti on August 15, 2021, 07:58:07 PM
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
Title: Re: Console script to import orthomosaic and export it in tiles with certain size
Post by: Paulo on August 15, 2021, 08:58:17 PM
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,
Title: Re: Console script to import orthomosaic and export it in tiles with certain size
Post by: slarti on August 16, 2021, 08:33:55 PM
Thank you, seems to work fine now!

Regards!
Title: Re: Console script to import orthomosaic and export it in tiles with certain size
Post by: techris on December 07, 2023, 10:32:15 PM
Hey slarti -- hopefully you're still around and get notices on replies, but I've been on the hunt for a solution to the exact thing you described... python script that would import orthomosaic and export into tiles. I see you were finally able to get your script working and was curious if you wouldn't mind sharing the final code?

Thanks in advanced!!