Python-如何从列表中提取元组



我的变量points:[(100, 50)]中存储了两个坐标我试图用pyautogui.moveTo(points)移动我的鼠标,但我得到了错误:

pyautogui.PyAutoGUIException: The supplied sequence must have exactly 2 or exactly 4 elements (0 were received).

我想这意味着我传递的是一个列表对象,而不是坐标。

表达式[(100, 50)]是什么意思,以及我如何将x和y变换为两个元素。

我获取points的源代码:

import cv2 as cv
import numpy as np
class Vision:
# properties
needle_img = None
needle_w = 0
needle_h = 0
method = None
# constructor
def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
# Save the dimensions of the needle image
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]
self.method = method
def find(self, haystack_img, threshold=0.5, debug_mode=None):
# run the OpenCV algorithm
result = cv.matchTemplate(haystack_img, self.needle_img, self.method)
# Get the all the positions from the match result that exceed our threshold
locations = np.where(result >= threshold)
locations = list(zip(*locations[::-1]))
for loc in locations:
rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h]
# Add every box to the list twice in order to retain single (non-overlapping) boxes
rectangles.append(rect)
rectangles.append(rect)
# Apply group rectangles
rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5)
points = []
if len(rectangles):
line_color = (0, 255, 0)
line_type = cv.LINE_4
marker_color = (255, 0, 255)
marker_type = cv.MARKER_CROSS
# Loop over all the rectangles
for (x, y, w, h) in rectangles:
# Determine the center position
center_x = x + int(w/2)
center_y = y + int(h/2)
# Save the points
points.append((center_x, center_y))
if debug_mode == 'rectangles':
# Determine the box position
top_left = (x, y)
bottom_right = (x + w, y + h)
# Draw the box
cv.rectangle(haystack_img, top_left, bottom_right, color=line_color, 
lineType=line_type, thickness=2)
elif debug_mode == 'points':
# Draw the center point
cv.drawMarker(haystack_img, (center_x, center_y), 
color=marker_color, markerType=marker_type, 
markerSize=40, thickness=2)
if debug_mode:
cv.imshow('Matches', haystack_img)
return points

表达式[(100,50(]是什么意思,以及如何将x和y转换为两个元素。

[...]创建一个列表,其中包含您放入其中的任何内容。(100, 50)创建一个包含整数10050的元组。因此,您有一个列表,其中包含一个包含两个数字的元组。

我认为这意味着我传递的是单个列表对象,而不是坐标。

你是对的,有点。问题不在于您传递的是单个列表对象,需要传递单个列表,或者更确切地说,序列对象。问题是该列表对象只包含一个元素:元组。

您可以通过查看列表的len进行检查:

>>> l = [(100, 50)]
>>> len(l)
1

按照您想要的使用方式,pyautogui.moveTo(points)想要一个包含两个元素的序列(列表或元组(。这些元素是您要移动到的点的坐标。

列表中的元组是这个两元素序列,所以这就是您需要传递的:

pyautogui.moveTo(points[0])

最新更新