Forum

Author Topic: Image Bulk Rename  (Read 3942 times)

kylemryan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Image Bulk Rename
« on: October 05, 2023, 12:48:06 PM »
Is there a way to bulk rename images in a workspace? The reason I ask is I have multiple contiguous chucks I would like to merge together and I would like to "lock down" a few of the chucks at a time so the other merged chunks adjust to them during bundle adjustment. Adding a prefix with the chunk name would make this much easier. (I already ran align images so I don't want to rename the images in Windows and redo the align.) Cheers.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15359
    • View Profile
Re: Image Bulk Rename
« Reply #1 on: October 10, 2023, 01:05:19 PM »
Hello kylemryan,

I recommend to put the cameras to the camera groups in the original chunks before merging and name the groups according to the chunk labels, thus it will be easier to navigate workspace after chunk merging operation, as the groups will be preserved.

And here is the script that allows to add the chunk's label as a prefix to the camera label (either to selected chunks or to all the chunks in the active project):
Code: [Select]
from PySide2 import QtCore, QtGui, QtWidgets
import Metashape

class RenameCameras(QtWidgets.QDialog):

def __init__ (self, parent):
QtWidgets.QDialog.__init__(self, parent)

self.setWindowTitle("Add chunk label to camera names")
self.btnQuit = QtWidgets.QPushButton("&Exit")
self.btnP1 = QtWidgets.QPushButton("&Rename")
self.pBar = QtWidgets.QProgressBar()
self.pBar.setTextVisible(False)

self.radioBtn_all = QtWidgets.QRadioButton("Apply to entire workspace")
self.radioBtn_sel = QtWidgets.QRadioButton("Apply to selected chunks")
self.radioBtn_all.setChecked(True)
self.radioBtn_sel.setChecked(False)

layout = QtWidgets.QGridLayout()   #creating layout
layout.addWidget(self.radioBtn_all, 0, 1)
layout.addWidget(self.radioBtn_sel, 1, 1)
layout.addWidget(self.pBar, 2, 0)
layout.addWidget(self.btnP1, 2, 1)
layout.addWidget(self.btnQuit, 2, 2)
self.setLayout(layout) 

proc_chunks = lambda : self.proc_all()

QtCore.QObject.connect(self.btnP1, QtCore.SIGNAL("clicked()"), proc_chunks)
QtCore.QObject.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))

self.exec()

def proc_all(self):

global app
print ("\nScript started...")
self.pBar.setMinimum(0)
self.pBar.setMaximum(0)
self.btnP1.setDisabled(True)
self.btnQuit.setDisabled(True)
self.radioBtn_sel.setDisabled(True)
self.radioBtn_all.setDisabled(True)

selected = False
if self.radioBtn_sel.isChecked():
selected = True
elif self.radioBtn_all.isChecked():
selected = False

doc = Metashape.app.document
for chunk in doc.chunks:
if selected and not chunk.selected:
print("Chunk '" + chunk.label + "' is not selected. Skipping...")
continue
print("Processing chunk " + chunk.label + "...")
app.processEvents()
for camera in chunk.cameras:
if camera.type != Metashape.Camera.Type.Regular:
continue #skipping animation cameras
camera.label = chunk.label + "_" + camera.label
print("..done!")
app.processEvents()

self.pBar.setMaximum(100)
self.pBar.setValue(100)
self.btnP1.setDisabled(False)
self.btnQuit.setDisabled(False)
self.radioBtn_sel.setDisabled(False)
self.radioBtn_all.setDisabled(False)
app.processEvents()
print("Script finished.\n")
return 1

def rename_cameras():

global app
app = QtWidgets.QApplication.instance()
parent = app.activeWindow()
dlg = RenameCameras(parent)

Metashape.app.addMenuItem("Custom menu/Add chunk label to camera names", rename_cameras)
Best regards,
Alexey Pasumansky,
Agisoft LLC