Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Imagesearch Help


  • Please log in to reply
12 replies to this topic
tamn
  • Members
  • 7 posts
  • Last active: Aug 05 2011 10:41 PM
  • Joined: 15 Apr 2011
Hello,
I've been reading up on AHK and coding some scripts the past few weeks, I've read all the guides and tutorials I can find, and I cannot get the image search function to work for me.

Mainly, I would like some clarification on how this command works.
I've messed around for many many hours with this without success.

So I understand in the following code, the X and Y are where the image co-ordinates were found on the screen. What are the following "0, 0" standing for, in the examples I look at, the numbers are usually different, but not always and I was not able to find what they stood for.

And the a_ScreenWidth, A_ScreenHeight is the area being searched for the image? If I'm understanding that right.

I've taken high res .bmp's and .png's and even .jpg's without resizes trying to get the function to work. But have not been able too.

ImageSearch, X, Y, 0, 0, a_ScreenWidth, A_ScreenHeight, image.bmp
If Errorlevel = 0
Click %X%, %Y%


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Hi tamn, welcome to the forum :)

The description for the command seems perfect to me <!-- m -->http://www.autohotke...ImageSearch.htm<!-- m -->

but here is another example.
In the docs the command is listed like this :arrow: ImageSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ImageFile

You seem to understand the first 2 parameters, OutputVarX, OutputVarY, just not the next 4.

If you have only one monitor, the pixels at the upper left corner of your screen has a location of (x=0,y=0)
the pixel at bottom right corner has larger values for x & y values.

If you drew a rectangle on your screen 200 pixels wide and 100 pixels tall, you would have a box with 4 corners that is wider.
The upper left corner would be X1, Y1, and the lower right corner would be x2, y2 Or, 0,0,200,100

You change the box size and position by changing these 4 values.

Here is an example script that opens the main AHk webpage and searches for an image on that page.

#singleinstance force
settitlematchmode 2

CoordMode, ToolTip, screen
CoordMode, Mouse, screen
CoordMode, Pixel, screen


image_name=DE.gif

msgbox, , , open main AHK webpage where there are GIF images the search for ,3

run, http://www.autohotkey.com/

; poor example of waiting for the webpage to load

sleep, 1000

msgbox, , , start with a known image.. the Deutsch Flag icon on http://www.autohotkey.com ,2


IfNotExist, %image_name%   ; start with a know image, the Deutsch Flag icon on http://www.autohotkey.com
{
  UrlDownloadToFile, http://www.autohotkey.com/docs/images/DE.gif, %image_name%
  msgbox,,, d/l complete, 1
}

SysGet, VirtualScreenWidth, 78
SysGet, VirtualScreenHeight, 79

SysGet, VirtualScreenLeft, 76
SysGet, VirtualScreenTop, 77

msgbox , ,, VirtualScreen upper_left is %VirtualScreenLeft% %VirtualScreenTop%, 3
msgbox , ,, VirtualScreen size is %VirtualScreenWidth% %VirtualScreenHeight%, 3

; show that the mouse can move the full screen size
mousemove, VirtualScreenLeft, VirtualScreenTop, 50
mousemove, VirtualScreenLeft+VirtualScreenWidth, VirtualScreenTop+VirtualScreenHeight, 50

msgbox , ,, move back to center, 3

mousemove, VirtualScreenLeft+(VirtualScreenWidth/2), VirtualScreenTop+(VirtualScreenHeight/2), 50

msgbox , ,, Search for the image on the webpage, 3

ImageSearch, OutputVarX, OutputVarY, VirtualScreenLeft, VirtualScreenTop
      ,VirtualScreenLeft+VirtualScreenWidth, VirtualScreenTop+VirtualScreenHeight, %image_name%


if (errorlevel<>0)
{
msgbox ErrorLevel was %ErrorLevel% `nErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found
, or 2 if there was a problem that prevented the command from conducting the search (such as failure
. to open the image file or a badly formatted option). `n`n press OK to continue.
}
else
{

msgbox, , ,  Upper left corner of %image_name% is at %OutputVarX% %OutputVarY% "`n" move the mouse there, 3


mousemove, OutputVarX, OutputVarY, 50

msgbox,,, we're pointing at the image !, 3


}


;return

msgbox,,, Exiting Application, 3
exitapp


#q::  ;Win-Q to exit
msgbox exitapp
exitapp


keybored
  • Members
  • 351 posts
  • Last active: Apr 26 2013 09:08 AM
  • Joined: 18 Jun 2006
If your image is in the folder with the script it should have worked. Otherwise you need the full file address in your script. Although Leef_me is right it could be a screen size issue.

ImageSearch, X, Y, 0, 0, a_ScreenWidth, A_ScreenHeight, C:\Where\image.bmp
If Errorlevel = 0
Click %X%, %Y%


tamn
  • Members
  • 7 posts
  • Last active: Aug 05 2011 10:41 PM
  • Joined: 15 Apr 2011
Thanks for the helpful script mate, it clarified some things and ran fine on my computer. I'll go see if I can get mine to work now. :D

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
I meant to share the following ideas that have worked for me.

1. if you have a supplied example of the image to find, use it ex:DE.gif
2. if you have to create the image yourself, use 24 bit bmp, not jpg. I haven't created png so I can't help.
3. I think that you have an image with transparency use the *transN option.

4. If you want to click an image (such as the center of a button) crop your image so that the upper left corner is the click point.

tamn
  • Members
  • 7 posts
  • Last active: Aug 05 2011 10:41 PM
  • Joined: 15 Apr 2011

I meant to share the following ideas that have worked for me.

1. if you have a supplied example of the image to find, use it ex:DE.gif
2. if you have to create the image yourself, use 24 bit bmp, not jpg. I haven't created png so I can't help.
3. I think that you have an image with transparency use the *transN option.

4. If you want to click an image (such as the center of a button) crop your image so that the upper left corner is the click point.


Yea, I've been messing around with it all day now.

I can get any imagesearch function to work properly now for me outside of the game window.

I tested getpixel search inside the windows, and it does return the pixel colors back. So I'm thinking my problem is now based on the transparency you mentioned.

Also was reading up on WinSet, and maybe that could be used as well.
I'll mess with it some more tonight.

By the way, I've been using .png's most of the time. Would .bmps work better?

The images im searching for, have a semi transparent background around them in some cases.

But I also took some non changing parts of the screen to check for testing. And it was still unable to find them either.

I tried full screen, as well as windows, and messed with resolutions a bit as well to no success.

tamn
  • Members
  • 7 posts
  • Last active: Aug 05 2011 10:41 PM
  • Joined: 15 Apr 2011
Update: I messed with pixelsearch for awhile, and was never able to find a pixel color within the game window.

I then was thinking for some reason the game is preventing these methods, so I took a windows printscreen of the game. Pasted it into paint. And then made my imagesearch do the paint for the object. Which also failed every time.

But if i open the image im searching for in paint alone, and then search for it, it finds it every time.

I'm pretty confused. :?

Edit: I tried again with .bmps and it failed again, so I tried taking a section of the screen for the imagesearch, without zooming in and making it perfect and all that. And it works every time searching the original picture now.

But if I zoom in on the pic, and carefully select an area, and then search that against it. It never works.

So whats up with that?
also...tried the same method with the actual game client. Works every time. So I'm not really sure why zooming in when editing the pic changes anything?

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
If you set the game the way you intend to play it, and hit printscreen, you should have a screencap on the clipboard.
If you open Paint, and paste the clipboard you should see the game image you 'caped'
If you don't see the game image, neither will AHk. :cry:

edit: I re-read your post and realized you already made imagesearch work.

... so I tried taking a section of the screen for the imagesearch, without zooming in and making it perfect and all that.
And it works every time searching the original picture now.

AHk can't know, on its own, that you zoomed the window. :roll:
Why do you need the window zoomed?

The search command has *wn and *hn
which can be used to change the apparant size of the imagefile.
This might work, but you'd need the correct scale and then you would need to figure the value, this is not trivial.

szujeq
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011
ImageSearch, X, Y, 0, 0, a_ScreenWidth, A_ScreenHeight, *32 C:\Where\image.bmp
If Errorlevel = 0
Click %X%, %Y%

you should use pixel color tolerance (0-255), ("*n (variation)" in help)
*32 C:\Where\image.bmp
and use only uncompressed image file format like bmp png etc
and best way is to make image in natural size without resizing

tamn
  • Members
  • 7 posts
  • Last active: Aug 05 2011 10:41 PM
  • Joined: 15 Apr 2011
Well, I was zooming in on my image editor so that I could trim the picture i printscreened.

but anyhow,
I've been messing with the transparency's, trying to get those too work.

Still work in progress
:wink:

  • Guests
  • Last active:
  • Joined: --
You might want to try the game in window mode if your running fullscreen,
I found that under window mode my game worked better for finding pixel colors

Here is a bit of code i have at the begging of my script

It has no allowance for the WIDTH of the screen yet first thing first getting everything to work under messing with the Y axis then i'll worry about width if i really want to.

; Windowed Mode  - Original 1920:1080
WMO:="0"  ; Window Mode On  1 = yes | 0 = No
WME:="1"  ; Window Mode Extended Display ( 2nd, 3rd, 4th Monitor ) 1 = yes | 0 = No
WMD:="0" ; Window 7 Desktop  1 = yes |  0 = | No Window Must be Snapped into place  !
WMXLD:="0" ; Left Allowance Width
WMXRD:="0" ; Right Allowance Width
SysGet, WMYTD, 4 ; Windows Top Border Width
SysGet, WMX, 32
SysGet, WMY, 33

if WMO = 1
{
    ToolTip, Windows Mode Active, 10, 8 
	}
else If WME = 1
{
WMYBD = 0
WMXLD = 0
WMXRD = 0
    ToolTip, Windows Extended Display Mode Active, 10, 8 
	}

else If WMD = 1
{
WMYBD:= "40" ; Adjustment for Task Bar 
	}
else If (WMO = 0 AND WMD = 0 AND WME = 0)
{

WMX = 0
WMY = 0
WMXLD = 0
WMXRD = 0
WMYTD = 0
WMYBD = 0

 ToolTip, Full Screen Mode Active, 10, 8 

}

This is a snippet of what my click would look like ( i editted a few things i dont want to give away for my game )

x :=(8+(WMX)+(WMXLD))
y :=  (1026+(WMY)-(WMYBD))
click %x% %y%

I used this to get my image created

Original

<!-- m -->http://www.autohotke... ... s&start=15<!-- m -->

Modification just enter the coordinates vs click and drag

Suggestion make the search area 5 pixels in x,y axis larger then actual image

edit the *N variable start at something rediculous like 250 work your way down if you get a hit to an acceptable number that works test. This also depends how your game functions could also try editting all the other pixels in the image that arnt use to find the image your looking for to a specific color then set TransN to that color atleast thats how i think that works ( still a little stuck on this my self )

Sleep, 1000

SetFormat Integer,H
CoordMode Mouse, Screen
CoordMode Tooltip, Relative
CoordMode Pixel, Screen
file = C:\Users\(UserName)\Desktop\Test7.bmp Easy way to get this open my computer / documents navigate to where you want the image to go left click to nav bar, as if it was the internet ;p  then copy past that here with "\filename.bmp" at the end.
SetTimer, Clock, 1000
start_x:=2799
   start_y:=837
   end_x:=2955
   end_y:=857
   CurrentRunNumber+=1
   ClockCounter=0
   ToolTip,Retreiving Colors...,10,8
   TrayTip,,Retreiving Colors...,,1
   width  := end_x - start_x
   height := end_y - start_y
   Loop %height% {
      pixely := start_y + height - A_index - 1  ; bitmaps are written backwards
      Loop %width% {
         pixelx := start_x + A_index - 1
         PixelGetColor color,pixelx,pixely      ; get the BGR value
         StringTrimLeft color, color, 2         ; remove 0x
         colorlist = %colorlist%%color%
      }
   }
   TrayTip,,Saving Bitmap...,,1
   ToolTip,Saving Bitmap...,10,8
   WriteBMP(file,colorlist,width,height,0)
   TrayTip,,Done!,,1
   ToolTip,Done!...,10,8
   Msgbox Done
Sleep, 5000
Return

WriteBMP(file,colors,width=16,height=16,padded=0) {
   If (!padded and StrLen(colors)/6 <> height*width   ; check if we have the right number of pixels
     or padded and StrLen(colors)/6 - (width&3)*height <> height*width)
      MsgBox Pixel Mismatch!`nThe Height*Width specified does not match the number of pixels provided, or the padding is incorrect
   If (!padded and (width&3)<>0) {                    ; bitmap needed but not provided with padding
      Loop %height% {
         StringMid row, colors, % width*6*A_index-5, % width*6
         colorspadded := colorspadded row padhex(0x0,width&3) ; add 00 bytes
      }
      colors = %colorspadded%
   }
; Create the actual bitmap, starting with the header
   width := padhex(width +0,4)         ; make width and height 4-byte words
   height:= padhex(height+0,4)
;           B M size   reserved offbits bitsize             planes bitcount (24 bit)...the rest is color table (0s)
   BMPHex = 424d00000000000000003600000028000000%width%%height%01001800000000000000000000000000000000000000000000000000%colors%

   Handle := DllCall("CreateFile",Str,file,Uint,0x40000000,Uint,0,UInt,0,UInt,4,Uint,0,UInt,0)
   Loop % StrLen(BMPHex)//2 {
     StringMid Hex, BMPHex, 2*A_Index-1, 2
     DllCall("WriteFile",UInt,Handle, UCharP,"0x" Hex, UInt,1,UIntP,UnusedVariable,UInt,0)
   }
   DllCall("CloseHandle", Uint,Handle)
}

padhex(hex,bytes) {        ; remove 0x, right-pad hex number to bytes length <= 4
   If hex > 255
      hex := (hex>>8) + ((hex&255)<<8) ; little endian coding
   If StrLen(hex) & 1
        StringReplace  hex, hex, x
   Else StringTrimLeft hex, hex, 2
   hex = %hex%000000
   StringLeft hex, hex, 2*bytes
   Return hex
}

Clock:

	ClockCounter++
	
	{
	
		If (BestTime<>9999)
		{
			If (GlobalFinishTime="")
			{
				ToolTip, Clock=%ClockCounter%`, RunNumber=%CurrentRunNumber%, 360, 8, 20
			}
			/*
			Else
			{
				ToolTip, Clock=%ClockCounter%`, Finish:%GlobalFinishTime%`, BestTime=%BestTime%`, RunNumber=%CurrentRunNumber%, 360, 8, 20
			}
			*/
		}
		
	}

Return
; END Clock:


Other options

might be this......

<!-- m -->http://www.autohotke...topic47691.html<!-- m -->

Lusername
  • Members
  • 6 posts
  • Last active: Jun 18 2014 07:54 PM
  • Joined: 13 Jun 2014
Would this work if I was trying to find text that is not an image? Like if I was looking for the "Reply to this topic" just under this post.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010

You make an image of the text you want to find then search for that image just as you would for a graphic. Inside a pc everything you see on the screen is an image of one kind or another. I use the windows SnippingTool utility to grab images and save them off as .PNG files and they seem to work fine on my machine. Text images are especially useful to grab this way. With graphics images that move around on the screen, like sprites in some games, it's often best to take a screenshot first to freeze the image and then do a SnippingTool on the screenshot. For images that don't move on the screen you can go straightaway to SnippingTool to grab directly from the screen. The trick with grabbing images is to grab as little as possible that is unique to the image or text that you want to hunt for. Up to a point, the smaller the image the faster the search runs. Try also to try and restrict the search area to only those areas of the screen where the image is known to appear instead of trying to search the entire screen area. That helps with the search speed too.


The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ‚óŹ
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif