Sign in to follow this  
Followers 0
Sub

ImageSearch and GUI

5 posts in this topic

#1 ·  Posted (edited)

Im trying to use the ImageSearch.au3 from

but I am having a hard time trying to get it to detect an image not on the desktop itself. Here is what I have

#include <ImageSearch.au3>
#include <GUIConstants.au3>
#include <ScreenCapture.au3>
#Include <Misc.au3>
#include <WindowsConstants.au3>
HotKeySet("{F9}", "Start")
HotKeySet("{F10}","Stop")
HotKeySet("{F11}","Terminate")
HotKeySet("{F8}", "Pic")

$Stop = 0
$Height = 20
$Width = $Height*1.5
$X = 0
$Y = 0

Func Pic()
GUIDelete();Deletes any GUI present
$pos = MouseGetPos();Gets mouse position
_ScreenCapture_SetBMPFormat(0)
_ScreenCapture_Capture("Shot.bmp", $pos[0], $pos[1], $pos[0] + $Width, $pos[1] + $Height, 1);Takes screenshot around the mouse
GUICreate("", $Width, $Height, (@DesktopWidth/2)-100,@DesktopHeight-200,$WS_POPUP,$WS_EX_TOPMOST);Creates GUI for picure
GUICtrlCreatePic("shot.bmp", 0, 0, $Width, $Height);Displays picture on the GUI
GUISetState();Makes GUI visible
EndFunc

Func Start()
$Stop = 0
while $Stop = 0
$Search = _ImageSearch('Shot.bmp', 0, $X, $Y, 10);Searches for Shot.bmp taken by ScreenCapture
If $Search = 1 Then
TrayTip("","Found", 1)
Else
TrayTip("","Not Found", 1)
EndIf
Wend
EndFunc

while 1
sleep(10)
WEnd


Func Terminate()
Exit 0
EndFunc

Func Stop()
$Stop = 1
sleep(500)
TrayTip("","Stopped", 3000)
EndFunc

What it does is, if you press F8 it will take a small picture called 'Shot.bmp' around your mouse and display it in the lower middle of your screen by using a GUI. When you press F9 it will try to do an ImageSearch of 'Shot.bmp'. My problem is that it can not find the GUI I have displayed on the screen. It only finds the Icon of the 'Shot.bmp' on my desktop since Im running the script off my desktop and the picture is being saved there. If I open the 'Shot.bmp' from an image viewer, the ImageSearch does not find this picture either. Still it only detects the icon on the desktop. Am I using the ImageSearch wrong? Will it not find GUIs in this fashion?

Edited by Sub

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The image search starts from the top left and works down to the bottom right, so if shot.bmp is before your gui version then yes it will say "Found!" open up notepad or something to block off that image on your desktop and try again or change $x = 0 to $x = "after image" $y = "after image"

Images searching is not 100% accurate either and can "sometimes" go over the image and say "not found", when i played about with it i ended up using the same image multiple times (when it faled id take a new screen, crop the image and tell my script to look for both and add more when needed.)

Here is a basic example of what i mean which can be improved, i have another somewheer using a basic array but this was easier for modification

;images should be names test0, test1, test2 etc
$image = "test0"
$imgdir = "image"
$c = 5 ; number of image to check dont matter if this is 20 and you have only 2 images

For $i = 0 To $c
$img = _ImageSearchArea($imgdir & $image & $i & ".png", 1, 0, 0, @DesktopHeight, @DesktopWidth, $x, $y, $tollerence)
If $img = 1 Then
MouseClick("left", $x, $y)
Return
EndIf
Next

As you can see no error checking that was another 20 lines down. Btw i have never attepmtped to do what your doing, mine has always been desktop or web based.

Edited by Gotemp

Share this post


Link to post
Share on other sites

Well part of the problem is that I really question how ImageSearch is supposed to work when I capture a small picture and use that exact picture and open it in like Windows Photo Viewer and the ImageSearch turns up negative. If anyone could take the code I posted see what might be wrong with it cause I am really just confused to why it wouldnt work if you open up the picture in some viewer. Just press F8 to take the picture and then F9 to start the search.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well part of the problem is that I really question how ImageSearch is supposed to work ...

Open up ImageSearch.au3 to see what it does. It should be in the includes folder of the autoit folder.

After looking at your script I think I see your problem. Looks like image search only looks for something on your desktop, not on your screen unless you "load it" into an "Hbitmap" from what I understand.

Here is the description for image search:

"; ------------------------------------------------------------------------------

;

; AutoIt Version: 3.0

; Language: English

; Description: Functions that assist with Image Search

; Require that the ImageSearchDLL.dll be loadable

;

; ------------------------------------------------------------------------------

;===============================================================================

;

; Description: Find the position of an image on the desktop

; Syntax: _ImageSearchArea, _ImageSearch

; Parameter(s):

; $findImage - the image file location or HBitmap to locate on the

; desktop or in the Specified HBitmap

; $tolerance - 0 for no tolerance (0-255). Needed when colors of

; image differ from desktop. e.g GIF

; $resultPosition - Set where the returned x,y location of the image is.

; 1 for centre of image, 0 for top left of image

; $x $y - Return the x and y location of the image

;

; $HBMP - optional hbitmap to search in. sending 0 will search the desktop.

;

; Return Value(s): On Success - Returns 1

; On Failure - Returns 0

;

; Note: Use _ImageSearch to search the entire desktop, _ImageSearchArea to specify

; a desktop region to search

;

;==============================================================================="

"$HBMP - optional hbitmap to search in. sending 0 will search the desktop."

And here is the demo

#include
#include

$fileA = @ScriptDir & "testa.png"

_GDIPlus_Startup()

$hImageA =_GDIPlus_ImageLoadFromFile($fileA) ;this is the firefox icon use something else if you don't have it.
$hBitmapA = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageA)

$x = 0
$y = 0

$result = _ImageSearch($hBitmapA, 1, $x, $y, 20, 0) ;Zero will search against your active screen
If $result > 0 Then
MouseMove($x, $y)
EndIf

_GDIPlus_ImageDispose($hImageA)
_GDIPlus_Shutdown()

I would take your image and do this to it:

#include <GDIPlus.au3>

$fileA = @ScriptDir & "shot.bmp"

_GDIPlus_Startup()

$hImageA =_GDIPlus_ImageLoadFromFile($fileA) ;this is your screenshot

$hBitmapA = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageA)

_GDIPlus_ImageDispose($hImageA)

_GDIPlus_Shutdown()

;---------

I imagine that will work...

EDIT:

After downloading what seems to be the newest imagesearch with DLL, and using the demo.. I can't get it to work either. Even the demo doesn't work. Sorry I couldn't help more!

Edited by EndGame2013

Share this post


Link to post
Share on other sites

Well part of the problem is that I really question how ImageSearch is supposed to work when I capture a small picture and use that exact picture and open it in like Windows Photo Viewer and the ImageSearch turns up negative. If anyone could take the code I posted see what might be wrong with it cause I am really just confused to why it wouldnt work if you open up the picture in some viewer. Just press F8 to take the picture and then F9 to start the search.

Like i said in my first post image search is not 100% accurate, you can use one image that works flawless for hours restart the script and it will not find the image for some unknown reason, if your issue is only with Windows Photo Viewer then have you thought about not using it for tests? I personally dont use it as it discolors the images but then again i thought that this might of been a Windows 8 bug.

All my tests are done on the Desktop or in a Windows Folder, but Windows Paint works just fine too.

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
Sign in to follow this  
Followers 0