Page 1 of 3

help in code to fix script with image

Posted: 16 Sep 2021, 10:30
by Green Astronaut
I'm trying to create a script using images, but I'm having a bit of difficulty in the logic of the code,
I will explain how I would like it to work.

this is the image of the game:
https://imgur.com/a/nkfLkrb

these are the images i want checked:

Hotkey_Spell Go check this image
https://imgur.com/a/U2M0XuQ

Hotkey_Item Go check this image
https://imgur.com/a/03Y78CU


I'm having difficulty defining correctly the coordinates that are marked by the red square, as I could do to define the search in these certain areas,

this is the code i'm trying to fix:

Code: Select all

#SingleInstance, Force
#IfWinActive, Pokemon
;==============================CONFIGURAÇÕES/CONFIGS==============================

;[Stage 1]
Hotkey_Spell = {f4}											

;[Stage 2]
Hotkey_Item = {NumpadMult}


;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
#NoEnv
#Warn
#SingleInstance Force
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
SendMode Input
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1

SetTimer, AutoScript, 200 ;ler a cada 0,2 segundos

;===================AUTO HEALING SCRIPT=================
AutoScript:
If WinActive("ahk_class Qt5QWindowOwnDCIcon")
{
	ImageSearch, FirstStageX, FirstStageY, A_ScreenWidth - 368, 0, A_ScreenWidth, A_ScreenHeight + 470, *15, Imagens\manashield.png		;% Stage 1 [HOTKEY SPELL]
	if (ErrorLevel = 1)
	{
	ImageSearch, SecondStageX, SecondStageY, A_ScreenWidth - 368, 0, A_ScreenWidth, A_ScreenHeight + 470, *15, Imagens\utamo.png	;% Stage 2 [Hotkey Item]
	if (ErrorLevel = 1)
	{
                ControlSend,, %Hotkey_Item%, Pokemon ;Hotkey Second Stage [Hotkey Item]
		Sleep 50
		goto AutoScript
	}
                ControlSend,, %Hotkey_Spell%, Pokemon ;Hotkey Primeiro Estagio [Hotkey Spell]
		Sleep 50
		goto AutoScript
	}
}
return


;=============HOTKEYS===========
F20::
	Pause
return

now I'm going to explain how I need this code to work:


first i would like it to check the top part looking for the purple color bar.

if you find the image:
https://imgur.com/a/U2M0XuQ

go look for the second image :

https://imgur.com/a/03Y78CU

if the second image is found, press the hotkey inside the Hotkey_Item variable

and if it is not found, press the hotkey inside the Hotkey_Spell variable


Can someone help me fix this code?


my monitor resolution is 1360x768

Re: help in code to fix script with image

Posted: 16 Sep 2021, 16:43
by mikeyww
Although I did not test this, I noticed that you have the wrong syntax for ImageSearch.
Options: Zero or more of the following strings may be also be present immediately before the name of the file. Separate each option from the next with a single space or tab. For example: *2 *w100 *h-1 C:\Main Logo.bmp.
This is also shown in example #2 of the documentation.

If your script does not work, then test a one-liner instead, to get it working. Use an exact image, remove the variation parameter, and then display the ErrorLevel after your search. You can issue a MouseMove to your coordinates, to see where they are.

Re: help in code to fix script with image

Posted: 16 Sep 2021, 21:18
by Green Astronaut
Can you help me fix it? I've tried several ways and I wasn't successful, I tried to create something with pixel and I couldn't get it either.

Re: help in code to fix script with image

Posted: 16 Sep 2021, 21:28
by mikeyww
Here is a test that you can run.

Code: Select all

image = Imagens\manashield.png
If !FileExist(image)
 MsgBox, 48, Error, File not found.`n`n%image%
F3::
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image%
If ErrorLevel
     MsgBox, 48, Error, ErrorLevel = %ErrorLevel%
Else MsgBox, 64, Found, x = %x%`ny = %y%
Return
If it does not work, capture a new image, save it in PNG format, and ensure that this image is visible on your screen. Press F3 to run the search.

With complex images and transparencies, try using an image that is just a small cropped area.

Re: help in code to fix script with image

Posted: 17 Sep 2021, 11:19
by Green Astronaut
perfect I took the test and it found both images. but I would like the image to be searched
Hotkey_Spell
https://imgur.com/a/U2M0XuQ
from coordinate x = 910 onwards
would it be possible to do this? and can you help me fix this code so i can test it completely?

I would just like it to find the first image from the coordinate x = 910 and y= 39

would it be possible to adapt this code below to do this? or could you help me create something simple yet functional to do this?
as far as the images are concerned, the two were found accurately

Code: Select all

#SingleInstance, Force
#IfWinActive, Pokemon
;==============================CONFIGURAÇÕES/CONFIGS==============================

;[Stage 1]
Hotkey_Spell = {f4}											

;[Stage 2]
Hotkey_Item = {NumpadMult}


;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
;==============================!!!!!NAO EDITE NADA DAQUI PRA BAIXO!!!!!!==============================
#NoEnv
#Warn
#SingleInstance Force
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
SendMode Input
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1

SetTimer, AutoScript, 200 ;ler a cada 0,2 segundos

;===================AUTO HEALING SCRIPT=================
AutoScript:
If WinActive("ahk_class Qt5QWindowOwnDCIcon")
{
	ImageSearch, FirstStageX, FirstStageY, A_ScreenWidth - 368, 0, A_ScreenWidth, A_ScreenHeight + 470, *15, Imagens\manashield.png		;% Stage 1 [HOTKEY SPELL]
	if (ErrorLevel = 1)
	{
	ImageSearch, SecondStageX, SecondStageY, A_ScreenWidth - 368, 0, A_ScreenWidth, A_ScreenHeight + 470, *15, Imagens\utamo.png	;% Stage 2 [Hotkey Item]
	if (ErrorLevel = 1)
	{
                ControlSend,, %Hotkey_Item%, Pokemon ;Hotkey Second Stage [Hotkey Item]
		Sleep 50
		goto AutoScript
	}
                ControlSend,, %Hotkey_Spell%, Pokemon ;Hotkey Primeiro Estagio [Hotkey Spell]
		Sleep 50
		goto AutoScript
	}
}
return


;=============HOTKEYS===========
F20::
	Pause
return
I would just like it to find the first image of the coordinate x=910 and y=39 foward , it's because I'm just using a piece of the image and it's being found on the coordinate x=713 y=39.


but the image is all the same and I use a part of it, and I would like to search in the final part of the image on the game screen


i apologize for my not perfect english. I'm trying to explain as best I can

Re: help in code to fix script with image

Posted: 17 Sep 2021, 11:43
by mikeyww
Instead of testing everything at once, you can test some changes in a stepwise fashion. One step is below.

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found.
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return
Test this while your target window is active. You can see whether the two images are found, and your key is pressed correctly. After you verify that the script works, you can alter the coordinates to match what you need. After that works, you can see whether ControlSend works.

When you run a timer, you do not need a Goto command to execute the loop again, because that is what the timer does.

Re: help in code to fix script with image

Posted: 17 Sep 2021, 12:13
by Green Astronaut
worked perfectly. I just wanted to know how do I put area 910 = x and 39 = y for the first image

Re: help in code to fix script with image

Posted: 17 Sep 2021, 12:15
by mikeyww
You can just replace the 0, 0 with your specific values for x and y.

Re: help in code to fix script with image

Posted: 17 Sep 2021, 12:41
by Green Astronaut
I'm just having one problem now, when I set the coordinates to 0, 0 it's no longer identifying the image
the code is like this:

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 910, 39, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found.
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return
it stopped identifying only after I entered the coordinate, however the image is found, the complete image starts at the coordinate x = 713 y = 39 and ends x = 990 y = 39

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:06
by boiler
If your image’s upper-left corner is located at 713, 39, you won’t be able to find it with a search rectangle starting at 990, 39.

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:18
by Green Astronaut
I put it to locate in 910 39

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:29
by Green Astronaut
this is the whole picture
https://imgur.com/a/dpAqgFR

I would like to locate this piece of the image
https://imgur.com/a/7PBCWHS
however I would like it to be from the x910 y 39 coordinate and not before it, the coordinate is marked in green in the image signaling the location

I put the final coordinate at the bottom due to lack of space in the image I cropped

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:31
by boiler
How is the y value the same for each of those points? Is your reference image only one pixel high?

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:44
by Green Astronaut
sorry, the ending would be x990 y 48
sorry for my ignorance

Re: help in code to fix script with image

Posted: 17 Sep 2021, 13:53
by boiler
Can you post both the exact script that finds it followed separately by the exact script that fails to find it? Is the only change the start of the search rectangle from 0, 0 to 910, 39? How did you determine that the actual coordinates where that purple bar is located?

Re: help in code to fix script with image

Posted: 17 Sep 2021, 14:07
by Green Astronaut
the script that worked perfectly was this:

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found.
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return
however I just want it to search the first image from the coordinate x= 910 y = 39. (top left coordinate I want the search to start)

it worked looking from the coordinate x = 713 y = 39 (top left coordinate)

in the game the image starts at coordinate x = 713 y = 39 and ends at x = 990 y = 48

I would like the search for the first image to start from the coordinate x = 910 y = 39 and

Re: help in code to fix script with image

Posted: 17 Sep 2021, 14:13
by boiler
What does it show when you run this:

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found at %x% %y%
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return

Then does this one work?

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 910, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found at %x% %y%
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return

Re: help in code to fix script with image

Posted: 17 Sep 2021, 14:21
by Green Astronaut
I will test now

Re: help in code to fix script with image

Posted: 17 Sep 2021, 14:28
by Green Astronaut
when I text with this code: :

Code: Select all

 #Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found at %x% %y%
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return


it returns saying the image was found and starts using the "numpadmult" key
then when image two is not found he presses the "f4" key

already this code:

Code: Select all

#Persistent
dir    = Imagens
image1 = %dir%\manashield.png
image2 = %dir%\utamo.png
Loop, 2
 If !FileExist(image := image%A_Index%)
  MsgBox, 48, Error, File not found.`n`n%image%
Loop {
 WinWaitActive, ahk_class Qt5QWindowOwnDCIcon
 SetTimer, Check, 400
 WinWaitNotActive
 SetTimer, Check, Off
 SoundBeep, 1000
}
Return

Check:
SoundBeep, 1500
CoordMode, Pixel
ImageSearch, x, y, 910, 0, A_ScreenWidth, A_ScreenHeight, %image1%
If ErrorLevel
 Return
MsgBox, 64, Image1, The first image was found at %x% %y%
ImageSearch, x, y, 0, 0, A_ScreenWidth, A_ScreenHeight, %image2%
Send % ErrorLevel ? "{F4}" : "{NumpadMult}"
MsgBox, 64, ErrorLevel, %ErrorLevel%
Return
I just hear the beeps and there's no action

Re: help in code to fix script with image

Posted: 17 Sep 2021, 14:36
by boiler
I wanted you to tell me what the message said when it was found. It now reports the coordinates where it was found.