Home Image-Pro Plus Automation with Macros

How do I create a Macro to run as a loop to convert avi files and save them as jpeg

I have many avi files need to be "process" as best focus image then save avi file as jpeg.  
How do I create a macro that will allow me to do this automatically.  Meaning,  I just have to tell the macro where the avi file are located the macro will automatically convert the avi file after it have been process then save them as jpeg.   Meaning each avi file after the process will generate one jpeg image.  

Here is the general idea
step 1 load the avi file
step 2 run the process to generate the best focus image
step 3 save the image as jpeg.

I would like to put step 1, step 2 , step 3 into a loop  because I have alot  of avi files that need to go through step 1, step 2, step 3 repeatedly..
So far my macro only allow me to process one avi file at a time then I have to save the image manually,  meaning I have to click on save and change the name of the image.    How to do I do this AUTOMATICALLY?     Going from avi to jpeg repeatedly similar to batch process all the avi file after it has been process as best focus image and save the focus image as jpeg.

Can anyone answer this question?





Best Answer

  • Answer ✓
    Hi Yuri,

    Thanks.  This actually works.   Hope to see you answer more questions on the forum.
    Next step of the process will be data collection.

    Best Regard,

    Bill


Answers

  • edited August 2013
    Hi Bill,

    You can do it using Batch functionality of Premier.
    Here is the macro that gets the file name of the original image, does processing as in your Step 2 (commented) and saves it to JPG file to the Images folder:

        Public Function MyBatchMacro() As SimpleScript
            MyBatchMacro = New SimpleScript
            Dim image1, image2
            Dim FileName
    
            With Application.DocumentCommands.ActiveImage(MyBatchMacro)
                .Run(image1)
            End With
    
            With Automate.ScriptingCommands.CodeCommand(MyBatchMacro)
                If .Run() Then
                    ' get name of the original image file without extension'
                    FileName=System.IO.Path.GetFileNameWithoutExtension(image1.File.FileName)
                End If
            End With
    
            'do your EDF processing'
            'step 2'
    
            With Application.DocumentCommands.ActiveImage(MyBatchMacro)
                .Run(image2)
            End With
    
            'save result as JPG file to your folder'
            With Application.DocumentCommands.SaveAs(MyBatchMacro)
                'change output folder if necessary'
                .Filename = ThisApplication.Path(mcPathType.mcptWritableImages) & FileName & ".jpg"
                .Run(image2)
            End With
    
        End Function

    You have to modify it adding a call to your EDF processing and set this macro as "Loop On" macro in the Batch ribbon bar of the automation tab. Then you just select the folder and click the Batch Process button to process all files.

    Yuri
  • edited August 2013
    I just noticed that it was a question for Image-Pro Plus. My answer above was for Image-Pro Premier.

    You can also do it in Image-Pro Plus, but differently. There is a sample macro project called "Batch_Process.ipm" in the Scripts folder. 
    You can use ProcessDirectory macro to process all images in the folder. The processing is done in DoStuff macro:

    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
    
    You can replace the sample processing by your "Step 2" and activate IpWsSaveAs line with the new file name, which you generate from the original input parameter fileName.

     
  • Hi,


    Here is my Macro,  I am using Image Pro Plus ver. 7.0

    When I ran the Macro  A_flatten_GFAP_Jpeg ( )
    I have to manually select each the avi files and save it as jpeg...
    If you notice my For X  0 to xTime and Next X  this will allow me to process step 1,step 2 ,step 3
    then go back to step 1.   But, I have to do this manually.

    My questions is how do I make it so it can save AUTOMATICALLY as jpeg with the proper avi name
    without me having to rename jpeg each time to make it match the avi file name.

    in step 3
    ret=  IpWsSaveAs("I:\GFAP_Ts1converted video\AVI\N1758-10X-10001_original.jpg","jpg")

     




    Sub A_flatten_GFAP_Jpeg ( )


    Dim X As Integers
    Dim xTime As Integer

    xTime=100

    For X =0 to xTime



    ret = IpTemplateMode(1)
    'step 1 allow me to select and  load the avi files.
    ret = IpWsLoad ("I:\GFAP_Ts1converted video\AVI\N1758-10X-10001_original.avi","avi")




    'step 2 where I ran the Extended Depth of Field to Generate the best focus image

    ret = IpEDFShow(1)
    ret = IpEDFAdd(0)
    ret = IpEDFNew(0)
    ret = IpEDFSet(EDF_NORMALIZE,1,0)
    ret = IpEDFSet(EDF_CRITERIA, EDF_MAX_LOCALCONTRAST,0)
    ret = IpEDFSet(EDF_ORDER,EDF_BOTTOMUP, 0)
    ret = IpEDFCreate(EDF_COMPOSITE)
    ret = IpEDFShow(0)


    'step 3  I have to manually click on SAVE then rename the image to match the avi file name.
    'need help on how to save this jpg automatically without having to rename the image each time.

    ret=  IpWsSaveAs("I:\GFAP_Ts1converted video\AVI\N1758-10X-10001_original.jpg","jpg")



    ret= IpAppCloseAll( )

    Next X


    End Sub


    Can anyone answer this?




  • edited August 2013
    Bill, Did you look at Batch_Process.ipm? Do you understand how it works? Let me know if you can use it and I will show how to create JPG name from AVI name.
  • Hi Yuri,

    It is a bit too complicated.
       
    I am able to run it after I rename the Batch_Process.ipm save it as Batch_Process1.bas from my macro I was able to run the Process Directory( ).  But do not know where to place step 2 and most importantly step 3



    After I changed the path in

    If gDirStart ="" Then
    gDirStart = "C:\" to  "I:\GFAP_Ts1converted video\AVI\" because it contain my avi video files.
    End If



    Now the hard part..where do I place step 2?
     
    ret = IpEDFShow(1)
    ret = IpEDFAdd(0)
    ret = IpEDFNew(0)
    ret = IpEDFSet(EDF_NORMALIZE,1,0)
    ret = IpEDFSet(EDF_CRITERIA, EDF_MAX_LOCALCONTRAST,0)
    ret = IpEDFSet(EDF_ORDER,EDF_BOTTOMUP, 0)
    ret = IpEDFCreate(EDF_COMPOSITE)
    ret = IpEDFShow(0)


    and then the last step3  the saving jpg name with correct avi name



    Can you help me with this?
  • edited August 2013
    Hi Bill,

    The steps 2 and 3 should go to the "DoStuff" function. You have to modify it to be like this:
    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'
        ret = IpEDFShow(1)
        ret = IpEDFAdd(0)
        ret = IpEDFNew(0)
        ret = IpEDFSet(EDF_NORMALIZE,1,0)
        ret = IpEDFSet(EDF_CRITERIA, EDF_MAX_LOCALCONTRAST,0)
        ret = IpEDFSet(EDF_ORDER,EDF_BOTTOMUP, 0)
        ret = IpEDFCreate(EDF_COMPOSITE)
        ret = IpEDFShow(0)
    
        'save it to the same folder with name appending .JPG'
        IpWsSaveAs(fileName + ".jpg","jpg")
    
        ' Clean up temporary image'
        ret = IpDocClose()
    End Sub
    

    Note, that the result JPG file will be written to the same folder as AVI, so you have to set file mask in ProcessDirectory function to open only *.AVI files:

            ' Look for standard files, no directories or system files.'
            ' See the 'Dir' command help for details'
            fName = Dir(gDirStart + "*.AVI", 32)
    

    Regards,

    Yuri
  • Hi Yuri,

    I would like to take this Automation to next level if possible.   Instead having to change the name of the folders. manually,  I noticed the Batch_Process.ibm also contain Process_Sub_Directory function.  


    My question is that what to change on Process_sub_Directory function so it can walk through all the sub folders that also contain AVI files? and performed Step 1,Step 2, Step 3. 


    What do I have to do to make Process_sub_Directory function call DoStuff function? so DoStuff  function can performed Step 2 (processing the AVI files) and Step 3 (save as the jpeg in each sub folders) 

     

     I would like to use process_sub_directory function, but not sure how to make it to call DoStuff function, so Do Stuff function can performed step 2, step 3. 



    How do I make the Process_Sub_Directory function to call Do stuff
    Where Do Stuff can performed Step 2, Step3?



    My avi videos are stored in one directory I:\GFAP_Ts1converted video\AVI\  in side this path
    contain many subfolders 

     
    for example
    N100 folder
    N101 folder
    N102 folder

    inside these folders contain more AVI videos.


    Can you answer this questions?  How to make Process_Sub_directory run like Process_directory function? By run through the subfolder to automatically? So, I do not have to manually changed the name of the folder to get to next folder.
  • edited August 2013
    Hi Bill,

    You have already found that the macro script contains ProcessSubDir, so if you want to process all sub-folders recursively you have to use this sub instead of ProcessDirectory. Just set the right start folder in ProcessDirectory (or select it interactively) and limit the processing to only AVI files in RecursiveSub:
         ...
         Do While DStat > 0
            X = X + 1
            Select Case DStat
            Case 1
                'check if it is an AVI'
                shortName = LCase(Right(IpTrim(Iname), 3))
                If shortName="avi" Then
                    ' avi file'
                    ' load it...'
                    docID = IpWsLoad(Iname, "")
                    If docID >= 0 Then
                        Debug.Print "Processing: "; Iname
    
                        ' Processing code goes here ***'
                        DoStuff docID, IpTrim(Iname)
    
                        ' Clean up...'
                        ret = IpAppSelectDoc(docID)
                        ret = IpDocClose()
                    End If
                End If
            Case 2
                ' Directory. Ignore "\." and "\..", as they'
    	...
    Regards,

    Yuri
Sign In or Register to comment.