Home Image-Pro General Discussions

Batch process a directory

Hello

I'm writing a script to cycle sequentially through all the images in a directory perform some analysis and close the image and then move on to the next image.

I have the code to get all the files in the directory but I can't figure out how to use the filename to open an image in Premier all the code I usually use opens a dialogue box for the user to select a file. This is a very simple thing but I can't find anything  I was expecting something like Open(filename) but this isn't going to work. my code is below

Imports System.IO
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Module BatchProcess

    Public Function StartHere()
        Dim docList1
        Dim startingpath As String
        Dim Dialog As New FolderBrowserDialog()
        Dialog.RootFolder = Environment.SpecialFolder.Desktop
        Dialog.SelectedPath = "T:\"
        Dialog.Description = "Select Starting Directory"
        If Dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            startingpath = Dialog.SelectedPath
        End If

        Dim TheDirectory As New IO.DirectoryInfo(startingpath)
        Dim DirectoryFiles As IO.FileInfo() = TheDirectory.GetFiles()
        Dim IndividualFiles As IO.FileInfo

        'cycle through names of files in the user specified directory
        Dim filename As String
        For Each IndividualFiles In DirectoryFiles
            filename = IndividualFiles.FullName
            FileOpener(filename)    'Go and open file and do stuff
        Next

    End Function

    Public Function FileOpener(ByRef filename)

        'This doesn't work because it trys to open another dialogue box
        With Application.DocumentCommands.Open(Nothing)
            .Filenames = Nothing
            .Run(docList1)
        End With

    End Function

Regards

David

Best Answer

  • edited May 2015 Answer ✓
    David,

    If you have such conflicts you have to use full class definition:
    System.Windows.Forms.FolderBrowserDialog
    
    or
    
    MediaCy.Automation.Application.DocumentCommands.
    
    There is also an IQL function to open an image:
    ThisApplication.Images.Open("T:\Signature.jpg")
    Yuri


Answers

  • edited May 2015
    Hi David,

    The dialog is shown when you use FileNames=Nothing, as in your example:

    'This doesn't work because it trys to open another dialogue box
            With Application.DocumentCommands.Open(Nothing)
                .Filenames = Nothing
                .Run(docList1)
            End With

    if you want to open a file, pass file names to this variable. (record a macro opening any file to see how it's recorded).

    Also, if you want to process all images in a folder, I would recommend using Premier's Batch processing functionality. Just create a macro to process one image, set is as "Loop On" and use it in a batch processing of the selected folder.

    Regards,

    Yuri


  • Hello Yuri

    That was the first thing I tried but I get the error Method 'System.Windows.Forms.Application.DocumentCommands' not found

    I assumed this was either a dialogue box being called when it shouldn't or a missing reference but I cannot figure out which reference would be needed if that's the problem.

    Regards

    David

  • David,

    Just activate recording and open one file, stop recording: it will generate you a macro to open one file, which you can use as example.

    Yuri
  • Yuri

    That's what I did and the generated code doesn't work, it gives the error above.


    Regards


    David

  • edited May 2015
    David,

    I don't see the error in your post. Can you please also post the recorded macro?

    Thanks,

    Yuri
  • Hello Yuri

    Here is the recorded code

    Public Function NewMacro() As SimpleScript
            NewMacro = New SimpleScript
            Dim docList1 = New List(1), doc1
    
            With Application.DocumentCommands.Open(NewMacro)
                .Filenames = New String() {"T:\Userdata\Manuel\Images\1-1.tif"}
                .Run(docList1)
            End With
    
            With Application.DocumentCommands.Activate(NewMacro)
                .Run(docList1(0), doc1)
            End With
    
        End Function

    which gives the error

    Regards

    David

  • Hi David,

    It looks like your import statement:

    Imports System.Windows.Forms

    causes conflict of namespaces, so DocumentCommands are tried to be loaded from that namespace. Please try to remove that line from your macro file.

    Yuri

  • Hello Yuri

    Can't do that as it stops FolderBrowserDialog from working which is how I get the working directory info from the user.

    Is there a simple command which just opens a file for example

    imp = IJ.openImage("T:\\Signature.jpg");

    I looked in the help files and there is an Open command that looks like it can take a filename but there was no syntax info with it.

    Regards

    David

  • Thanks Yuri

    The IQL function was exactly what I wanted

    Regards

    David

Sign In or Register to comment.