ImageSearch for full screen region (ErrorLevel = 1 )

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
srt
Posts: 14
Joined: 16 Mar 2024, 08:38

ImageSearch for full screen region (ErrorLevel = 1 )

03 Apr 2024, 03:08

FullScreen -https://drive.google.com/file/d/1QNFN3wHJmAPuYE7zJ4xm4eOvZIWjzAvW/view?usp=drive_link
ImageSeaarch(Buy) - https://drive.google.com/file/d/1h-GVbSXiMhFT1fjic-drjskV2QzBXZxf/view?usp=drive_link


ImageSeaarch/PixelSearch is not able to locate neither the 'Buy' imageIcon nor the pixels for the provided image above.

The region has been set to full screen and still the code gives errorlevel = 1 .


How to resolve this issue?


Code: Select all

;BUyGreeks
;Click Region click_1 = (850,1000) click_2 = (950,600)
x_d := 100
y_d := 400

buyGreeksImage := "C:\Users\User_0\AppData\Roaming\MacroCreator\Screenshots\buyGreeks.jpg"
^+a::RegionIconClick(buyGreeksImage,x_d,y_d)


#Persistent
RegionIconClick(imagePath,x_d,y_d){
  EnableClick := True  ; Toggle the global variable value
  ; EnableClick := !EnableClick  ; Toggle the global variable value

  
  while (EnableClick) {
    ; Check if clicking is enabled
    
    MouseGetPos, ClickX, ClickY
  
    Click, %ClickX% , %ClickY% , Right, 1
    Sleep, 2000
    CoordMode, Pixel, Window
    ; 0x1E0C94
    ; ImageSearch, BuyX , BuyY, ClickX , ClickY - 400,ClickX + 100 ,ClickY ,C:\Users\User_0\AppData\Roaming\MacroCreator\Screenshots\buyGreeks.png
    ; ImageSearch, BuyX , BuyY, 0,0,1920,1024,C:\Users\User_0\AppData\Roaming\MacroCreator\Screenshots\buyGreeks.png
    PixelSearch,BuyX , BuyY, 0,0,1920,1024, 0x2F269F, 0, Fast RGB
    Sleep, 100
    ; MsgBox, Image search result: %ErrorLevel%. X: %BuyX%, Y: %BuyY%

    if (ErrorLevel = 2) {
      MsgBox, RefImageFile unavailable. Exiting script
      ExitApp  ; Stop script execution
      } else if (ErrorLevel = 1) {
      MsgBox, (%ClickX% , %ClickY% ) Image not found in the specified region.  
      ExitApp  ; Stop script execution
      }else if (ErrorLevel = 0) {

      MsgBox, ImageFound
      Sleep, 50  
      Click, %BuyX% , %BuyY% , Left, 1
      ; MsgBox, Clicking at X: %BuyX%, Y: %BuyY%
      ClickXOffset_1 := BuyX + 425
      ClickYOffset_1 := BuyY + 78
      Sleep, 100  ; Delay before next click (if needed)
      Click, %ClickXOffset_1% , %ClickYOffset_1% , Left, 1
      Sleep, 100  ; Delay before next click (if needed)
      ClickXOffset_2 := BuyX + 525
      ClickYOffset_2 := BuyY + 15
      Click, %ClickXOffset_2% , %ClickYOffset_2% , Left, 1
      Sleep, 100  ; Delay before next click (if needed)
      EnableClick := False  ; Re-toggle clicking off
    }

    Sleep, 1000  ; Wait for 1 second before next iteration
}
}
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: ImageSearch for full screen region (ErrorLevel = 1 )

03 Apr 2024, 05:24

Hello,

If ImageSearch is "failing", you can try a simple new script to test it specifically. "Screen" and "Window" are different coord modes.
  1. The "window" coord mode uses the active window.
  2. Clicking can change which window is the active window.
  3. Some programs use multiple windows even without the user's knowledge.
  4. If you are not sure which window is active, you can use the script to inform you of that information.
I did not test this with a full-screen window. If the failure occurs only in full screen and not elsewhere when tested, you might have a look at some of the posts that discuss full-screen windows. Your meaning behind "full screen region" is not clear to me.

Test an ImageSearch in 3 lines

An ImageSearch can be tested in a total of three lines, as demonstrated in the hotkey subroutine below.

Code: Select all

#Requires AutoHotkey v1.1.33.11
image := "buyGreeks.png"

^F5::  ; Testing an ImageSearch in three lines
CoordMode Pixel
ImageSearch x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, % image
MsgBox % ErrorLevel
Return
image240403-0623-001.png
image240403-0623-001.png (7.99 KiB) Viewed 206 times
gregster
Posts: 9096
Joined: 30 Sep 2013, 06:48

Re: ImageSearch for full screen region (ErrorLevel = 1 )

03 Apr 2024, 09:43

Jftr, while it might be more advanced and not necessarily AHK-related, I would consider using one of this trading company's API interfaces for customers instead of a more or less wonky Imagesearch. For example, the offered Java-based Web API should be a much more reliable and serious approach. That said, I have neither experience with this company nor with their specific API offerings and have no plans to look into it.
rockitdontstopit
Posts: 98
Joined: 12 Nov 2022, 15:47

Re: ImageSearch for full screen region (ErrorLevel = 1 )

03 Apr 2024, 13:24

I was able to locate both images using the following:

Code: Select all

Loop
{
    CoordMode, Pixel, Screen
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, C:\Users\UserName\AppData\Roaming\Screenshots\buy_fullscreen.png
    If ErrorLevel = 0
    {
        MsgBox, 49, Continue?, Image / Pixel Found at %FoundX%x%FoundY%.`nPress OK to continue.
        IfMsgBox, Cancel
        	Return
    }
    Sleep, 50
}
Until ErrorLevel = 0
Same code for the buy button, just replace the png file location.

I like to crop images in Paint with 1 pixel height and maybe about 24 pixels wide. It gives you something similar to a bar code. I've include the images I've cropped.

(I just realized my post as is was not that helpful. I use IB TWS myself. So I tested to see if I could find the Buy button inside TWS and was able to find it.)
buy_button.png
buy_button.png (154 Bytes) Viewed 160 times
buy_fullscreen.png
buy_fullscreen.png (164 Bytes) Viewed 160 times
srt
Posts: 14
Joined: 16 Mar 2024, 08:38

Re: ImageSearch for full screen region (ErrorLevel = 1 )

03 Apr 2024, 23:16

hi rockitdontstopit ,thanks for the confirmation that the buy button gets detected and executed in IB-Tws(Options Greeks).

Please share the buyicon cropped image on an external link(eg Google Drive) ,as it has been deleted from here.
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: ImageSearch for full screen region (ErrorLevel = 1 )

04 Apr 2024, 06:26

srt wrote: Please share the buyicon cropped image on an external link(eg Google Drive) ,as it has been deleted from here.
What image has been deleted? There are two images attached to his message above. They are a single pixel in height, so perhaps you missed them.
rockitdontstopit
Posts: 98
Joined: 12 Nov 2022, 15:47

Re: ImageSearch for full screen region (ErrorLevel = 1 )

04 Apr 2024, 07:14

srt wrote:
03 Apr 2024, 23:16
hi rockitdontstopit ,thanks for the confirmation that the buy button gets detected and executed in IB-Tws(Options Greeks).

Please share the buyicon cropped image on an external link(eg Google Drive) ,as it has been deleted from here.
The images are in my first post (hard to see since 1 pixel height). They're just cropped images of the images you uploaded.

Sometimes ImageSearch doesn't find the image even though the image is findable by autohotkey. That's the reason I loop the search until ErrorLevel = 0.

If the image may or may not be present and you don't want to be stuck in an infinite loop, I find myself doing a 2 loop for the ImageSearch. If ahk doesn't find it the first time, it almost always will the 2nd time, given that the image is findable.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], CoffeeChaton, ntepa, wjt936826577 and 160 guests