click image with confidence python - python

I'm trying to build a macro for a game I play. I want to use pyautogui to find an image with confidence=(0.2) and then move the mouse to its position and click. I'm not sure what to do from here. It will find the image and print but when I try to add pyautogui.click('forgedspirit.png') I get an error: cannot unpack non-iterable NoneType object.
def new_method():
if pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2)) != None:
print("I can see it.")
pyautogui.click('forgespirit.png')
time.sleep(0.5)
else:
print("image not found")
time.sleep(0.5)
new_method()

instead of telling pyautogui to click the image, you should tell it to click the coordinates of the image.
Try this:
try:
pos = pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2))
if pos != None:
print("I can see it.")
pyautogui.click(pos[0], pos[1])
time.sleep(0.5)
else:
print("image not found")
time.sleep(0.5)
except:
print("image not found")

Related

Pyautogui Randomize Clicks locateonscreen and moveto

Hello I want to Randomize Clicks inside Pyautogui if it found a locateonscreen image or if it found pixelmatch color
so i tryed something but i cant check it becorse of the script is only use my part of code after other actions bevore
i have no idea to try only this function
i tried
def DisclaimerCheckAndClick():
try:
# Diclaimer 1
print("locate DisclaimerBingAccept01.png")
sleep(1)
x,y = locateCenterOnScreen("DisclaimerBingAccept01.png",region=(10,400,920,630), confidence=0.7)
click(x,y)
print("clicked DisclaimerBingAccept01.png",(x,y))
sleep(5)
pyautogui.hotkey('f5')
print("hotkey('f5') pressed..")
print("sleep(10)")
sleep(10)
ClickThirdTab()
# Doesn´t work for bing
#try:
#x,y = locateCenterOnScreen("BingSearchInputfieldSymbol.png",region=(10,160,920,630), confidence=0.7)
#pyautogui.moveTo(x,y,0.5)
#sleep(1)
#click(x,y)
#sleep(0.5)
#click(x,y)
#print("clicked BingSearchInputfieldSymbol.png",(x,y))
#except:
#print("BingSearchInputfieldSymbol.png not found")
except:
try:
# Diclaimer 2
print("locate DisclaimerGoogleAccept01.png")
sleep(1)
x,y = locateCenterOnScreen("DisclaimerGoogleAccept01.png",region=(10,400,920,630), confidence=0.7)
click(x,y)
print("clicked DisclaimerGoogleAccept01.png",(x,y))
sleep(5)
pyautogui.hotkey('f5')
print("hotkey('f5') pressed..")
print("sleep(15)")
sleep(15)
try:
x,y = locateCenterOnScreen("GoogleSearchInputfieldSymbol.png",region=(10,160,920,630), confidence=0.7)
pyautogui.moveTo(x,y,0.5)
sleep(1)
click(x,y)
print("clicked GoogleSearchInputfieldSymbol.png",(x,y))
pass
except:
print("GoogleSearchInputfieldSymbol.png not found")
I Try to Randomize this Disclaime Clicker script i found this :
DisclaimerYoutubeAccept01 = locateOnScreen('DisclaimerYoutubeAccept01.png',region=(10,400,920,630), confidence=0.7)
if Bank is not None:
pyautogui.moveTo(randomClick(DisclaimerYoutubeAccept01))
click()
print("clicked DisclaimerYoutubeAccept01.png",(DisclaimerYoutubeAccept01))
this isnt working so i tryed
DisclaimerYoutubeAccept01 = locateOnScreen('DisclaimerYoutubeAccept01.png',region=(10,400,920,630), confidence=0.7)
pyautogui.moveTo(randomClick(DisclaimerYoutubeAccept01))
click()
print("clicked DisclaimerYoutubeAccept01.png",(DisclaimerYoutubeAccept01))
this is my Def for Click
#Clicks location at x,y
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.1) #This pauses the script for 0.1 seconds
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
and this for the RandomCLICK(box)
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
but i cant check if it works script started but i cant check if this is working becorse the check of Disclaimer will only trigger if many things before are tryed
so what you think?

Why my indentation can not work in python while True: loop

hello in my code is a lot of error that we can remove only with restarting a script so I use a while true loop and try: to continue but I am still in the inclosed cycle. But please a code in if and else is not important only a indentation
while True:
try:
# video tas can be used for an image
if len(video_tags) == 1:
#That meaan that a code1 or code2 function will be activated
# I deleted a code1 function because a gifs was in the bad quality
code2_function()
print("Function code2 has been activated")
check_px("code_img.png")
image_resize("code_img.png",1920,1080,"res_image.png")
if get_rgb("res_image.png",1730,11) > 700:
print("white")
img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
else:
print("black")
img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png")
audiovizualizere("output.mp3","prep_image.png")
#Everithink is done
else:
print(video_tag)
video_or_image = random.choice(["image","video"])
print(f"This a random picker to video_or_image: {video_or_image}")
if video_or_image == "image":
pexels_or_pixabay_image = random.choice(["pexels","pixabay"])
print(pexels_or_pixabay_image)
if pexels_or_pixabay_image == "pexels":
print("A image to video from pexels has been activated!")
image_to_video_from_pexels(video_tag,"image_from_pexels.png")
check_px("img_from_pexels")
image_resize("img_from_pexels.png",1920,1080,"res_image.png")
if get_rgb("res_image.png",1730,11) > 700:
print("white")
img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
else:
print("black")
img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png")
else:
# this is only temporary
print("A image to video from pexels has been activated!")
image_to_video_from_pexels(video_tag,"image_from_pexels.png")
check_px("img_from_pexels")
image_resize("img_from_pexels.png",1920,1080,"res_image.png")
if get_rgb("res_image.png",1730,11) > 700:
print("white")
img_edit("res_image.png","QR_white.png",1580,20,"prep_image.png")
else:
print("black")
img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png)
audiovizualizere("output.mp3","prep_image.png")
# Video section good
else:
# code here is not important
break
except Exception:
continue
where I have a problem
You're missing ") at the end of line 49, the line that says img_edit("res_image.png","QR_black.png",1580,20,"prep_image.png

how to if image found on a region then press key

i want to make this script to detect if an image on a region change image it press key
if pyautogui.locateOnScreen(image=r'C:\Users\ether\Desktop\New folder (4)\saveimage.png')(region=(800,460,90,90), grayscale=True, confidence=0.5):
elif pyautogui.locateOnScreen('yellow.png', confidence=0.8) !=None:
keyboard.press_and_release('d')
elif pyautogui.locateOnScreen('blue.png', confidence=0.8) !=None:
keyboard.press_and_release('a')

Why is cv2 showing a blank grey square?

I have a python program using the cv2 library, that chooses a random US state, pulls the image of the state from a folder and displays it. It was working fine, so I saved it, and suddenly, instead of showing the image, it shows a blank grey square and crashes. Why is it doing this, and how can I fix it? Heres the sample images:
and heres the code:
import cv2
import random
import time
#makes a list of the states
def states():
states = ['alabama','alaska','arizona','arkansas','california',
'colorado','connecticut','deleware','florida','georgia','hawaii',
'idaho','illinois','indiana','iowa','kansas','kentucky','louisiana',
'maine','maryland','massachussets','michigan','minnesota',
'mississipi','missouri','montana','nebraska','nevada',
'new_hampshire','new_jersey','new_mexico','new_york',
'north_carolina','north_dakota','ohio','oklahoma','oregon',
'pennsylvania','rhode_island','south_carolina','south_dakota',
'tennessee','texas','utah','vermont','virginia','washington',
'west_virginia','wisconsin','wyoming']
while True:
#choose a random state from the states list
state = random.choice(states)
#take a picture from the states folder, and display it
img = cv2.imread('picture_files/states/' + state + '.png')
cv2.imshow('guess the state!', img)
#checks if you typed the right state,
#and gives an appropriate response
guess = input("guess the state! (type stop to stop!)\n")
if guess.lower() == state:
print("Correct!")
time.sleep(2)
print("Lets do it again!")
elif guess.lower() == "stop":
break
else:
print("Nope! It was " + state + ". Keep trying!")
time.sleep(2)
if __name__ == '__main__':
states()
Basically you are missing some cv2.waitKey() function to show the image (see also here).
This is a possible example for a solution.
def pick_a_state():
states = ['a','b'] # replace with your list
return random.choice(states)
def show_state(state):
img = cv2.imread(state + '.png', cv2.IMREAD_UNCHANGED)
cv2.imshow('guess the state!', img)
cv2.waitKey(1000)
def get_the_answer(state):
guess = raw_input("guess the state! (type stop to stop!)\n")
if guess.lower() == state:
print(state)
print("Correct!")
time.sleep(2)
print("Lets do it again!")
return 1
elif guess.lower() == "stop":
return 0
else:
print("Nope! It was " + state + ". Keep trying!")
time.sleep(2)
return 1
if __name__ == '__main__':
while True:
state = pick_a_state()
show_state(state)
if get_the_answer(state) == 0:
cv2.destroyAllWindows()
break
Please analyze the code to understand how it works. Hope can help.
I've had an error like this before where opencv would not open and have been successful in a few ways to solve it.
Add cv2.waitKey(0) at the end of your function and then add
cv2.destroyAllWindows() underneath your calling of states()
like this:
if __name__ == '__main__':
states()
cv2.destroyAllWindows()
Install opencv-contrib-python.
Try installing it with the command pip3 install opencv-contrib-python==4.4.0.46.

how to make locateCenterOnScreen more accurate -PYTHON-, -WINDOWS-

Hello my goal is to be able to make my bot click the thing of my choosing on any screen size because I think that is the main issue. Ive tried to decrease the confidence level but it just ends up clicking something else with the same general color. ive tested it with an EXACT image and it clicks the correct spot so its not like the coordinates are off or anything its just the image recognition.
These are the images to go by
(X1, NextLesson, Arrow)
from pyautogui import *
import pyautogui
import time
import keyboard
import random
def NextLesson():
keepGoing = True
while keepGoing == True:
counter = 0
nl_coordinates = pyautogui.locateOnScreen('images/nextLesson.png', confidence=0.4)
print(nl_coordinates) # This will print out where it is
if nl_coordinates:
print(f"I can see it at {nl_coordinates}")
pyautogui.click(nl_coordinates)
keepGoing = False
else:
print("I cannot see it.")
def Arrow():
keepGoing = True
while keepGoing == True:
counter = 0
arrow_coordinates = pyautogui.locateOnScreen('images/arrow.png', confidence=0.4)
print(arrow_coordinates) # This will print out where it is
if arrow_coordinates:
print(f"I can see it at {arrow_coordinates}")
pyautogui.click(arrow_coordinates)
keepGoing = False
else:
print("I cannot see it.")
def X1():
keepGoing = True
while keepGoing == True:
counter = 0
x1_coordinates = pyautogui.locateOnScreen('images/x1.png', confidence=0.4)
print(x1_coordinates) # This will print out where it is
if x1_coordinates:
print(f"I can see it at {x1_coordinates}")
pyautogui.click(x1_coordinates)
keepGoing = False
else:
print("I cannot see it.")
while True:
counter = 0
counter2 = 0
true = True
time.sleep(2)
X1()#
time.sleep(8)
NextLesson()#
time.sleep(10)
Arrow()#
print("calibration complete ")
time.sleep(5)
cords = pyautogui.position()
while counter != 1800:
time.sleep(60)
pyautogui.click(cords) #clicking where ouse is at
print("clicked")
counter += 60
print(counter)
if counter == 1800:
time.sleep(5) #stops code for 5 secs
X1() #clicks mouse to x button
print("clicked x")
time.sleep(5) #stops code for 5 secs
NextLesson() #clicks mouse to the assignment button
print("clicked assignemnt")
time.sleep(15) #stops code for 2 secs
Arrow() #clicks mouse to the second assignment button
print("clicked 2nd assignment button ")
time.sleep(5) #waits 5secs to put cursor at position
cords = pyautogui.position() #grabs position
print("grabbed position")
We can use opencv-python to perform multiscale template matching. The idea is to scale the template image and attempt to locate the resized template in the screenshot. The code below (adapted from here) loops over 50 scaling parameters in the range [0.25,2] and selects the one the that provides the best match. You might want to decrease the range or number of scaling parameters for efficiency. It is also better to save the correct scaling parameter and reuse it for multiple images.
import cv2
import pyscreeze
import numpy as np
import imutils
import pyautogui
def template_match_with_scaling(image,gs=True,confidence=0.8):
"""
Locate an image and return a pyscreeze box surrounding it.
Template matching is done by default in grayscale (gs=True)
Detect image if normalized correlation coefficient is > confidence (0.8 is default)
"""
templateim = pyscreeze._load_cv2(image,grayscale=gs) # template image
(tH, tW) = templateim.shape[:2]
screenim_color = pyautogui.screenshot() # screenshot of image
screenim_color = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_RGB2BGR)
if gs is True:
screenim = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_BGR2GRAY)
else:
screenim = screenim_color
#try different scaling parameters and see which one matches best
found = None #bookeeping variable for the maximum correlation coefficient, position and scale
scalingrange = np.linspace(0.25,2,num=50)
for scale in scalingrange:
resizedtemplate = imutils.resize(templateim, width = int(templateim.shape[1]*scale) ) # resizing with imutils maintains the aspect ratio
r = float(resizedtemplate.shape[1])/templateim.shape[1] # recompute scaling factor
result = cv2.matchTemplate(screenim, resizedtemplate, cv2.TM_CCOEFF_NORMED) # template matching using the correlation coefficient
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result) #returns a 4-tuple which includes the minimum correlation value, the maximum correlation value, the (x, y)-coordinate of the minimum value, and the (x, y)-coordinate of the maximum value
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
(maxVal, maxLoc, r) = found
if maxVal > confidence:
box = pyscreeze.Box(int(maxLoc[0]), int(maxLoc[1]), int(tW*r), int(tH*r) )
return box
else:
return None
def locate_center_with_scaling(image,gs=True):
loc = template_match_with_scaling(image,gs=gs)
if loc:
return pyautogui.center(loc)
else:
raise Exception("Image not found")
#sample usage
coords = locate_center_with_scaling('images/arrow.png')
The answer I found is taking a more accurate photo of what I need like trying to remove the background as much greyscale=ture and adding more confidence

Categories

Resources