Beginner Macro Creation-navigating directory trees

Your place to discuss general handling of images and the CZI file format
Post Reply
user-6549
Posts: 1
Joined: Thu Jan 01, 1970 1:00 am

Beginner Macro Creation-navigating directory trees

Post by user-6549 »

Hi all! I started using Zeiss microscopes as part of a microscopy core facility about 8 months ago, and have finally gotten around to playing with macros as a few users are generating projects that are large enough where automated image manipulation is a major time saver. The key point with these projects are:
1. There is usually some sort of directory tree structure, with the main project folder having several sub-folders for conditions, and those have several sub-folders for samples, with the image files below those.
2. There are frequently enough subfolders and images that even the Batch processing is annoyingly time consuming, and all too often the manipulations the user wants to do are not available as one of the Batch functions.
3. The images frequently have the exact same file names, with the parent folder distinguishing one condition from another.

So while I have done a bit of scripting in Groovy and a few other languages, I am coming at this fairly cold, though the large selection of macros available from both the rep and on this forum have been very useful in getting started.

The first macro (that I modified) was altering the display settings of all files in a project based on a display settings file that could be located anywhere (due to collaborations and network folders) based on a sample macro called "ZEN2 - Apply display setting for multiple images.czmac":

Code: Select all

#######################################################
##Alter the display settings of all CZI files in various subfolders of one top level project folder

##First dialog is looking for the directory with your display setting files
##Second dialog allows you to choose your display setting, and the top of
##folder of the file tree.


from System.IO import File, Directory, FileInfo, Path, SearchOption

Zen.Application.Documents.RemoveAll()

##Collect folder location for display setting files
defaultFolder = "C:\\"
window = ZenWindow()
window.AddFolderBrowser('dispfolder','Select the folder containing your display settings files',defaultFolder)
result=window.Show()
## check, if Cancel button was clicked
if result.HasCanceled == True:
    sys.exit('Macro aborted with Cancel!')

## get string name of source folder/destination folder
dspPath = str(result.GetValue('dispfolder'))

fileNames = []
itype = '*.czdsp'
files = Directory.GetFiles(dspPath,itype)
for i in range(0,files.Length): 
    file = files[i] 
    fileInfo = FileInfo(file) 
    OrgFileNameWE = fileInfo.Name.Substring(0,fileInfo.Name.Length-6)
    fileNames.append(OrgFileNameWE)

##Collect exact display setting file and location of project folder with images
window2 = ZenWindow()
window2.AddFolderBrowser('parentFolder','Parent folder','C:\OAD\Input')
window2.AddDropDown('dval','Display setting       ', fileNames, 0)
result=window2.Show()
## check, if Cancel button was clicked
if result.HasCanceled == True:
    sys.exit('Macro aborted with Cancel!')

path = str(result.GetValue('parentFolder'))
dsp = str(result.GetValue('dval'))
dispset = ZenDisplaySetting()
dispset.Load(dsp,ZenSettingDirectory.Workgroup)

itype = "*.czi" 
## get files
dirs = Directory.EnumerateDirectories(path)
files = Directory.GetFiles(path,itype, SearchOption.AllDirectories)

##Main script
for file in files:
    	PathAndFile = Path.GetFullPath(file)
    	## load image without display the image in center screen area
    	image = Zen.Application.LoadImage(PathAndFile,False) 
        image.SetDisplaySetting(dispset,True) 
        image.Save(PathAndFile)
    	image.Close
##
#######################################################
If anyone has any suggestions on how this could be accomplished in a more streamlined fashion, please do comment/criticize!

My second macro attempt was less successful, as aside from streamlining the process of making all of the images have comparable display settings, the other process of interest is exporting TIFFs for viewing outside of Zen. While I was able to create a macro similar to the above, I have yet to find a way to create the exported TIFF files in the same folder as the original CZI file while traversing the directory tree. Mostly this seems due to the fact that the expset contains the target folder as a text field, and I do not know of a way to ignore the target folder and simply place the TIFF file into the same folder as the original image.
Exporting all images to the same folder will not work due to the duplicate names.

Am I overlooking some easy way to export many TIFFs to their original folders? Can I macro Batch? Should I try to move the file within the for loop back to the location of the original image? Or might it be easier to try and edit the XML file as described in the 3rd post here: http://forums.zeiss.com/microscopy/comm ... SingleFile

I am basing the macro on the macro provided in that thread, except going through multiple folders worth of files.

Any advice on how to proceed would be appreciated.

Cheers
Emma Brown
Posts: 2
Joined: Wed Apr 26, 2023 10:39 am

Re: Beginner Macro Creation-navigating directory trees

Post by Emma Brown »

Thank you for sharing how to create a directory tree in such detail. For a newbie who doesn't know anything, your tutorial helped me a lot. contexto
Brandon Tam
Posts: 1
Joined: Fri Jun 30, 2023 8:50 am

Re: Beginner Macro Creation-navigating directory trees

Post by Brandon Tam »

Actually, I don't know much about navigating directory trees. I hope can see some useful info here.
tiny fishing
Clay Emir
Posts: 1
Joined: Mon Aug 07, 2023 4:46 pm

Re: Beginner Macro Creation-navigating directory trees

Post by Clay Emir »

Dealing with nested sub-folders can be cumbersome. One approach could be to develop a custom macro that recursively navigates through the directory tree and applies the MEP estimation necessary manipulations to the images in each sub-folder based on their parent folder's information.
Adam Sara
Posts: 5
Joined: Fri Dec 15, 2023 2:20 am
Contact:

Re: Beginner Macro Creation-navigating directory trees

Post by Adam Sara »

good
[url=https://suikagame.io]Suika game[/url]
Post Reply