Jump to content

ImageSearch Usage Explanation


Centrally
 Share

Recommended Posts

When I have this part of the code:

local $search = _ImageSearchArea('check5.bmp', 1, 800, 40, 900, 80, $x, $y, 0)

Does the first parameter dictate the tolerance? In the code above that would be "1" or how do I dictate the tolerance as I have an image which is semi transparent which I would like to find no matter what screen is in the background.

Link to comment
Share on other sites

  • 2 weeks later...

I was trying to write a script that could check several areas searching for a specific BMP at the same time but I don't know how to use more than one imagesearcharea. Sorry for this, I'm a noob. Any help would be appreciated. Thanks

Link to comment
Share on other sites

Hmm not sure you can, it follows a pattern of search. Experts chime in pls.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

I have written a simple code and it's working great. Thanks for the great library. Here are a snippet -

WinActivate('My Window')

$x = 0
$y = 0
If _ImageSearch('searchButton.PNG', 1, $x, $y, 20) = 1 Then
   Return 1
ElseIf _ImageSearch('bundle.PNG', 1, $x, $y, 20) = 1 Then
   Send('{ESC}{LCTRL}{LCTRL}{LCTRL}')
   sleep(1000)
Else
   Send('{LCTRL}')
EndIf

Now my question is how can I use that _ImageSerach function in a specific program (background process) so that I can use my pc to do other work?

I know how to use control commands to click on background but I can't use _ImageSerach in background.

Please help.

Link to comment
Share on other sites

Because, i believe, it doesn't work in the background, you have to be able to see the image yourself, in order for the code to work.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

6 hours ago, smahmud said:

Now my question is how can I use that _ImageSerach function in a specific program (background process) so that I can use my pc to do other work?

I know how to use control commands to click on background but I can't use _ImageSerach in background.

It takes a screenshot and finds the image you requested!
So it does not work with hidden windows or background processes!

Regards,
 

Link to comment
Share on other sites

Thanks for the reply :)

I guess only way to do is screen capture on specific window and run search on that picture but I am not sure if there any function exists to do that.

One more question: How can i look for multiple occurrence of same image and return all the cords?

Thanks.

Link to comment
Share on other sites

On 7/2/2017 at 3:13 PM, MazeM said:

Then there's still a bug in the code, you cannot use black as its color value is zero, and only non-zero is accepted as transparent:

If $transparency <> 0 Then $findImage = "*" & $transparency & " " & $findImage

 

I had to change the ImageFile parameter when calling the DLL function to get transparency to work. It was missing the correct prefix for the transparency option.

For reference, the Image file options are here: https://autohotkey.com/docs/commands/ImageSearch.htm

*TransN: This option makes it easier to find a match by specifying one color within the image that will match any color on the screen. 

This is my updated _ImageSearchArea function with the transparency option fixed. Notice the inclusion of the *Trans text in the ImageFile parameter parameters.

If $transparency <> 0 Then $ImageFileOptions = "*Trans" & Hex($transparency) & " " & $ImageFileOptions

Func _ImageSearchArea($findImage, $resultPosition, $x1, $y1, $right, $bottom, ByRef $x, ByRef $y, $tolerance = 0, $transparency = 0);Credits to Sven for the Transparency addition
    If Not FileExists($findImage) Then Return "Image File not found"
    If $tolerance < 0 Or $tolerance > 255 Then $tolerance = 0
    If $h_ImageSearchDLL = -1 Then _ImageSearchStartup()

   Local $ImageFileOptions = ""
   If $tolerance > 0 Then $findImage = "*" & $tolerance & " " & $ImageFileOptions
   If $transparency <> 0 Then $ImageFileOptions = "*Trans" & Hex($transparency) & " " & $ImageFileOptions
   $ImageFileOptions = $ImageFileOptions & $findImage

   $result = DllCall($h_ImageSearchDLL, "str", "ImageSearch", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $ImageFileOptions)
   If @error Then Return "DllCall Error=" & @error
   If $result = "0" Or Not IsArray($result) Or $result[0] = "0" Then Return False

    $array = StringSplit($result[0], "|")
    If (UBound($array) >= 4) Then
        $x = Int(Number($array[2])); Get the x,y location of the match
        $y = Int(Number($array[3]))
        If $resultPosition = 1 Then
            $x = $x + Int(Number($array[4]) / 2); Account for the size of the image to compute the centre of search
            $y = $y + Int(Number($array[5]) / 2)
        EndIf
        Return True
    EndIf
EndFunc   ;==>_ImageSearchArea

 

Link to comment
Share on other sites

  • 3 months later...

Hi guys!

 

So I´ve been playing around with AutoIt again after a couple of years of absence. I'm trying to make ImageSearch work in my favor and got a test script going that looks like this: 

#include <ImageSearch2015.au3>



Local $findImage[8], $resultPosition, $tolerance, $transparency, $waitSecs, $x, $y

$findImage[0] = 7
$findImage[1] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\HK.png"
$findImage[2] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\K4.png"
$findImage[3] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\K5.png"
$findImage[4] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\R8.png"
$findImage[5] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\S7.png"
$findImage[6] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\S8.png"
$findImage[7] = "C:\Users\Micke\Desktop\AutoIt Script\ImageSearch2015\SJ.png"

$x=0
$y=0
$waitSecs = 1

  $findImage =  _WaitForImagesSearch($findImage, $waitSecs, $resultPosition, $x, $y, $tolerance, $transparency = 0)
ConsoleWrite($findImage & @CRLF)

It works but kind of slow(2.5 s) and the area I want to search for those .png's is much smaller than my desktop, so my question is how would I go about setting that smaller area up in my script?

Link to comment
Share on other sites

  • 4 weeks later...
  • Moderators

Yes

Spoiler

Sorry, not enough info? Perhaps if you gave more detail about what you're trying to do you'd get more in return ;)

 

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

1 hour ago, JLogan3o13 said:

Yes

  Hide contents

Sorry, not enough info? Perhaps if you gave more detail about what you're trying to do you'd get more in return ;)

 

Wow great and easy. If someone  will search it

 

#AutoIt3Wrapper_Res_File_Add=img\search.bmp, rt_bitmap, TEST_BMP_1
Global $x, $y
Global $pic1
; set BMP image to picture control from resource  bitmap
_ResourceSetImageToCtrl($pic1, "TEST_BMP_1", $RT_BITMAP)

Case $imageSearch
            Local $find = _ImageSearch($pic1,0,$x,$y,0)
            If $find Then
                MouseMove($x,$y,10)
            Else
                MsgBox(0,"","Not Found")
            EndIf

 

Link to comment
Share on other sites

  • 3 months later...

Guys i have a problem i have at the same folder the ImageSearch.au3 file and the dll and also my new script and when i make the imagesearch at my main script its not working i have made the include at the start of my sciprt whats wrong?

Link to comment
Share on other sites

  • Developers
28 minutes ago, TolisX said:

whats wrong?

you didn't post the script so we either do a wild guess or you share enough information to a a proper look. :)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

That's not a script, it's a picture of a script. Kind of like taking a picture of an apple doesn't feed you, giving us code in a picture doesn't help us help you.

The only thing I can guess is that you're trying to use the 64 bit image search, and running it as an x86 process. Due to the naming you're giving things and the fact that the x86 version of AutoIt is being executed.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • 1 month later...

UDF has

#include-once at the top

If i include entire UDF (well, the main function of it) in my main script, what difference does it make ?.

I would end up with this.

 

#include "GDIPlus.au3";Required to get ImageSearch to function. Comes with AutoIT
Global $iX, $iY;Make these variables global so we can reuse them after finding an image, instead of creating new variables for that purpose.
Global $xCenter, $yCenter ;these are the final result of the CENTER Of the found image
Do
    Sleep(300)
Until _FindImage("Example.jpg")
MsgBox(0, "Example", $xCenter & @CRLF & $yCenter)
MouseMove ($xCenter ,$yCenter) ;Mov mouse to the center of found image
Func _FindImage($sImgPath);the function that searches

    ;Initialize and prepare ImageSearch.
    _GDIPlus_Startup();Initialize Microsoft Windows GDI+.
    Local $hImage = _GDIPlus_ImageLoadFromFile($sImgPath);Create an image object based on a file.
    Local $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage);Create a handle to a bitmap from a bitmap object.

    ;Search for Image.
    $iRet = _ImageSearch($hHBmp, 1, $iX, $iY, 10) ; Default=5: Tolerance, if it does not work for you rise it (up to 255)

    ;Begin Cleanup.
    _WinAPI_DeleteObject($hHBmp);Deletes a logical pen, brush, font, bitmap, region, or palette.
    _GDIPlus_ImageDispose($hImage);Release an image object.
    _GDIPlus_Shutdown();Clean up resources used by Microsoft Windows GDI+.

    ;Report Result.
    Return $iRet;Will return 1 if image was found. If no image was found, this will return 0.

EndFunc   ;==>_FindImage

Func _ImageSearch($findImage,$resultPosition,$x,$y,$tolerance, $HBMP=0)
   return _ImageSearchArea($findImage,$resultPosition,0,0,@DesktopWidth,@DesktopHeight,$x,$y,$tolerance,$HBMP)
EndFunc

Func _ImageSearchArea($findImage,$resultPosition,$x1,$y1,$right,$bottom,$x,$y, $tolerance,$HBMP=0)

    If IsString($findImage) Then
        if $tolerance>0 then $findImage = "*" & $tolerance & " " & $findImage
        If $HBMP = 0 Then
            $result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$findImage)
        Else
            $result = DllCall("ImageSearchDLL.dll","str","ImageSearchEx","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$findImage,"ptr",$HBMP)
        EndIf
    Else
        $result = DllCall("ImageSearchDLL.dll","str","ImageSearchExt","int",$x1,"int",$y1,"int",$right,"int",$bottom, "int",$tolerance, "ptr",$findImage,"ptr",$HBMP)
    EndIf

    ; If error exit
    if $result[0]="0" then return 0

    ; Otherwise get the x,y location of the match and the size of the image to
    ; compute the centre of search
    $array = StringSplit($result[0],"|")
   $x=Int(Number($array[2])) ;this is x most left
   $y=Int(Number($array[3])) ;This is y most high
   if $resultPosition=1 then
      $xCenter=$x + Int(Number($array[4])/2) ;x center
      $yCenter=$y + Int(Number($array[5])/2) ;y center
   endif
   return 1
EndFunc

 

Is there something i need to worry about when running this code like that in a loop ?
My main script will continuously look for that image until all its instances are gone.

I am trying to click off inventory items automatically.

 

Link to comment
Share on other sites

  • 5 months later...

Hello Centrally,

I tried using your code above with ImageSearch.au3 in Windows 10 x64 and it just hangs; see my code below, are you able to assist?, thank you.

#include <ImageSearch.au3>

HotKeySet("p", "checkForImage")

global $y = 0, $x = 0

Func checkForImage()
local $search = _ImageSearchArea('d:\autoit\Test.bmp', 1, 800, 40, 900, 80, $x, $y, 0)
If $search = 1 Then
MouseMove($x, $y, 20)
EndIf
EndFunc

while 1
sleep(200)
WEnd

Link to comment
Share on other sites

 The function has one more parameter, bitmap in the end, can be set to 0 try adding it like this.

_ImageSearchArea('d:\autoit\Test.bmp', 1, 800, 40, 900, 80, $x, $y, 0, 0) 

;Func _ImageSearchArea($findImage,$resultPosition,$x1,$y1,$right,$bottom, ByRef $x, ByRef $y, $tolerance,$HBMP=0)

Searching within a rectangle with 100x40.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

  • 1 month later...

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

×
×
  • Create New...