Apply macro to an entire directory
(Originally posted by Octothorpe on 10/8/2007)
Greetings.
I'm
generally proficient on writing simple macros in IPP, but have zero
background in this type of programming language. I've been tearing my
hair out trying to get my macro to be able to be applied to an entire
directory, and output the files with a different filename. I've
searched the forum and have tried copying and pasting code into my
macro, but to no avail.
Here is the macro I want applied to an
entire directory (the files I'm applying this to are ics/ids stacks
generated from a Nikon C1si confocal microscope):
Sub confocal_max_proj()
'<c>M
ret = IpCmpSet( COMP_MAKESEQUENCE, DOCSEL_ACTIVE, 0 )
ret = IpEDFShow(1)
ret = IpEDFAdd(2)
ret = IpEDFNew(2)
ret = IpEDFSet(EDF_NORMALIZE, 0, 0)
ret = IpEDFSet(EDF_CRITERIA, EDF_MAX_INTENSITY, 0)
ret = IpEDFSet(EDF_ORDER, EDF_BOTTOMUP, 0)
ret = IpEDFCreate(EDF_COMPOSITE)
ret = IpEDFShow(0)
End Sub
Ideally,
when the macro is run, I would like it to prompt me for the directory I
wish to apply the macro to, and then once it runs automatically change
the filename of the generated images by appending a "_maxproj" to it.
I've tried copying and pasting the following macro into mine, but keep coming up with errors:
http://support.mediacy.com/answers/showquestion.asp?faq=44&fldAuto=322#FILENAMES
Any
help would be MUCH appreciated, as I have a client whom needs thousands
of confocal files processed. I truly want to learn how to write more
complex macros, and I hope that any responses I get here will allow me
to understand the language more by reverse engineering it.
Comments
(Originally posted by KevinR on 10/8/2007)
Good question, Tracy. Any number of folks wish to run the same code on multiple images, and we've tried to support that.
An excellent starting point would be the Batch_Process.ipm macro distributed with Image-Pro. Make a copy of this file in the \Scripts directory, and name it something informative such as "My_Confocal_Projection.ipm".
The Batch_Process script includes three major macros that are intended to be customized for your use. The first, ProcessOpenDocs, is a framework that will run a set of code on all currently open images. The second, ProcessDirectory, will run a set of code on every image in a directory, while ProcessSubDir will run on a directory and all subdirectories. The existing code will handle looping through images and tracking file names - you just add your own processing as appropriate.
Walking through this for your case:
- Make a copy of the Batch_Process.ipm script, rename it as desired, and select it as the active script using "Macro | Macro... | Change".
- Scroll down in the script file to ProcessDirectory, and look for '***'. This marks where you can insert your code.
- At "Call a setup routine ***" you can put in any startup code if needed. This isn't necessary for your particular application.
- At "Call a finish routine ***" you can run whatever code you need for finishing your processing. I often place code generating a report at this point, or final cleanup.
- At "Call processing routine here ***" you can insert your processing code. However, to make things a bit simpler, I would suggest going to the "DoStuff" routine in the script file.
"DoStuff" contains the following:
Private Sub DoStuff(source As Integer, fileName As String) ' Sample processing code for ProcessDirectory() ' Note that the temporary image generated here is closed before exiting Dim invImage As Integer ' Process. Keep track of images generated in this routine. invImage = IpWsDuplicate() ret = IpFltSobel() ret = IpLutSetAttr(LUT_CONTRAST, -2) ' Arrange and display ret = IpAppSelectDoc(DOCSEL_NEXTID) ret = IpAppArrange(DOCS_TILE) ' Pause for viewing ret = IpMacroWait(5) ' You could write out a processed image here ' ret = IpWsSave(...) or IpWsSaveAs(...) ' Clean up temporary images ret = IpAppSelectDoc(invImage) ret = IpDocClose() End Sub
To perform your processing, replace the example code with your own, something like the following:
Private Sub DoStuff(source As Integer, fileName As String) ' Sample processing code for ProcessDirectory() ' Note that the temporary image generated here is closed before exiting Dim edfImage As Integer, compImage As Integer ' Process. Keep track of images generated in this routine for cleanup later compImage = IpCmpSet( COMP_MAKESEQUENCE, DOCSEL_ACTIVE, 0 ) ret = IpEDFShow(1) ret = IpEDFAdd(compImage) ret = IpEDFNew(compImage) ret = IpEDFSet(EDF_NORMALIZE, 0, 0) ret = IpEDFSet(EDF_CRITERIA, EDF_MAX_INTENSITY, 0) ret = IpEDFSet(EDF_ORDER, EDF_BOTTOMUP, 0) edfImage = IpEDFCreate(EDF_COMPOSITE) ret = IpEDFShow(0) ' Modify the file name fileName = Left(fileName, Len(fileName-4)) + "_maxproj.tif" ret = IpWsSaveAs(fileName, "tif") ' Clean up temporary images ret = IpAppSelectDoc(edfImage) ret = IpDocClose() ret = IpAppSelectDoc(invImage) ret = IpDocClose() End Sub
I've also added saving the file. To get the output file name, the input name is clipped (removing the '.ics' or other extension), the "_maxproj" and an extension are added to it, and the image is saved under that name. Feel entirely free to choose another extension and format - the .TIF is just an example.
You may need to replace the lines
ret = IpAppSelectDoc(docID) ret = IpDocClose()
with
ret = IpAppCloseAll()
in ProcessDir after the DoStuff call - this macro was originally written for single image processing (one at a time), and if you're working with image sets you will need to close all the related images instead of just one.