Pyautogui not detecting images - python

In one of my scripts, which used to work for about 2 months, I used the pyautogui module and the script remained unchanged. For the past 2 days, I have been trying to script something else to detect objects on my screen but it does not detect it.
Code:
import pyautogui
pyautogui.locateCenterOnScreen('Image.png')
In the above code, when I should get some output, I get None.
Any idea what I should do?
Thanks.

Please include the code you have tried so far along with the question. how to ask questions?

Related

How can i fix my problem with pyautogui.click?

Need to mention that im new with Python and i decided to create a bot for multiplayer game, to autobuy items on auctionhouse, using opencv and pyautogui, so far everything was going pretty well, the cursor was heading to the right point on the screen (reload auction), but
pyautogui.click(clicks=1) isnt working in game window.
IDE (PyCharm) is running with admin rights, googled alot about the topic, but nothing works so far. Will be pleased if anyone could help me, this is my first big project i really want to work with, so hopefully you guys can help me :D
additional info: game uses Battle Eye anticheat, engine Java (probably... Game is called Stalcraft, you can find it on steam, looks like its something Minecraft-based, but im not sure about it)
OS: Win 10 x64
Python:3.11
What i tried:
pyautogui library (tried pyautogui.MoveTo(x,y) and the method with pyautogui.locateCenterOnScreen("whatever.png",confidence=0.85 Need to mention that first method works only in IDE, the second one based on img recognition also works with browser. Tried this in other apps, but no results. It's just hovering cursor on the right place, but no clicks at all)
pydirectinput library
Here's what i got so far
import cv2
import random
import pyautogui
from time import sleep
import imutils
import numpy as np
import pydirectinput
pyautogui.FAILSAFE=True
rng=random.uniform(0.87, 1.3)
sleep(5)
pyautogui.size()
print(pyautogui.size())
pyautogui.position()
print(pyautogui.position())
pyautogui.moveTo(x=1344, y=342, duration=rng)
pyautogui.click(1344, 342, clicks=5)```
This might be very silly. But I had no luck with .click. In my sample I used .moveTo as you did, but then .mouseDown() and .mouseUp() to simulate a click. Also sometimes it required a delay between the two. I wonder if that combo would help instead?
pyautogui.mouseDown()
sleep(1)
pyautogui.mouseUp()

Window resolution in Tkinter

I know that this might sound weird, but I have a problem considering the screen resolution of my Tkinter program. I have attached two pictures for a comparison.
I think you can clearly tell, what I'm talking about. The problem is that I don't really know why this happened. Suddenly the tkinter window had a way better resolution as you can see in the top picture. I did not change anything related to screensize/resolution by myself and was therefore expecting Tkinter to have published an update which just has better textures for every widget.
But because I had to reinstall python and all its modules this is no longer the case and the widgets look very blurry again (Bottom picture). Now I'm asking myself whether this has something to do with another version of Python or tkinter I'm using or what else could have been the reason for that.
I did not change my PC, Screen or anything like that. I just reinstalled python and its modules. Sadly I don't have the source code of the version where it looked great, but only an .exe file, because im continuously updating my program. But as I said before, I don't know what I could have changed which led to this huge resolution improvement.
I hope you get, what I'm trying to tell you.
Thanks in advance!
I was able so solve it!
I had a look on this question: on Stackoverflow
The answer from #binaryfunt solved my problem completely. So it was not really a tkinter/python thing, but rather windows itself.

Colab - Box for input() in python code too long so I need to scroll back to read the question

I plan to use colab notebooks to teach my pupils (12-13 year olds) Python. We will start with using input() and display with print() to ask simple questions and display the answer.
One problem I have come across is when I use input() in a code cell with an input string a really long input box is produced (in the output area) to get the input value. It is so long that the window scrolls to the end of this empty input box, and you have to scroll back to read the question, even when the window is at full screen width . This is really strange as the input box is completely empty does not need to to be bigger than the window or frame.
Are there settings I can change to prevent this or can I do something with the css to reduce the size of this input box?
This may seem a trivial problem, but little things like this can be distracting and add to the frustration of learning the language so I would like to prevent it if I can.
I have attached a picture that shows the problem.
picture that shows the problem.
I looked at the problem you are trying to solve. Colab fits inside my window perfectly when I size the window to full screen. I think this might be a problem with your browser. Are you using the latest version of Firefox or Chrome?
I just encountered the same problem. For me, it was because I was plotting a graph using the matplotlib.pyplot library before prompting for the input. After I removed that, it worked just fine.

Live Keyboard Input in Python?

I have a laser pointer that I'm using along with my webcam as a drawing tablet, but I'd like to use the extra buttons on it too. They seem to be bound to Page Up, Page Down, Escape, and Period. I can't seem to figure out a way to get the input(which is handled like it's a keyboard) without any windows being selected.
I've tried serial and pyusb, but I've had issues with both of those. I got it to work with Pygame, but as far as I know, you can't receive input without the window it creates being selected. Any ideas?
You could try making a python key-logger. However, it would be much easier to just use Pygame.
CodeSurgeon answered me in a comment.
Looks like there are a lot of youtube tutorials on the subject, surprisingly. This one shows a cross-platform approach using the pynput module, while this one looks to be using a windows-specific approach (pyhook and pythoncom). Can't vouch for either of these as I just found them through some searching, and I am sure there are others as well.
I found that pynput works for me. (Windows 10/Python 3.4)

pyautogui.locateOnScreen() Returns... Nothing?

I'm trying to use PyAutoGui's image recognition functions. (OS X)
Needless to say, I'm running into some slight issues that I can't seem to solve myself no matter where I look or what I do. I'm attempting to have PyAutoGui click on the Chrome shortcut based off a .png screenshot saved to my desktop.
Here's my code in terminal:
>>>import pyautogui
>>>chrome = pyautogui.locateOnScreen('/Users/ianscalzo/Desktop/chrome.png")
>>>
I get no backfire on my filepath, but it causes my shell/terminal to return nothing but go to a new line. (As shown in the code example above - Just causes terminal to go to a blank ">>>")
I don't really understand why it doesn't do anything but go to a new line, so any insight would be greatly appreciated.
Thank you so much!
After struggling with this forever also, finally figured out that you either use command line to take the screenshot or using the screenshot button with windows key. It doesn't work with the snipping tool.
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')

Categories

Resources