I'm new to python so I'm trying to make a python bot for a simple game. I tried making a function that clicks on any color I want to, but I keep getting errors saying "The Pillow Package is required to use this function" does anyone know why? Here is the function.
def findColor():
for x in range(s.width):
for y in range(s.height):
if s.getpixel((x, y)) == color:
click(x, y)
And I made a function called click that will click in the place I want so that isn't the bug, color is a variable I made so I can change it if I need to, and s is a screenshot. The variable for s is
s = pyautogui.screenshot()
Also I am very new to python so if I did something really dumb please go easy on me. Thank you!
This problem is occurring since the screenshot function uses the PIL module,
This may help: https://pyautogui.readthedocs.io/en/latest/screenshot.html
according to the documentation,
"Screenshot functionality requires the Pillow module. "
since s uses the screenshot module and returns a Image object, therefore PIL needs to be installed.
Related
I am not sure what it is actually called. I am a beginner. I am learning python and using VS Code as my code editor. But when I write codes the message in the red circle highlighted in the screenshot pops up. It's kind of annoying.
Can someone please help me with it? I don't know what it is called and would like to turn it off.
That pop up just shows the structure of the function you are using. It includes parameter hints.
In the image it is showing the structure of print function. It's a useful feature to be honest.
Press esc to cancel out of an individual popup, or set"editor.parameterHints.enabled": false to disable it entirely.
So, that box you have there is the documentation of the method you are calling. In this case, print(). It's actually quite useful. It'll give you a quick description of the parameters that you can pass into the function. For more confusing functions, the auto-documentation is a must have. But, of course, it is up to you.
You can disable it by setting "editor.parameterHints.enabled" to false in your .json
This is a function provided by VS Code to display information such as method parameters. You can turn it off in settings.
Click the gear icon in the lower left corner and select Settings,
then search for parameterhints, and uncheck the options in the picture.
In addition, as mentioned above, you can also add the following configuration to the settings.json file to achieve the same effect.
"editor.parameterHints.enabled": false
So I'm playing around with some code and I'm trying to design a program that displays an image if a true statement results from the input. An example would be as follows:
name = input('name: ')
if name == 'Sammi':
# Here is where I would put the command to open an image. In place of print or whatever.
# I need some help with exactly what function to put here though.
The idea is that if the input string matches 'Sammi', then an image of me will be displayed preferably in a separate window, but I'm not exactly sure if that's possible or practical.
I've seen some guides that use PIL but the process of downloading and installing the required software is really tedious and I just have to wonder if it is actually required. I primarily use PyCharm for my code and occasionally ill go over to Notepad++ but it's mostly PyCharm. I'm not sure if that information is helpful but I thought I would provide it.
The image I want to use is located on my desktop and ideally would use the path C:\users\sammi\OneDrive\Desktop\B&W_2.jpg
My question as stated before is: exactly what function will allow me to do this? When you answer I would also really appreciate it if you explain the purpose of certain operators like fromand or for example, or whatever other operators or functions are used. I'm still fairly new to this stuff but I want to get really good at it.
Short reply, if you are running Python from the Standard Library, you can't, directly. The Standard Library is mostly a back-end library, so it works with logic, but doesn't "show" anything. This means that there's no way with the Standard Library to show images, you can however launch a process which opens the images.
E.g. Microsoft Paint
import subprocess
def open_image(path:str) -> None:
command = f'mspaint {path}'
subprocess.Popen(command)
open_image(input('Type your path: '))
If you don't want paint or another application, you should find a library that does it, be it Pillow or others.
To do this "right", you will need to learn about GUI programming with a library such as tkinter. Otherwise, the quick and dirty way is to use Image.show() from PIL. The documentation states that this is only intended for debugging purposes, so you should avoid it as a permanent solution in a serious project.
First of all, it is a good thing to know how to install packages (the things you can import in your scripts to use functions others made). It will be usefull when doing more complicated projects.
In Pycharm, you can add packages to your project:
File -> Settings -> Project: Project_Name -> Python Interpreter
Clicking on the + icon and searching pillow
This is not really used because there are better ways of installing packages
These are the two main ways of installing packages:
Using pip (https://pip.pypa.io/en/stable/installation/)
Using conda (https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html#regular-installation)
Both of these allow you to install packages.
I would recommend using conda because it is easier to separate projects from one another and allows the installation of packages that are not available using pip.
You will then be able to install PIL in the command line using
pip install Pillow
or (conda requires more setup, I encourage you to read https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)
conda install -c anaconda pillow
After installing the package you want here pillow, you can then use it in your code:
from PIL import Image
#from is used to specify the package to import
#import is used to specify which module you want to import in that library.
#The module contains functions you can use. (It's more complicated than that, but for basics this is what is does)
name = input('name: ')
if name == 'Sammi':
im = Image.open("sample-image.png") #Open the image
im.show() #Show the image on screen
If you prefer to avoid using any PyPi you can use tkinter.
# As to not to pollute the namespace, from module 'tkinter' only import these classes, and make it
# so you don't have to type tkinter.Whatever() every time you want to have a class instance
from tkinter import Tk, PhotoImage, Label
# Allow for case-insensitive inputs.
if input('Name: ').casefold() == 'sammi':
# Initialize graphical window instance
window = Tk()
window.title("StackOverflow Example")
# Load the image
image = PhotoImage(file=r"C:\users\sammi\OneDrive\Desktop\B&W_2.jpg")
Label(window, image=image).pack()
# Bring the window to the front once it appears
window.lift()
window.attributes('-topmost', True)
window.after_idle(window.attributes, '-topmost', False)
# Make window appear with all parameters set as described above
window.mainloop()
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')
import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))
Instead of returning coordinates, it returns None.
My problem is Solved when I took screenshot by pyautogui inbuilt function rather than taking WIN+Printscr because if we took screenshot by WIN+Printscr then pixel density and other image related data may be different in comparison to pyautogui inbuilt function.
Maybe this thing worked for you, for me it worked.
For Ex - wifi.png so first I took full screenshot and I cropped it from that full image then I put this in my code shown below
import pyautogui
print(pyautogui.locateCenterOnScreen('wifi.png'))
Seems like it couldn't find anything matching your image on the screen.
locateCenterOnScreen(image, grayscale=False) - Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Returns None if not found on the screen.
The initial problem is quite simple - the library does not find the image passed represented on the screen and therefore returns None rather than the co-ordinates as it says it will in the docs.
However, there is a possible misunderstanding here, in particular from a user who posted a bounty on the question and posed a similar question here.. A comment was made
"The pictures are on my desktop"
When you use this function, you pass in a filename as a string. The library then loads the image file and looks for the picture on screen (not the filename). pyautogui.locatecentreonscreen() will look for the actual image if it is visible on the screen. It does not look for files on the desktop, or file icons with the same name as the image passed to it.
Example
Say you have a file with the name flower.jpg containing the following image, saved on your desktop.
With no other windows open, run:
coords = pyautogui.locateCenterOnScreen('C:\\Richard\\Users\\flower.jpg')
print(coords)
The result is None
This is because that image is not displayed on my screen even though an icon is on the desktop, with the name flower.jpg. This is true even if that icon is a small scale version of the flower.
However, if I leave the image visible (as I'm preparing this post) and do the same thing, I get co-ordinates - e.g.:
As you see - because the actual image is on the screen, the library finds it, with co-ordinates 524,621
In summary if the library doesn't find the image displayed to the user on the screen, it will return None. Note the image has to be visible to the user at the point at which the code is running. It won't find the icon on your desktop, or similar, or the image in a window that is "hidden" behind another. Is that what you're trying to do?
Are you sure that the image is of the same size as of the icon?
If not pyautogui.locateCenterOnScreen() will raise TypeError: 'NoneType' object is not iterable
Also make sure that the full icon is visible and looks the same as the image:"C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"
Hope the problem is solved!
Building off of what Don Kirby said, no matching image was found on the screen. You could open the image in, for example, Windows Photo Gallery, (or Tk) and then pyautogui would find it.
Good explanation, is there any library that work better than pyautogui? I mean it wants excatly the same picture on the screen. We need similar sometimes. – GLHF May 11 '16 at 15:45
Try using this code line:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
I believe confidence range from 0.1-0.9.
Unless you have several pictures looking almost alike, this might solve the exception.
If that doesn't work try making a second screenshot with more/less of the original image and write this code:
try:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
except TypeError:
pyautogui.locateCenterOnScreen("yourscreenshot2.PNG", confidence=0.9)
This will give it a second try with a slightly different picture, and hopefully not return a TypeError.
If you can't use pyautogui.locateCenterOnScreen() because of image problem , try using the snipping tool (if you are on Windows) to take screenshots.It works.
Also make sure that you have downloaded the "Pillow" module
Try this :
pip install opencv-contrib-python
It confused me a lot that I ran the same code:
coords =pyautogui.locateCenterOnScreen('C:\\test.jpg')
in two different virtual environment( X and Y, almost same) returned None and Point(x=1543, y=461).
I read Aleks's answer and guess it use the parameter confidence implicitly when opencv-contrib-python in current environment(which Y had but X hadn't).
I didn't dig in but just installed opencv-contrib-python in virtual environment X and solved my problem.
Edit:
It might help to know that I'm using python 2.7.9 (that's what was taught in my GIS class).
I've almost got it working I think. Although now it's a new question.
I have this code
from PIL import Image
im = Image.open("C:/users/Chrostopher/Asuna.png")
There are no error messages and my screen flashed black like it wanted to do something, but the picture didn't show/open/display. What should I do?
Thanks for all the help so far. I feel like I'm slowly (and with many mistakes) learning something useful.
Old:
I am very, very new at this, which is why I'm asking. I've looked around for help, but there's always one thing I don't understand and it's just turned into a very deep rabbit hole.
When I've tried the code I've seen here, it doesn't work. Looking further, I need the Python Image Library (PIL). I've downloaded it, but I can't figure out how to set it up to work in Python. The file is a .gz. Is there some place I need to put the file or some way to import it?
If you could answer step by step, that would be wonderful for this extreme newb.
This is the code I have (to try and open an image which is the end goal)
import Image
def main():
filename = "desert.jpg"
image = Image.open(filename)
image.show
del image
if (__name__ == "__main__"):
main()
Is there something I'm missing or not doing right that is messing up what I'm trying to do?
First install Pillow with "pip"
$ pip install Pillow
Then, instead writing
import Image
In the first line, you can use:
from PIL import Image