Jump to content
Centrally

ImageSearch Usage Explanation

Recommended Posts

20 minutes ago, dont_panic said:

Link for ImageSearch15.au3 is not working!

Additionally, the ImageSearch2015.au3 has a lot of problems...
In Win 7 64bit it says it needs 32 bit.

But no matter if I put the 32 bit dll or the 64 bit dll or both in the folder, it is not working.

Share this post


Link to post
Share on other sites

I have made my own solution for image searching. I would like to share here. Im from spain (if someone of you can notice a bad grammar, etc):

My idea is to make an aproximation to the problem, its not perfect but its usefull if you take 1 or 2 tips while using my system:

a] You should try to detect images with the upper left corner in a pixel that not has the same color in the background or in big areas (this is for speed reasons).

b] Sometimes, in order to set a] tip properly, a trick is to detect an image, lets name it "AUX", that "AUX" verify a] tip, and then, when you get the coordinates of that image with my image detection system, the only thing you need is to apply an offset to that point, exactly the offset to transform that point into the right one, this is, the coordinates of original "TARGET" image. Obviusly this is only valid when the relatives positions between AUX and TARGET are fixed and both of them exists always togheter in the screen.

Anyway, the advantages of my system are the following:

- You dont need any DLL file or library.

- You dont need to deal with extrange problems or bugs mentioned here around the imageSearch posted in this thread.

- Your dont need to put a copy of the image file you are trying to detect, neather distribute this file with the script .au3 file.

- It doesnt matter the size/resolution of the image, because my algorithm is O(1) efficient in this aspect, i mean, the image size not affect to the speed detection.

Even with this advantages... as i said, its not at all a perfect system, because it has the limitation mentioned above, in a] point. I should say that i have make my efforts in every part of the algorithm, i mean, even if you dont take a] point seriously, the system should be as fast as is possible.

All i use from AutoIt library is PixelGetColor and PixelSearch (as you can imagine, specially this one).

If someone of you are interested in this idea, i can attach here the files, so you can freely dowload, and i will put here also some examples of using this idea, all you wiil need to use the system are these 3 things:

-1 Your script file with the image file you want to detect (you will be able to delete the image when all is set up)

-2 My image_search.au3 to import in your script (so you can use my func image_search(), that returns the center point of the image).

-3 A little Java app that i provide also and i have made specially to make it easy the use of the system.

The idea behind this Java app is simple: it takes any image file and generates an .au3 script with only one function, named "func get_<image_name>_<widthxheight>_sign()". This new function, makes a simple signature from the image, a kind of "footprint". This signature has always the same size, thats why the image size doesnt matter. And that signature is what my search in the screen. Then, the scan itself is "optimized" by using PixelSearch in a certain way (not the best way, but i have tried).

Anyway, i will put the codes and samples here if there are people with the desire.

Cheers.

PD: Or maybe i could make a youtube video and share here as a little tutorial or something, but i cant garantee this.

 

Share this post


Link to post
Share on other sites

Hello again,

Please see this new post: 

If you are interested in a new image detection solution.

I have used a little bit better english, (better than these posts).

Thank you and enjoy!

Edited by Lasker

Share this post


Link to post
Share on other sites

Hey OP can you take a look at why this thing won't work?

 

I try the code in your example (everything is in place, the files (either 32 or 64 none of them works same error))
 

#include <ImageSearch.au3>

HotKeySet("p", "checkForImage")

global $y = 0, $x = 0

Func checkForImage()
Local $search = _ImageSearch('checkImage.bmp', 0, $x, $y, 0)
If $search = 1 Then
MouseMove($x, $y, 10)
EndIf
EndFunc

while 1
sleep(200)
WEnd

Then I get this error:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\w1r3dh4ck3r\Desktop\ImageSearch\Search.au3"    
"C:\Program Files (x86)\AutoIt3\Include\ImageSearch.au3" (44) : ==> Subscript used on non-accessible variable.:
if $result[0]="0" then return 0
if $result^ ERROR
>Exit code: 1    Time: 4.889

Please help me out?

Share this post


Link to post
Share on other sites

I have the same error, spend hours on this and cant fix it

"C:\Program Files (x86)\AutoIt3\Include\ImageSearch.au3" (44) : ==> Subscript used on non-accessible variable.:
If $result[0] = "0" Then Return 0
If $result^ ERROR
->14:47:19 AutoIt3.exe ended.rc:1
+>14:47:19 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 4.602

Share this post


Link to post
Share on other sites

Works fine here, where are you using "$result" var?


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

Share this post


Link to post
Share on other sites

How to find multiple same pictures?

I tried with loops but it always give me the same one picture _ImageSearch returns 0 or 1 and i dont know exactly how to achieve this.

Maybe there is possibility to $marker will be an array

Thx for help

Func find_img()

    Local $yy = 0, $xx = 0
    Local $i = 1
    Local $marker

    Do
        $marker = _ImageSearch('img/map_marker.bmp', 0, $xx, $yy, 0)
        MsgBox("","","Found "&$i&" Position -"&$xx&" "&$yy)
        $i = $i + 1
    Until $marker <> 0

EndFunc

 

 

Share this post


Link to post
Share on other sites

Maybe _ImageSearchArea in 4 "quadrants" or more, know what i mean?


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

Share this post


Link to post
Share on other sites
22 hours ago, Miliardsto said:

How to find multiple same pictures?

I tried with loops but it always give me the same one picture _ImageSearch returns 0 or 1 and i dont know exactly how to achieve this.

Maybe there is possibility to $marker will be an array

Thx for help

Func find_img()

    Local $yy = 0, $xx = 0
    Local $i = 1
    Local $marker

    Do
        $marker = _ImageSearch('img/map_marker.bmp', 0, $xx, $yy, 0)
        MsgBox("","","Found "&$i&" Position -"&$xx&" "&$yy)
        $i = $i + 1
    Until $marker <> 0

EndFunc

 

You're going in the right direction I think by how you are considering usage of your $marker variabl.

The main thing you need to do is use the Returned Data in $marker from the _ImageSearch function to change the starting points for the next search on every loop after every previous result.

Only on the first search do you want the search to begin at 0,0 which are your $yy and $$xx variables.
You want to update those variables after every previous result.

You're gonna get in twouble here if that's game automation related, tss tss tss

Don't have the time right now to write or test any actual script for you but I hope the idea is clearer now :)

Share this post


Link to post
Share on other sites
1 hour ago, guestscripter said:

You're going in the right direction I think by how you are considering usage of your $marker variabl.

The main thing you need to do is use the Returned Data in $marker from the _ImageSearch function to change the starting points for the next search on every loop after every previous result.

Only on the first search do you want the search to begin at 0,0 which are your $yy and $$xx variables.
You want to update those variables after every previous result.

You're gonna get in twouble here if that's game automation related, tss tss tss

Don't have the time right now to write or test any actual script for you but I hope the idea is clearer now :)

I have done something like above. I have in screen 3  same pics - map_marker.bmp but function gives me always 1 to 10 elements because indicates to this same one picture. :/

Is it possible to define $xx parameter must be different from for example 1212? then I will make another array and put there values of $xx.

How to solve this xd

Func walker()
    Local $marker[10]
    Do
        $yy = 0
        $xx = 0
        For $i = 0 to 10
            $val = _ImageSearch('img/map_marker.bmp', 0, $xx, $yy, 0)
            _ArrayInsert($marker, $i, $val)
            MsgBox("","",$val&" Position -"&$xx&" "&$yy)
        Next
    Until $marker <> 0
     _ArrayDisplay($marker, "display")
EndFunc

 

Share this post


Link to post
Share on other sites
On 10/09/2016 at 9:23 AM, Miliardsto said:

Dont know what u mean. It returns me all time same picture.

What i mean is, divide the screen in 4 parts, for example, and loop through them, may be 4 or 8 or whatever.

Obviously you'd have to change the starting coordinates, one way to do that is @DesktopWidth/2 @DesktopHeight/2 or whatever you need for the areas.


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

Share this post


Link to post
Share on other sites
#include 'ImageSearch.au3'
#include <Array.au3>
Local $x, $y
$val = _ImageSearchArea(@ScriptDir & '\map_marker.PNG', 1, 0, 0, @DesktopWidth / 2, @DesktopHeight / 2, $x, $y, 0, 0)
If $val = 1 Then MsgBox("", "1", " Position: " & $x & " x " & $y)
$val = _ImageSearchArea(@ScriptDir & '\map_marker.PNG', 1, @DesktopWidth / 2, 0, @DesktopWidth, @DesktopHeight / 2, $x, $y, 0)
If $val = 1 Then MsgBox("", "2", " Position: " & $x & " x " & $y)
$val = _ImageSearchArea(@ScriptDir & '\map_marker.PNG', 1, 0, @DesktopHeight / 2, @DesktopWidth / 2, @DesktopHeight, $x, $y, 0)
If $val = 1 Then MsgBox("", "3", " Position: " & $x & " x " & $y)
$val = _ImageSearchArea(@ScriptDir & '\map_marker.PNG', 1, @DesktopWidth / 2, @DesktopHeight / 2, @DesktopWidth, @DesktopHeight, $x, $y, 0)
If $val = 1 Then MsgBox("", "4", " Position: " & $x & " x " & $y)

 


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

Share this post


Link to post
Share on other sites

_ImageSearchArea will work with changing size of area search
using cords of objects found as start point for next searching loop,
I'm using bmp's, png should be fine too, I tested that and it search fine
You have to test with y coordinates if it work fine but that's a way which You can follow with Your imagesearch :)
Of course You can simplify that, I just want to show what guestscripter explained
BTW. I noticed problems with 64 bits imagesearch libraries,
Someone tested working 64 bit imagesearch?

Local $val0, $val1, $val2, $val3, $val4
Local $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4
$val0 = _ImageSearchArea(@ScriptDir & '\map_marker.bmp', 1, 0, 0, @DesktopWidth, @DesktopHeight, $x0, $y0, 50)
If $val0 = 1 Then MsgBox("", "1", " Position: " & $x0 & " x " & $y0)
$val1 = _ImageSearchArea(@ScriptDir & '\map_marker.bmp', 1, $x0, 0, @DesktopWidth, @DesktopHeight, $x1, $y1, 50)
If $val1 = 1 Then MsgBox("", "2", " Position: " & $x1 & " x " & $y1)
$val2 = _ImageSearchArea(@ScriptDir & '\map_marker.bmp', 1, $x1, 0, @DesktopWidth, @DesktopHeight, $x2, $y2, 50)
If $val2 = 1 Then MsgBox("", "3", " Position: " & $x2 & " x " & $y2)
$val3 = _ImageSearchArea(@ScriptDir & '\map_marker.bmp', 1, $x2, 0, @DesktopWidth, @DesktopHeight, $x3, $y3, 50)
If $val3 = 1 Then MsgBox("", "4", " Position: " & $x3 & " x " & $y3)
$val4 = _ImageSearchArea(@ScriptDir & '\map_marker.bmp', 1, $x3, 0, @DesktopWidth, @DesktopHeight, $x4, $y4, 50)
If $val4 = 1 Then MsgBox("", "5", " Position: " & $x4 & " x " & $y4)
Edited by WiValdiBB

Share this post


Link to post
Share on other sites

Yep, 64bit works here.

I think there's one thing that would be a great improvement, which is the ability to have the equivalent to a wildcard.

For example: an image with transparency areas that would serve as wildcard, so that the surroundings of the image could change, but it would find it anyway, because parts of it would remain the same, know what i mean?

Something similar could be done with pixel search, by checking if X pixels to the right of Y, there is a determinate pixel. etc


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

Share this post


Link to post
Share on other sites

careca May You send my Your 64 bits library with Your working example? I tested few of them without success

What I don't like in image search is screen resolution relation, Have You tried transblack, transwhite options (ImageSearch_hacked) ?
Is not exactly what You want but there is a way to
Do You Use FastFind library? Like them much more then Pixelsearch

Edited by WiValdiBB

Share this post


Link to post
Share on other sites

https://drive.google.com/open?id=0B23_uXFHGQQ1M2c2UHdpTC1fd1U

Nop, didn't try that and never used fastfind.

Edited by careca

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

Share this post


Link to post
Share on other sites

I was curious how long this thread would go after being resurrected before someone ruined it for everyone...

@nckibonbon please read the forum rules as it is obvious you didn't do so on your way into the forum. You'll notice the section where we don't support game automation, which will tell you why this thread is now locked.

Always open to reviewing these things, after a discussion among the Mod team the thread is reopened so as not to be heavy-handed.

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!

Share this post


Link to post
Share on other sites

Hi,

I don't know about AutoIT but could manage the DLL working with Delphi:

for retrieving all occurences you'll have to Loop (the DLL allways returns the first occurce from left to right THEN from up to down !):

so after first match you have to Loop from left to right until no match anymore and then from top to bottom !

example in Delphi:

procedure FindAllGraphics(pStartX,pStarty,pEndeX,pEndeY:integer);
var
  pPosX,pPosY,pSizeX,pSizeY,pRetC : integer;
  sRet        : AnsiString;
  pFileName   : AnsiString;
  sText       : AnsiString;
  sStringList : TStringList;
Begin
  sStringList := TStringList.Create;
  sStringList.Delimiter := '|';
  sStringList.StrictDelimiter := true;
  pFileName := '*50 z:\ImageSearch.bmp';
  sText := '';
  sRet := ImageSearch(pStartX, pStarty, pEndeX, pEndeY, PAnsiChar(pFileName));
  while (LeftStr(sRet,1) = '1') do begin
    sText := sText + sRet + #13#10;
    sStringList.DelimitedText := sRet;
    pRetC  := StrToInt(sStringList.Strings[0]);
    pPosX  := StrToInt(sStringList.Strings[1]);
    pPosY  := StrToInt(sStringList.Strings[2]);
    pSizeX := StrToInt(sStringList.Strings[3]);
    pSizeY := StrToInt(sStringList.Strings[4]);
    sRet := ImageSearch(pPosX+1, pPosY, pEndeX, pEndeY, PAnsiChar(pFileName)); {left to right}
    if LeftStr(sRet,1) = '0' then begin {no match => lstart left next line}
    sRet := ImageSearch(pStartX, pPosY+1, pEndeX, pEndeY, PAnsiChar(pFileName)); {top to bottom}
    end;
  end;
  sStringList.Free;
  memo1.Clear;
  memo1.lines.Add(TimeToStr(now()) + ' Capture ' + Format(' %d x %d y ... %d x %d y',[xPosStart, yPosStart, xPosEnd, yPosEnd]));
  memo1.lines.Add(sText);
End;

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By zuladabef
      All my scripts were working fine and now I am getting this error.  How do I resolve it?  Which folder(s) do the DLLs need to be in?
      ! Dll not found or Call Dll error !  
    • By Synaps3
      I've recently been annoyed by how google images works. You can't click an image and see the full res image. You have to go to the website and find the image on the page and occasionally, it won't allow you to view the image easily. This script makes it easy to view any image in google images in full resolution immediately. You just press Ctrl + Q while hovering over the image you want and it'll open full res in a new tab. This is only tested in firefox, but it shouldn't be hard to modify for another browser.
      Here it is:
      #include <Misc.au3> #include <MsgBoxConstants.au3> #include <AutoItConstants.au3> #include <Clipboard.au3> #include <Array.au3> #include <String.au3> Local $clipB Local $urlArray Local $theLink HotKeySet("^q", "OpenImage") Func DecodeUrl($src) Local $i Local $ch Local $buff ;Init Counter $i = 1 While ($i <= StringLen($src)) $ch = StringMid($src, $i, 1) ;Correct spaces If ($ch = "+") Then $ch = " " EndIf ;Decode any hex values If ($ch = "%") Then $ch = Chr(Dec(StringMid($src, $i + 1, 2))) $i += 2 EndIf ;Build buffer $buff &= $ch ;Inc Counter $i += 1 WEnd Return $buff EndFunc ;==>DecodeUrl Func OpenImage() MouseClick($MOUSE_CLICK_RIGHT) Send("A") Sleep(100) $clipB = _ClipBoard_GetData($CF_TEXT) $theLink = DecodeUrl($clipB) $urlArray = _StringBetween($theLink, "=", "&") If StringInStr($urlArray[0], "?") <> 0 Then $urlArray = _StringBetween($theLink, "=", "?") EndIf ShellExecute($urlArray[0]) EndFunc While 1 Wend  
    • By Schuster
      Hey there,
      i need your help guys. I am working in the QA department of my company. A little part of or software produces hints (like little popups) which needs to be tested every release. I wrote scripts to trigger those hints and i also implemented the imagesearch.dll to find those hints on the screen. My goal now is it to let the machine work over night, so when i come back to work i only get a report which says 130/140 hints were found successfully. This already works, aslong as my pc is not locked, but i want/need to lock my pc because of security concerns.
      My general question. Is it possible to run my scripts on my locked PC and still find an image that i provide which will open sooner or later in the background?
       
      Thanks in advance
    • By kylejustknows
      The code is solid and simple, it can almost explain itself.
      This is the native autoit way to do the "imagesearch", no 3rd party .dll needed.
       
      It gets "your.bmp", and "screenshot.bmp" ----> Convert the  .bmp files into 2D-Arrays (Malkey's function)  ----> Compare the 2D-arrays, return the matched position.
       
      Tested on: Windows 7; Windows server 2008R2; Windows 10 1809.
       
      Pros:
      It is native. No extra .dll needed
      It is super robust. (I used to have lots of funny results using other imagesearch libs).
      It gets screenshot the same you get your screenshot crop, so it always gets a solid result, and 100% accurate.
      The code is very simple and friendly, all level users can understand and use it.
       
      Cons:
      It is slow to convert your.big.screen.bmp into a 2D-array, and may consume 200+MB of memory and may take 5 - 20 seconds to return the result. (the actual search in an array is fast, but the conversion from .bmp to array is slow. The speed depends on your CPU speed and your screen size).
      Correct: now optimized,  it's ~5 seconds and ~ 70MB ram usage.
      It is a pixel-by-pixel color-code strict comparison in the "array-in-array" search, so you have to use the 24-bit BMP file, no "Tolerance" allowed.
       
      2019-Jun-11: script update:
      Same day updated: Update example;  Optimize the algorithm for performance, now most computers can get the result in ~5 seconds, using ~70MB temporary memory, for the 1920x1080 resolution screen.
       
      2019-Jun-12 script update:
      It now uses "PrintScreen" hotkey to save the screenshot.bmp (restores the user's old clipboard content after it is done) ~This is the only way to make sure the screenshot matches exactly what the user is seeing, after doing dozens of harsh tests.
      The reason: The UDF "ScreenCapture" and "ImageSearch.dll"  are not reliable for an unknown reason. Some window/dialogue special drawings are "invisible" in their screenshots.
      But the "PrintScreen" key -> Clipboard -> screenshot.bmp, this method always catches exact things showing on the screen.
       
      #include <GDIPlus.au3> #include <ClipBoard.au3> ;Sinple Example.================== the 1.bmp is what you want to find on your screen $result = _ScreenSearchBmp("1.bmp") if $result[0] = 0 Then MsgBox(0,"","not found") Else MouseMove($result[0],$result[1],20) ;move mouse to the result EndIf ;Example End.================== You can "include" this file after you remove this "Example" part here. ;=============================================================================== ; ; Description: Main Function. Find the position of an image on the desktop ; Parameter(s): ; $center = 1 - Set where the returned x,y location of the image is. ; default 1 means center, 0 means top-left ; ; Return Value(s): On Success - Returns the array of matched position [x,y] on your screen. ; On Failure - Returns array [0,0] (BTW, there is no position 0,0 on a screen, so it means error) ; ; Note: Warning: The BMP file must be a 24-bit BMP (windows default) ; ;=============================================================================== Func _ScreenSearchBmp($file,$center=1) local $pixelarray,$screenarray ;get both your image.bmp and screenshot.bmp into pixel-by-pixel 2D arrays _FileImageToArray($file, $pixelarray) _Clip_screenshot(@TempDir & "\screenshot.bmp") _FileImageToArray(@TempDir & "\screenshot.bmp",$screenarray) FileDelete(@TempDir & "\screenshot.bmp") ;compare the 2 2D-arrays local $result = _2darray_in_2darray($screenarray,$pixelarray) ;result tidy up, for if $center=1, and for if not found. Local $aresult[2] $aresult[0] = $result[0] $aresult[1] = $result[1] if $aresult[0] = 0 then Return $aresult ;if not found , return 0 0 here if $center = 1 then $aresult[0] = $result[0]+ Round(UBound($pixelarray,1)/2) if $center = 1 then $aresult[1] = $result[1]+ Round(UBound($pixelarray,2)/2) Return $aresult ;if ALL GOOD, and $center=1 then return the center of the image here. EndFunc ;=============================================================================== ; Code by Malkey, converts .bmp into 2D array pixal by pixal. : thanks man! ;=============================================================================== Func _FileImageToArray($filename, ByRef $aArray) Local $Reslt, $stride, $format, $Scan0, $iW, $iH, $hImage Local $v_Buffer, $width, $height Local $i, $j _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($filename) $iW = _GDIPlus_ImageGetWidth($hImage) $iH = _GDIPlus_ImageGetHeight($hImage) $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") Dim $aArray[$width][$height] For $i = 0 To $iW - 1 For $j = 0 To $iH - 1 $aArray[$i][$j] = DllStructGetData(DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4)), 1) Next Next _GDIPlus_BitmapUnlockBits($hImage, $Reslt) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() Return EndFunc ;==>_FileImageToArray ;=============================================================================== ; ; Description: ; My code, search a 2D array inside another 2d array ; If found, return the positon of first element ; If error or not found, return array [0,0]. Because the very first match would be [1,1], "0" means something wrong. ; eg. search a 2d array ; [1,2,3,4] ; [5,6,7,8] ; [9,0,1,2] ; for: ; [7,8] ; [1,2] ; You will get result [2,3] (means, matched, first element position is row 2, colunm 3) ; ; Parameter(s): ; ; Return Value(s): On Success - Returns the array of matched [x,y], the top-left element position in the source. ; On Failure - Returns [0,0] ; ; ;=============================================================================== Func _2darray_in_2darray($source,$search) ;get the size of the both arrays local $sourcerow = UBound($source,1) Local $sourcecol = UBound($source,2) local $searchrow = UBound($search,1) Local $searchcol = UBound($search,2) ;error input cheching, if error return position 0,0 if $sourcerow = 0 or $sourcecol = 0 or $searchrow = 0 or $searchcol = 0 then Local $aPeople[2] $aPeople[0] = 0 $aPeople[1] = 0 Return $aPeople EndIf ; A crazy 4-for-loops, compare every x,y of search array in every x,y in source array for $ssr = 1 to $sourcerow - $searchrow +1 for $ssc = 1 to $sourcecol - $searchcol +1 for $sr = 1 to $searchrow for $sc = 1 to $searchcol ;if an element not match, go back, search for next if $search[$sr-1][$sc-1] <> $source[$ssr+$sr-2][$ssc+$sc-2] then ContinueLoop 3 Next Next ;if the loop passed all elements test, made it here, means the result is found! congress! lets return the result: Local $aPeople[2] $aPeople[0] = $ssr $aPeople[1] = $ssc Return $aPeople Next Next ;all the loops finished, no result found. return [0,0] Local $aPeople[2] $aPeople[0] = 0 $aPeople[1] = 0 Return $aPeople EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Clip_screenshot ; Description ...: This get a screenshot.bmp using "Print Screen" key, so the image is EXACT same image you use "Print Screen" key, to avoid funny results. ; Syntax ........: _Clip_screenshot($file) ; Parameters ....: $file - The location of the screen shot .bmp file you want it to save ; Return values .: None ; Author ........: Kyle ; =============================================================================================================================== Func _Clip_screenshot($file) local $tempdata = _ClipBoard_GetData() ;save current user's clipboard Send("{PRINTSCREEN}") sleep(200) If _ClipBoard_IsFormatAvailable($CF_BITMAP) Then _ClipBoard_Open(0) $hClipboardImage = _ClipBoard_GetDataEx($CF_BITMAP) _ClipBoard_Close() _GDIPlus_Startup() $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hClipboardImage) Local $iX = _GDIPlus_ImageGetWidth($hBitmap) Local $iY = _GDIPlus_ImageGetHeight($hBitmap) Local $hClone = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iX, $iY, $GDIP_PXF24RGB) ;make sure its 24bit bmp _GDIPlus_ImageDispose($hBitmap) $hBitmap = $hClone $sCLSID = _GDIPlus_EncodersGetCLSID("BMP") _GDIPlus_ImageSaveToFileEx($hBitmap, $file, $sCLSID, 0) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_Shutdown() EndIf _ClipBoard_SetData($tempdata) ; restore user clipboard EndFunc Remove the "example" part then you can include this code as a file.
    • By kuhicop
      Hello, I need to find an image on screen and return it's position left, top, right, botton.
      I'm using the ImageSearch function but it only returns 1 or 0.
      Any ideas? Thanks!
×
×
  • Create New...