Jump to content

Cmd to autoit conversion


memerim
 Share

Recommended Posts

Trying convert this command line code to autoit, but i getting a value of "1" from msgbox, while i should receive 1955.72 (0.0298424)

Follow:

image.thumb.png.db267577cc7ecf5d0702c5a64b324991.pngT

My code:

$iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\convert.exe'
        $D = ShellExecuteWait($iC, 'compare "C:\Users\CAIO\Downloads\x.bmp" "C:\Users\CAIO\Downloads\y.bmp" -metric RMSE -subimage-search', "", "", @SW_HIDE)
        MsgBox(0,"",$D)
Link to comment
Share on other sites

I see the value now.  Just not feeling well today.    According to shell execute the return value is the exit code for whatever was executed.   Probably the only accepted method for one program to communicate is via some kinda control read.  Or possibly a shared file.  If the value is in the text on the screen check the info tool its possible to get the text using some kinda control command I believe.   Not 100% sure but I think contrlread() can possibly get it if its available to read.

Just a fast tip any function you run that your unfamiliar with check out the help file.   Literally everything you need to know

Edited by markyrocks
Link to comment
Share on other sites

Something going wrong, i have tried export the value to a .txt, using cmd it worked, but in shellexecut dont:

$iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\convert.exe'
        $D = ShellExecuteWait($iC, 'compare "C:\Users\CAIO\Downloads\x.bmp" "C:\Users\CAIO\Downloads\y.bmp" -metric RMSE -subimage-search 2>diff.txt', "", "", @SW_HIDE)
        MsgBox(0,"",$D)

image.png.6f7b3c3d2cea4bf2de13ab881a808fe6.png

 I also have tried:

$iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\convert.exe'
        $X = 'compare "C:\Users\CAIO\Downloads\x.bmp" "C:\Users\CAIO\Downloads\y.bmp" -metric RMSE -subimage-search 2>diff.txt'
        $D = ShellExecuteWait($iC, $X, "", "", @SW_HIDE)
        ConsoleWrite($D)  ;; MsgBox(0,"",$D)

 

Edited by memerim
Link to comment
Share on other sites

Sry,  use .tmp.  they seem to be more friendly if you must use a file.  But I apologize for earlier if the info tool shows the text on the window you can get it in controlgettext().

In your script it doesn't show any file read.  Once you make the file us fileopen and _readfiletoarray()  and sort through it. Use stringinstr() to see if what your looking for is in there.  If true then do whatever you have to do to get the data out.  Control get text is probably the easier route.   If that option is available 

Edited by markyrocks
Link to comment
Share on other sites

.tmp also dont worked, about controlgettext wouldnt help, those command are from a software ImageMagick, the commands are to compare and get difference from img, i will use it in a loop to compare many images, and when img found continue, so each ms will cost.

Using the commands direclty on cmd everything is working fine 🤔

Link to comment
Share on other sites

why do you need to do the comparison in the command prompt can't you just do whatever comparison from the download folder and then do whatever you need to do in the cmd prompt after the fact?

 

 

 

maybe this will help as well

 

https://www.autoitscript.com/autoit3/docs/functions/StdoutRead.htm

Edited by markyrocks
Link to comment
Share on other sites

#RequireAdmin

        $iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\convert.exe'

        $Commandline = "magick compare C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search 2>diff.tmp"
        $D = ShellExecuteWait($iC, $Commandline, "", "", @SW_HIDE)
        ConsoleWrite("D = "&$D&@CRLF)  ;; MsgBox(0,"",$D)

        $Commandline2 = "magick compare C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"

        $Z = RunWait(@ComSpec & " /C " & $iC & $Commandline2 & " > C:\output.txt")
        ConsoleWrite("Z = "&$Z&@CRLF)

Console:

D = 1
Z = 1

And i got a blank output.txt 😓

Link to comment
Share on other sites

#include <File.au3>
    #include <Array.au3>
    _filereadtoarray($filepath,$sReturn)
    If isarray($sReturn) then 
    _arraydisplay($sReturn)
    Else 
    Exit
    Endif
    For $x=1 to ubound($sReturn)-1
    if stringinstr($sReturn[$x],"whatever") then
    ;do whatever
    endif
    Next

....untested but it really doesn't get any easier than that 

 

edit sry edit edit... 

Edited by markyrocks
Link to comment
Share on other sites

8 minutes ago, memerim said:

I need run the code by command line to know the % between two images, based on % continue the rest of my code.

I dont understand how this code you post could help.

Bc it reads the output file...I'm assuming that the output file is created by whatever program you're using.   

 

What does the screen look like?

Maybe not do this with the window hidden until it works.  

Edited by markyrocks
Link to comment
Share on other sites

This is the program folder:

Spoiler

image.thumb.png.4d5137dfc5afde13b17029b1d5d9a933.png

 

As the code in cmd runs good with:

image.thumb.png.64f446b9f73668b826d1ab34344e4dda.png

 

I have changed the code to:

$iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\compare'

instead \convert

$iC     = 'C:\Program Files\ImageMagick-7.0.9-Q16\compare'

        $Commandline = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search 2>diff.txt"
        $D = ShellExecuteWait($iC, $Commandline, "", "", @SW_HIDE)
        ConsoleWrite("D = "&$D&@CRLF)

 

Now instead receive 1 on console i receive 2, what it means?

@markyrocks if you could download imagemagick heres the link:

 

 

Then just add two image same size on download folder and run the command in cmd

"compare C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"

 

I have found this comment if it can help in something:

image.png.ba67d6c9b4b981adfe3a869e7ed31f7a.png

Edited by memerim
Link to comment
Share on other sites

ShellExecuteWait returns the exit code of the program. You know.. like the number between 0 and 255 that says if it errored or not.

You read stdout like this:

#include <AutoItConstants.au3>
Opt("ExpandVarStrings", 1)

Local $pid = Run("@ComSpec@ /C echo test", "", Default, $STDOUT_CHILD)
ProcessWaitClose($pid)
Local $out = StdoutRead($pid)
StdioClose($pid)
ConsoleWrite($out); test

Check the docs for StdoutRead.

Link to comment
Share on other sites

$iC     = "C:\Program Files\ImageMagick-7.0.9-Q16\compare.exe"
        $Commandline2 = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"
        Local $pid = RunWait(@ComSpec & " /C " & $iC & $Commandline2, "", Default, $STDOUT_CHILD)


        ProcessWaitClose($pid)
        Local $out = StdoutRead($pid)
        StdioClose($pid)
        ConsoleWrite($out); test

Output: 'C:\program' is not recognized as an internal or external command

 

There are some topics about it, and:

@Jos

image.png.f20f2f75fb8e0b5ba9541a14ffec47ea.png

 

I have latest Scite:

SciTE
32-bit
Version 4.2.0 
Sep  3 2019 19:04:05

 

Btw, i have uninstalled ImageMagick and reinstalled it in "C:\Users\CAIO\Downloads\" and saved the folder just as ImageMagick.

Code:

$iC     = "C:\Users\CAIO\Downloads\ImageMagick\compare.exe"
        $Commandline2 = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"
        Local $pid = RunWait(@ComSpec & " /C " & $iC & $Commandline2, "", Default, $STDOUT_CHILD)


        ProcessWaitClose($pid)
        Local $out = StdoutRead($pid)
        StdioClose($pid)
        ConsoleWrite($out); test

Now i have a different output on console.  (The command is working correctly using CMD and the new path/name)

sOut = The filename, directory name, or volume label syntax is incorrect.

 

I also tried put the autoit script inside the ImageMagick folder and run $iC as 

$iC     = @ScriptDir & "\compare"
        $Commandline2 = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"
        Local $pid = RunWait(@ComSpec & " /C " & $iC & $Commandline2, "", Default, $STDOUT_CHILD)


        ProcessWaitClose($pid)
        Local $out = StdoutRead($pid)
        StdioClose($pid)
        ConsoleWrite($out); test

I got same output "The filename, directory name, or volume label syntax is incorrect."

 

 

 

 

 

 

------------EDIT------------

Finally i found the problem 😡

It was just a space in the command after "

This:

$Commandline2 = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"

To:

$Commandline2 = " C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"

 

Edited by memerim
Link to comment
Share on other sites

You run $iC & $Commandline2 however you didn't include a space, use either $iC & " " & $Commandline2 or $iC = @ScriptDir & "\compare ".  The best way to test is to always print the command first using ConsoleWrite or InputBox, you can then run the command manually in Cmd to see if the syntax is correct, example: 

ConsoleWrite($iC & $Commandline2 & @CRLF)
Run(...
;~ Or if you include #RequireAdmin in your script (since you can't see ConsoleWrite entries)
InputBox("", "", $iC & $Commandline2)
Run(...

 

Link to comment
Share on other sites

#RequireAdmin
        #include <WinAPIProc.au3>
        #include <AutoItConstants.au3>
        Opt("ExpandVarStrings", 1)


        $t1 = TimerInit()

        $iC     = @ScriptDir & "\compare"
        $Commandline2 = "C:\Users\CAIO\Downloads\x.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"
        $pid = RunWait(@ComSpec & " /C " & $iC & " " & $Commandline2, "", Default, $STDOUT_CHILD)


        ProcessWaitClose($pid)
        $out = StdoutRead($pid)
        StdioClose($pid)
        ConsoleWrite($out&@CRLF); test

        $t2 = TimerDiff($t1)
        ConsoleWrite("Time = "&$t2&@CRLF)

        ;
        If StringInStr($out, "(") Then $SPLIT = StringSplit($out, "(")
        ConsoleWrite("SPLIT = "&$SPLIT&@CRLF)

        For $i = 1 To $SPLIT[0]

            If $i = 1 Then $SPLITING = $SPLIT[$i]
            If $i = 2 Then $SPLITING2 = $SPLIT[$i]
            ConsoleWrite($SPLit&@CRLF)
        Next

Took 139.ms to compare just 1 image, and 1 sec to compare 5, any possibility otimizing the script faster? as ill compare a bunch of images each time to then continue script depending on value.

How can i run the command line hidden as it have $STDOUT_CHILD i cant use @SW_HIDE 🤔

My $out output is = 1955.72 (0.0298424)

I tried split the variable to remove things inside (       ), but it dont recognize the variable $out containing any (

 

Edited by memerim
Link to comment
Share on other sites

#RequireAdmin
        #include <WinAPIProc.au3>
        #include <AutoItConstants.au3>
        Opt("ExpandVarStrings", 1)



        $t1 = TimerInit()
        $iC     = @ScriptDir & "\compare"
        $X = 0
        While 1

        $X = $X + 1
        $Commandline2 = "C:\Users\CAIO\Downloads\'&$X&'.bmp C:\Users\CAIO\Downloads\y.bmp -metric RMSE -subimage-search"
        $pid = RunWait(@ComSpec & " /C " & $iC & " " & $Commandline2, "",  @SW_HIDE, $STDOUT_CHILD)


        ;ProcessWaitClose($pid)
        $out = StdoutRead($pid)
        StdioClose($pid)


        $Y = StringLeft($out, StringInStr($out, " (", 0, 1) - 1)
        ConsoleWrite($X&" = "&$Y&@CRLF); test

        If $X = 5 Then ExitLoop

        WEnd

        $t2 = TimerDiff($t1)
        ConsoleWrite("Time = "&$t2&@CRLF)

My output continues messed up:

1955.72 (0.0298424)1 = 
1955.72 (0.0298424)2 = 
3 = 
1955.72 (0.0298424)4 = 
1955.72 (0.0298424)5 = 
Time = 554.1816
1955.72 (0.0298424)+>13:47:08 AutoIt3.exe ended.rc:0

Time = 554.1816
1955.72 (0.0298424)

The code wrote things of $Y after $Time whos on the end 😵

 

I have run

$Y = StringInStr($out, "(", 0, 1, 1)
        ConsoleWrite("Y = "&$Y&@CRLF)

And its returning 0

Edited by memerim
Link to comment
Share on other sites

$sString = "1234.67 (0.123456)1 ="

We need to get the first 8 characters of the string if we know it was always going to be 8 we could just use:

StringLeft($sString, 8 )

However if the " (" could be at different position then we use

StringInStr($sString, " (", 0, 1) which will returns the position of " (" in the $out string for example it should return 9.

To remove the extra character we use - 1

As I mentioned #RequireAdmin doesn't write to console so unsure how you're getting any results with ConsoleWrite try the following instead:

#RequireAdmin
#include <WinAPIProc.au3>
#include <AutoItConstants.au3>
Opt("ExpandVarStrings", 1)
Local $hTimer = TimerInit()
Local $sCompare = @ScriptDir & "\compare"
Local $sCommand, $sResult = "", $iPID
For $i = 1 to 5
    $sResult = ""
    $sCommand = '"C:\Users\CAIO\Downloads\' & $i & 'x.bmp" "C:\Users\CAIO\Downloads\' & $i & 'y.bmp" -metric RMSE -subimage-search'
    $iPID = Run(@ComSpec & " /c " & $sCompare & " " & $sCommand, "",  @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($iPID)
    $sResult = StdoutRead($iPID)
    StdioClose($iPID)
    $sResult = StringStripWS($sResult, 3)
    $sResult = StringLeft($sResult, StringInStr($sResult, " (", 0, 1) - 1)
    MsgBox(4096, "Result", $i & " = " & $sResult, 5)
Next
Local $fTimerDiff = TimerDiff($hTimer)
MsgBox(4096, "Timer", "Time = " & $fTimerDiff)

 

Link to comment
Share on other sites

The boxs are poppin blank and even changing msgbox to consolewrite the diff time changed from 500ms to 1793

Spoiler
#RequireAdmin
#include <WinAPIProc.au3>
#include <AutoItConstants.au3>
Opt("ExpandVarStrings", 1)
Local $hTimer = TimerInit()
Local $sCompare = @ScriptDir & "\compare"
Local $sCommand, $sResult = "", $iPID
For $i = 1 to 5
    $sResult = ""
    $sCommand = '"C:\Users\CAIO\Downloads\' & $i & '.bmp" "C:\Users\CAIO\Downloads\y.bmp" -metric RMSE -subimage-search'
    $iPID = Run(@ComSpec & " /c " & $sCompare & " " & $sCommand, "",  @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($iPID)
    $sResult = StdoutRead($iPID)
    StdioClose($iPID)
    $sResult = StringStripWS($sResult, 3)
    $sResult = StringLeft($sResult, StringInStr($sResult, " (", 0, 1) - 1)
    MsgBox(4096, "Result", $i & " = " & $sResult, 5)
Next
Local $fTimerDiff = TimerDiff($hTimer)
MsgBox(4096, "Timer", "Time = " & $fTimerDiff)

 

image.thumb.png.fc6571e41d984edae962cd78bf382db7.png     

Spoiler

 

#RequireAdmin
#include <WinAPIProc.au3>
#include <AutoItConstants.au3>
Opt("ExpandVarStrings", 1)
Local $hTimer = TimerInit()
Local $sCompare = @ScriptDir & "\compare"
Local $sCommand, $sResult = "", $iPID
For $i = 1 to 5
    $sResult = ""
    $sCommand = '"C:\Users\CAIO\Downloads\' & $i & '.bmp" "C:\Users\CAIO\Downloads\y.bmp" -metric RMSE -subimage-search'
    $iPID = Run(@ComSpec & " /c " & $sCompare & " " & $sCommand, "",  @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($iPID)
    $sResult = StdoutRead($iPID)
    StdioClose($iPID)
    $sResult = StringStripWS($sResult, 3)
    $sResult = StringLeft($sResult, StringInStr($sResult, " (", 0, 1) - 1)
    MsgBox(4096, "Result", $i & " = " & $sResult, 5)
Next
Local $fTimerDiff = TimerDiff($hTimer)
MsgBox(4096, "Timer", "Time = " & $fTimerDiff)

 

image.thumb.png.d67353f66faa7192d9f7bb9b189fb31b.png

.

I have tried store the value to a ini, but even on ini it stored nothing:

$out = StdoutRead($pid)
        StdioClose($pid)
        IniWrite("C:\Users\CAIO\Downloads\TEST.ini", "Value", $X, $out)

Ini:

image.png.26460ab525d5c01eeec69ffaf3395e0a.png

😡

Edited by memerim
Link to comment
Share on other sites

You need to debug your command line, use the following to test the command line manually within cmd.exe

#RequireAdmin
#include <WinAPIProc.au3>
#include <AutoItConstants.au3>
Opt("ExpandVarStrings", 1)
Local $hTimer = TimerInit()
Local $sCompare = @ScriptDir & "\compare"
Local $sCommand, $sResult = "", $iPID
Run(@ComSpec)
For $i = 1 to 5
    $sResult = ""
    $sCommand = '"C:\Users\CAIO\Downloads\' & $i & '.bmp" "C:\Users\CAIO\Downloads\' & $i & 'y.bmp" -metric RMSE -subimage-search'
    InputBox("Test Command", "Copy and Paste the contents into Cmd and see what results you get:", $sCompare & " " & $sCommand, "", 768, 120)
    $iPID = Run(@ComSpec & " /c " & $sCompare & " " & $sCommand, "",  @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($iPID)
    $sResult = StdoutRead($iPID)
    StdioClose($iPID)
    $sResult = StringStripWS($sResult, 3)
    $sResult = StringLeft($sResult, StringInStr($sResult, " (", 0, 1) - 1)
    MsgBox(4096, "Result", $i & " = " & $sResult, 5)
Next
Local $fTimerDiff = TimerDiff($hTimer)
MsgBox(4096, "Timer", "Time = " & $fTimerDiff)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...