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
Related
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()
We are writing a very simple code for a game which automates an enhancing process (it's our own server so it's just for the fun of it) during this process you occasionally get a captcha which you have to solve in order to continue enhancing. We are stuck up on how we could solve the captchas and this is where we need your help. The code is written in python and is very simple. The captcha is also very simple it's only 3 numbers. (can't be anything else other than numbers from 0-9) Here is how the captcha window looks like: [https://i.stack.imgur.com/27mAK.png]
The code looks like this:
import pyautogui
import time
import keyboard
while True:
opt = pyautogui.locateOnScreen('asd.png', confidence=.95) #looks for a good enchant
forgat = pyautogui.locateOnScreen('forgatas.png') #locates the button to press for enhancing
stop = keyboard.is_pressed("shift") #stops the loop with shift
if opt:
print('Done')
break
if stop:
print('Stopped')
break
else:
pyautogui.click(forgat)
time.sleep(0.2)
Did some testing with pytesseract:
from cv2 import cv2
import pytesseract
import pyautogui
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread('ak.png')
text = pytesseract.image_to_string(img)
print(text)
It successfully converts the img to text but we can't figure out how to copy the text into the text-box in game. Copying the ingame text is not an option.
Would also like to ask you to give suggestions regarding speeding up the process while the locateOnScreen function is still able to keep up (don't want the code to skip over a good enchant for going too fast) and maybe using something else instead of time.sleep because it heavily taxes the system. Sorry if the code is messy we are still very much beginners and we never learned python before. Any help would be greatly appricated! Looking forward to any suggestion!
I suggest you to try this library I've found some time ago. If you have a set of labelled captchas that service would fit you. Take a look: https://github.com/punkerpunker/captcha_solver
In README there is a section "Train model on external data" that you might be interested in.
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.
I have a Python script that displays images fullscreen on a BeagleBoard with the GUI disabled. The script is started when the board boots. For this I use PyGame which works perfectly fine. Except for some reason the image qwality is scaled down. Because the images are stored in HQ I assume that PyGame resamples the image. I was unable to find out where this can be changed so I decided to replace PyGame, it also seems a bit much to "just" display an image.
I have the code below to display the image. According to documentation the default image viewer will show the image. (Which is supposed to be XV). But as soon as I run the code below where image is a filepath I get "sh: xv: not found".
from PIL import Image
im = Image.open(image)
im.show()
So I tried to install the XV package but can't find how to install it for Angstrom.
My question could either be "How to display images fullscreen with Python?" (To which the answer was supposed to be the code above). Or the question is "How can I get XV installed on Angstrom?" (What is the package name for opkg install)
I did search, but haven't found something that works...
Image.show() in PIL is more intended for debugging than actual, production use. It is hardcoded to call xv <temp-image-file-pil-creates>. You can hack around this (make a symbolic link called xv that will call some other image viewer), but it's still a rather bad way to go about it.
I don't know enough about the BeagleBoard to tell you the best/canonical way to display an image fullscreen, but if you got halfway there with PyGame, perhaps you can post your code and the community can help you fix the quality problem.
If the image is getting downscaled to fit the screen, you might look into using transform.smoothscale to scale the image manually (to avoid losing quality).