6

I'm building a simple program with Tkinter to be distributed based on Python Pyautogui that has to click on certaing png images. These images change every month so I'd like to have these on a web site where I update them every time they're needed so I don't have to compile the program and redistribute it each time. Example

import pyautogui
# instead of this:
location=pyautogui.locateOnScreen('clickable_image.png')
pyautogui.click(location)

# I would like something like this:
location=pyautogui.locateOnScreen('https://www.example.com/clickable_image.png')
pyautogui.click(location)
Filip Müller
  • 1,096
  • 5
  • 18
  • 3
    You could download the image in a local folder and use the first approach. See https://stackoverflow.com/a/37821542/9282844 for downloading images from URLs. – Guimoute Jul 26 '22 at 23:04

1 Answers1

2

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.

Filip Müller
  • 1,096
  • 5
  • 18