Python Forum
pyautogui.locateOnScreen producing error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyautogui.locateOnScreen producing error
#1
When I attempt to run
isburger = pyautogui.locateOnScreen('singleburger.png', region=(str(xregion,yregion,)+str(length,height)))
I keep getting the error
Error:
line 88, in <module> isburger = pyautogui.locateOnScreen('singleburger.png', region=(str(xregion,yregion,)+str(length,height))) TypeError: decoding str is not supported
yregion, xregion, length, and height variables are all integers

However, when left as an integer, it returns

Error:
TypeError: can only concatenate str (not "int") to str
Help greatly appreciated!
Reply
#2
region should be a tuple of 4 integers. So possibly you want

isburger = pyautogui.locateOnScreen('singleburger.png', region=(xregion, yregion, length, height))
Reply
#3
(Apr-17-2022, 03:56 AM)bowlofred Wrote: region should be a tuple of 4 integers. So possibly you want

isburger = pyautogui.locateOnScreen('singleburger.png', region=(xregion, yregion, length, height))

Ran that, got back
Error:
TypeError: can only concatenate str (not "int") to str
Reply
#4
If it helps, here is the full script:

import pyautogui
import time
import PIL

regionx = 0
regiony = 0
length = 1920
height = 1080

pyautogui.alert("Please put you cursor over the single burger selection button within 5 seconds of pressing OK")
time.sleep(5)
bplain = pyautogui.position()
print("single burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the double burger selection button within 5 seconds of pressing OK")
time.sleep(5)
bdouble = pyautogui.position()
print("double burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the lettuce burger selection button within 5 seconds of pressing OK")
time.sleep(5)
blettuce = pyautogui.position()
print("lettuce burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the bloxy cola selection button within 5 seconds of pressing OK")
time.sleep(5)
cola = pyautogui.position()
print("cola button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the fries selection button within 5 seconds of pressing OK")
time.sleep(5)
fries = pyautogui.position()
print("fries button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Put your cursor over the upper left corner of a customer's speech bubble within 5 seconds of pressing OK. Do not let the cash register images get in the box.")
time.sleep(5)
topleft = pyautogui.position()
print("top left is captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Put your cursor over the bottom right corner of a customer's speech bubble within 5 seconds of pressing OK. Do not let the cash register images get in the box.")
time.sleep(5)
btmrght = pyautogui.position()
print("bottom right is captured")
selection = pyautogui.alert("Calibration is complete. Please finish the current customer's order (if there is one) before pressing OK.")

## Finding the region of selection:

topleftstr = str(topleft)
btmrghtstr = str(btmrght)

topleftList = topleftstr.split(sep=", y=", maxsplit = 2)
endregionList = btmrghtstr.split(sep=", y=",maxsplit = 2)

print(str(topleftList))
print(str(endregionList))

regionx,regiony = topleftList
endregionx,endregiony = endregionList

regionxfixed = regionx.split(sep="=",maxsplit=2)
endregionxfixed = endregionx.split(sep="=",maxsplit=2)

regionyfixed = regiony.split(sep=")",maxsplit=2)
endregionyfixed = endregiony.split(sep=")",maxsplit=2)

nox,xregion = regionxfixed
print(xregion)

nox2,endxregion = endregionxfixed
print(endxregion)

yregion,noy = regionyfixed
print(xregion)

endyregion,noy2 = endregionyfixed
print(endxregion)

length = int(endxregion) - int(xregion)
height = int(endyregion) - int(yregion)

time.sleep(3)

##while True:

if True:
    isburger = pyautogui.locateOnScreen('singleburger.png', region=(xregion, yregion, length, height))
    if isburger == "ImageNotFoundException":
        isdburger = pyautogui.locateOnScreen('doubleburger.png', region=(str(xregion,yregion,)+str(length,height)))
        if isdburger == "ImageNotFoundException":
            pyautogui.leftClick(blettuce)
        else:
            pyautogui.leftClick(bdouble)
    else:
        print("found single burger")
        pyautogui.leftClick(bplain)
    isCola = pyautogui.locateOnScreen('cola.png', region=(str(xregion,yregion,)+str(length,height)))
    if not isCola == "ImageNotFoundException":
        print("found cola")
        pyautogui.leftClick(cola)
    isFries = pyautogui.locateOnScreen('fries.png', region=(str(xregion,yregion,)+str(length,height)))
    if not isFries == "ImageNotFoundException":
        print("found fries")
        pyautogui.leftClick(x=599, y=537)
    time.sleep(5)
Line 86 and below is what is giving the error.
Reply
#5
You didn't make the change everywhere, and you didn't show the full traceback to see which line caused it. I'm presuming that line 86 is now working, but 88 or 96 is now generating the error.
Reply
#6
(Apr-17-2022, 05:36 AM)bowlofred Wrote: You didn't make the change everywhere, and you didn't show the full traceback to see which line caused it. I'm presuming that line 86 is now working, but 88 or 96 is now generating the error.

86 is still producing the error:
Error:
Traceback (most recent call last): File "/Volumes/PortableSSD/untitled_folder/imagefinder.py", line 86, in <module> isburger = pyautogui.locateOnScreen('singleburger.png', region=(xregion, yregion, length, height)) File "/Users/dude8074/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 175, in wrapper return wrappedFunction(*args, **kwargs) File "/Users/dude8074/Library/Python/3.8/lib/python/site-packages/pyautogui/__init__.py", line 213, in locateOnScreen return pyscreeze.locateOnScreen(*args, **kwargs) File "/Users/dude8074/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 360, in locateOnScreen retVal = locate(image, screenshotIm, **kwargs) File "/Users/dude8074/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 340, in locate points = tuple(locateAll(needleImage, haystackImage, **kwargs)) File "/Users/dude8074/Library/Python/3.8/lib/python/site-packages/pyscreeze/__init__.py", line 259, in _locateAll_python haystackImage = haystackImage.crop((region[0], region[1], region[0] + region[2], region[1] + region[3])) TypeError: can only concatenate str (not "int") to str
with this script:

import pyautogui
import time
import PIL

regionx = 0
regiony = 0
length = 1920
height = 1080

pyautogui.alert("Please put you cursor over the single burger selection button within 5 seconds of pressing OK")
time.sleep(5)
bplain = pyautogui.position()
print("single burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the double burger selection button within 5 seconds of pressing OK")
time.sleep(5)
bdouble = pyautogui.position()
print("double burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the lettuce burger selection button within 5 seconds of pressing OK")
time.sleep(5)
blettuce = pyautogui.position()
print("lettuce burger button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the bloxy cola selection button within 5 seconds of pressing OK")
time.sleep(5)
cola = pyautogui.position()
print("cola button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Please put you cursor over the fries selection button within 5 seconds of pressing OK")
time.sleep(5)
fries = pyautogui.position()
print("fries button position captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Put your cursor over the upper left corner of a customer's speech bubble within 5 seconds of pressing OK. Do not let the cash register images get in the box.")
time.sleep(5)
topleft = pyautogui.position()
print("top left is captured")
pyautogui.alert("Position captured. Press OK to continue calibration. Do not move your character or adjust your camera zoom.")
pyautogui.alert("Put your cursor over the bottom right corner of a customer's speech bubble within 5 seconds of pressing OK. Do not let the cash register images get in the box.")
time.sleep(5)
btmrght = pyautogui.position()
print("bottom right is captured")
selection = pyautogui.alert("Calibration is complete. Please finish the current customer's order (if there is one) before pressing OK.")

## Finding the region of selection:

topleftstr = str(topleft)
btmrghtstr = str(btmrght)

topleftList = topleftstr.split(sep=", y=", maxsplit = 2)
endregionList = btmrghtstr.split(sep=", y=",maxsplit = 2)

print(str(topleftList))
print(str(endregionList))

regionx,regiony = topleftList
endregionx,endregiony = endregionList

regionxfixed = regionx.split(sep="=",maxsplit=2)
endregionxfixed = endregionx.split(sep="=",maxsplit=2)

regionyfixed = regiony.split(sep=")",maxsplit=2)
endregionyfixed = endregiony.split(sep=")",maxsplit=2)

nox,xregion = regionxfixed
print(xregion)

nox2,endxregion = endregionxfixed
print(endxregion)

yregion,noy = regionyfixed
print(xregion)

endyregion,noy2 = endregionyfixed
print(endxregion)

length = int(endxregion) - int(xregion)
height = int(endyregion) - int(yregion)

time.sleep(3)

##while True:

if True:
    isburger = pyautogui.locateOnScreen('singleburger.png', region=(xregion, yregion, length, height))
    if isburger == "ImageNotFoundException":
        isdburger = pyautogui.locateOnScreen('doubleburger.png', region=(xregion, yregion, length, height))
        if isdburger == "ImageNotFoundException":
            pyautogui.leftClick(blettuce)
        else:
            pyautogui.leftClick(bdouble)
    else:
        print("found single burger")
        pyautogui.leftClick(bplain)
    isCola = pyautogui.locateOnScreen('cola.png', region=(xregion, yregion, length, height))
    if not isCola == "ImageNotFoundException":
        print("found cola")
        pyautogui.leftClick(cola)
    isFries = pyautogui.locateOnScreen('fries.png', region=(xregion, yregion, length, height))
    if not isFries == "ImageNotFoundException":
        print("found fries")
        pyautogui.leftClick(x=599, y=537)
    time.sleep(5)
I'm thinking its probably an error with my math to calculate xregion,yregion,length, and height.
Reply
#7
On lines 5&6, you make regionx and regiony integers.

But on line 57 you throw that away and assign them from topleftList, which from line 51 we see are now strs

Then they're reassigned again on lines 66 and 72 (again to strs).

So by the time you get to 86, they're not ints. region has to be ints.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyautogui, İmagesearch, Moving target Beyazx 0 594 Jun-27-2023, 08:47 PM
Last Post: Beyazx
  Filter and str.isdigit producing an error tester_V 5 1,964 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  PyautoGUI- How to use If - Else with pyautogui.locateCenterOnScreen Tiel 3 8,407 Jun-27-2022, 02:00 PM
Last Post: DeaD_EyE
  producing numbers out of a list bouraque7878 10 3,782 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  how to avoid reapiting lines with locateonscreen rachidel07 3 2,279 Feb-07-2021, 09:30 PM
Last Post: buran
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,562 Feb-03-2021, 01:16 PM
Last Post: rachidel07
Sad problem with Pyautogui rachidel07 1 2,478 Jan-27-2021, 05:43 PM
Last Post: nilamo
  pyautogui with a display emulator? gumby4231 0 2,611 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  pyautogui.screenshot region is not working alexlu 6 6,526 Jun-04-2020, 10:46 AM
Last Post: alexlu
  I don't think my program is producing the correct answer emmapaw24 1 1,727 Mar-31-2020, 01:13 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020