Using a PIL Image
Even though it doesn't seem to be mentioned in the docs, pyautogui.locateOnScreen
can also take a pillow
Image
as the argument, apart from a file name. Thus, you can use PIL
(will need to be installed with pip install pillow
) and the built-in requests
module to get the image from a URL and then pass it to the locateOnScreen
function.
import pyautogui
from PIL import Image # pip install pillow
import requests
URL = "https://picsum.photos/200"
response = requests.get(URL, stream=True) # get the picture data from url
image = Image.open(response.raw) # create a pillow Image from the data
pyautogui.locateOnScreen(image) # now you can pass the image as argument
Requests take time and resources
Note that it would be fairly inefficient and slow to retrieve the image from the url like this every single time you want to locate it. So it would be best to make the request and create the pillow
Image
only once, when the program starts. Then use this Image
the whole time the program runs. Obviously this wouldn't work if the image were to change every minute or so, but if it will only update once a month, you don't need to constantly get the same image from the server again and again.
Other solution - save the image locally
Another possible solution would be to get the image from the url, save it locally and then use it like you're used to. Check out this question on how to save an image from a url locally. Once again, it would be best to only save the image once on program start. Note that unless you somehow delete these locally stored images, they will keep piling up.