app = QApplication(sys.argv) screen = QApplication.primaryScreen() def jietu(self, hwnd): img = self.screen.grabWindow(hwnd).toImage() buffer = QBuffer() buffer.open(QBuffer.ReadWrite) img.save(buffer,"bmp") dd = Image.open(buffer) #dd.save("1.bmp") return dd def remove_colors_except(image, target_colors, threshold): result = np.zeros_like(image) for target_color in target_colors: #例如 target_colors = ['FF0000', '00FF00', '0000FF'] # 将目标色格式 "FFFFFF" 转换成 RGB 值 blue = int(target_color[0:2], 16) green = int(target_color[2:4], 16) red = int(target_color[4:6], 16) # 计算目标颜色的范围 lower_range = np.array([blue - threshold, green - threshold, red - threshold]) upper_range = np.array([blue + threshold, green + threshold, red + threshold]) # 创建掩码,将目标颜色范围以外的像素设为黑色,目标颜色范围内的像素设为白色 mask = cv2.inRange(image, lower_range, upper_range) # 将目标颜色范围内的像素设为白色 result[mask > 0] = (255, 255, 255) # 显示结果图像 cv2.imwrite('222.bmp', result) return result ff=fangfa() def main(): hwnd=ff.huoqujubin('【魔域】') if hwnd!=[]: jietushuju=ff.jietu(hwnd) ff.remove_colors_except(jietushuju, ['FFFFFF'], 0) 报错 ff.remove_colors_except(jietushuju, ['FFFFFF'], 0) TypeError: fangfa.remove_colors_except() takes 3 positional arguments but 4 were given
时间: 2024-02-14 22:16:03 浏览: 83
Qt教程.pdf(中文版)
5星 · 资源好评率100%
根据你提供的代码,`remove_colors_except` 方法应该是 `fangfa` 类的一个方法,因此在调用时应该使用 `self.remove_colors_except` 而不是 `ff.remove_colors_except`。修改代码如下:
```python
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
class fangfa:
def jietu(self, hwnd):
img = screen.grabWindow(hwnd).toImage()
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
img.save(buffer,"bmp")
dd = Image.open(buffer)
#dd.save("1.bmp")
return dd
def remove_colors_except(self, image, target_colors, threshold):
result = np.zeros_like(image)
for target_color in target_colors:
#例如 target_colors = ['FF0000', '00FF00', '0000FF']
# 将目标色格式 "FFFFFF" 转换成 RGB 值
blue = int(target_color[0:2], 16)
green = int(target_color[2:4], 16)
red = int(target_color[4:6], 16)
# 计算目标颜色的范围
lower_range = np.array([blue - threshold, green - threshold, red - threshold])
upper_range = np.array([blue + threshold, green + threshold, red + threshold])
# 创建掩码,将目标颜色范围以外的像素设为黑色,目标颜色范围内的像素设为白色
mask = cv2.inRange(image, lower_range, upper_range)
# 将目标颜色范围内的像素设为白色
result[mask > 0] = (255, 255, 255)
# 显示结果图像
cv2.imwrite('222.bmp', result)
return result
ff = fangfa()
def main():
hwnd = ff.huoqujubin('【魔域】')
if hwnd != []:
jietushuju = ff.jietu(hwnd)
ff.remove_colors_except(jietushuju, ['FFFFFF'], 0)
```
另外,如果你的代码中有 `import` 语句,需要将其也放在代码开头的位置。
阅读全文