我是Python编程的初学者. 我用Chatgpt制作了一个我不完全理解的程序,但我知道我想制作更多的if-else陈述,而不会让嵌套变得复杂. 除了将if-else后面的行放在if-else陈述中之外,我还能做什么?

import pyautogui
import numpy as np
import cv2
import time

CountGooglyFound = 0

def take_screenshot():
    # Take a screenshot
    screenshot = pyautogui.screenshot()
    # Convert the screenshot to a numpy array for further processing
    screenshot_np = np.array(screenshot)
    return screenshot_np

def search_for_image(image_path, screenshot):
    # Load the image to search for
    image_to_search = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    # Convert the screenshot to grayscale
    screenshot_gray = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
    # Ensure both images have the same data type and depth
    screenshot_gray = np.uint8(screenshot_gray)
    image_to_search = np.uint8(image_to_search)
    # Perform template matching
    result = cv2.matchTemplate(screenshot_gray, image_to_search, cv2.TM_CCOEFF_NORMED)
    # Set a threshold for matching
    threshold = 0.75
    # Get the location of matched regions
    locations = np.where(result >= threshold)
    # Check if the image is found
    if len(locations[0]) > 0:
        return True, list(zip(*locations[::-1]))  # Return coordinates of matched regions
    else:
        return False, None

def main():
    # Take a screenshot and store it in a variable
    screenshot = take_screenshot()

    # Path to the image to search for within the screenshot
    image_path = r'C:\Users\Iris\Desktop\ImageTest\Googly.png'

    # Search for the image within the screenshot
    found, locations = search_for_image(image_path, screenshot)

    if found:
        print("Image found at the following locations:")
        print(locations)
        # Click at point (500,500)
        pyautogui.click(500, 500)
        CountGooglyFound =+ 1
        print(CountGooglyFound)
    else:
        print("Image not found in the screenshot.")
        pyautogui.click(500, 50)
    
pyautogui.click(50, 500)
print(CountGooglyFound)

if __name__ == "__main__":
    main()

我try 过这个,但pyautogui.Click(50,500)和print(CountGooglyFound)在if或else陈述之前执行.其他一切似乎都运行顺利,因为它可以检测到图像或不检测到图像.

推荐答案

如果我理解你的问题,你会想知道为什么这样:

    if found:
        print("Image found at the following locations:")
        print(locations)
        # Click at point (500,500)
        pyautogui.click(500, 500)
        CountGooglyFound =+ 1
        print(CountGooglyFound)
    else:
        print("Image not found in the screenshot.")
        pyautogui.click(500, 50)

在这些行之后执行:

pyautogui.click(50, 500)
print(CountGooglyFound)

如果是这样,请注意所讨论的if-陈述是函数定义的一部分.它是函数main()的一部分,因为它是缩进的.当您调用函数时,属于函数定义一部分的所有内容都会运行.在这里,您在最后一段中调用了函数main().

Python相关问答推荐

如何防止Plotly在输出到PDF时减少行中的点数?

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

分组数据并删除重复数据

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

我在使用fill_between()将最大和最小带应用到我的图表中时遇到问题

韦尔福德方差与Numpy方差不同

Pandas - groupby字符串字段并按时间范围 Select

如何记录脚本输出

数据抓取失败:寻求帮助

如果值发生变化,则列上的极性累积和

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

如何使Matplotlib标题以图形为中心,而图例框则以图形为中心

在Python中使用if else或使用regex将二进制数据如111转换为001""

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

跳过嵌套JSON中的级别并转换为Pandas Rame

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

以异步方式填充Pandas 数据帧

如何设置nan值为numpy数组多条件

大型稀疏CSR二进制矩阵乘法结果中的错误