Forum

Author Topic: Exporting orthopicture using chunklabels and date of photo shooting  (Read 6151 times)

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Hi,

using chatgpt i'v been trying to generate a script to export orthopicture using chunklabels and the date of each chunk's photoshoot without any luck (by extracting the date of the first picture for example)

import Metashape

# get the active chunk
chunk = Metashape.app.document.chunk

# set the output path
output_path = "C:\X"

# set the projection and resolution
crs = Metashape.CoordinateSystem("EPSG::32638")
resolution = 0.002

# loop through the cameras and find a valid orthorectification region
valid_ortho = None
for camera in chunk.cameras:
    if not camera.transform:
        continue
    region = chunk.regionForCamera(camera.transform, camera.photo.meta["sensorWidth"], camera.photo.meta["sensorHeight"], crs)
    if region:
        valid_ortho = region
        break

# check if a valid orthorectification region was found
if valid_ortho:
    # set the orthophoto file name
    date = camera.photo.meta["Exif/DateTime"].strftime("%Y%m%d")
    filename = "{}_ortho1_{}.tif".format(chunk.label, date)

    # export the orthophoto
    chunk.exportOrthophoto(
        path=output_path + filename,
        projection=crs,
        dx=resolution,
        dy=resolution,
        format=Metashape.ImageFormatTIFF,
        region=valid_ortho
    )
else:
    print("No valid orthorectification region found.")

what am I doing wrong?

Thanks a lot for your help
« Last Edit: April 26, 2023, 03:28:27 PM by Dukytony »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15472
    • View Profile
Re: Exporting orthopicture using chunklabels and date of photo shooting
« Reply #1 on: April 27, 2023, 03:30:05 PM »
Hello Dukytony,

Can you elaborate a bit the task? Do you need to export the existing orthomosaic from every chunk of the project and use some Date&Time information from the cameras of that chunk as the exported file name?
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Exporting orthopicture using chunklabels and date of photo shooting
« Reply #2 on: April 28, 2023, 05:22:20 PM »
Yes I have multiple chunks in a project.

Running on selected chunks in batch mode, I would like the script to export an orthomosaic using this filename format :  Chunklabel_Ortho1_YYYYMMDD.tif

YYYYMMDD would be taken from Date&time of the first camera of the chunk.

Thanks a lot for your help!


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15472
    • View Profile
Re: Exporting orthopicture using chunklabels and date of photo shooting
« Reply #3 on: April 28, 2023, 06:35:59 PM »
Hello Dukytony,

Can you please check, if the following script works as expected:

Code: [Select]
import Metashape

doc = Metashape.app.document
output_folder = "C:/X/"
crs = Metashape.CoordinateSystem("EPSG::32638")
proj = Metashape.OrthoProjection()
proj.crs = crs
resolution = 0.002

for chunk in doc.chunks:
if not chunk.orthomosaic:
continue #no orthomosaic to export

date = "YYYYMMDD"
for camera in chunk.cameras:
if "Exif/DateTime" in camera.photo.meta:
date = camera.photo.meta["Exif/DateTime"].split(" ",1)[0].replace(":","")
break

label = chunk.label + "_ortho1_" + date + ".tif"
output_path = output_folder + "/" + label

chunk.exportRaster(output_path, source_data = Metashape.OrthomosaicData, format = Metashape.RasterFormatTiles, image_format = Metashape.ImageFormatTIFF, projection = proj, resolution_x = resolution, resolution_y = resolution)
print("Exported ortho from " + chunk.label)
print("Script finished")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Exporting orthopicture using chunklabels and date of photo shooting
« Reply #4 on: April 28, 2023, 09:24:15 PM »
This is working but it's exporting orthomosaics for all chunks even if only one chunk is selected in batch mode.

I tried to edit it with chatgpt without any luck  :-\

Thanks for your help!
« Last Edit: April 29, 2023, 09:08:27 AM by Dukytony »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15472
    • View Profile
Hello Dukytony,

"Run script" operation in Batch Processing dialog is applied to the document, so it doesn't consider chunk selection.

The script can be, however, modified, so that it check, if the chunk is selected or not in the Workspace pane and proceed to the export operation only for selected chunks, if such solution is feasible to you.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Dukytony

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re Alexey,

Yeah I was looking for a way to apply the script only to the chunks selected in the batch mode pane.

I don't know if that's possible.

Thank you very much for your help anyways!