Kann jemand über den Python Code gucken?
Der Dateipfad ist definitiv richtig. Ich habe versucht, das Bild direkt in den Ordner der Python-Datei zu legen. Trotzdem tritt weiterhin der gleiche Fehler auf. Das Bild ist im richtigen Format, und ich habe keine Ahnung, was das Problem verursacht.
import logging
import pyautogui
class Coordinates:
def __init__(self, function, image_name, confidence=0.9, region=None):
self.function = function
self.image_name = image_name
self.confidence = confidence
self.region = region
def offset(self, x_offset=0, y_offset=0, width_offset=0, height_offset=0):
try:
image_path = fr'templates\{self.image_name}.png'
location = self.function(image_path, confidence=self.confidence, region=self.region)
if len(location) == 2:
x, y = location
x += x_offset
y += y_offset
return x, y
elif len(location) == 4:
x, y, width, height = location
x += x_offset
y += y_offset
width += width_offset
height += height_offset
return x, y, width, height
except pyautogui.ImageNotFoundException as e:
logging.error(f'{e}')
return None
x = Coordinates(pyautogui.locateCenterOnScreen, 'test').offset()
print(x)
1 Antwort
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
Computer, Programmiersprache, Python
Der Fehler ist doch unmissverständlich. Entweder fehlt die Datei oder sie ist nicht lesbar oder sie ist beschädigt. Was sagt denn
dir templates
Oder änderst Du das Arbeitsverzeichnis im Programm? Wenn Du unsicher bist, gib den absoluten Pfad im except-Block aus:
print( os.path.abspath(image_path) )