1.返回图片中选中的区域坐标
from typing import Union, List, Tuple
def find_area(search_: str, screen_x: int, screen_y: int) -> Union[None, Tuple[int, int, int, int]]:
x_division = int(search_[search_.index('x') + 1])
y_division = int(search_[search_.index('y') + 1])
coms = x_division * y_division
target_index = int(search_[search_.index('g') + 1])
if target_index > coms:
return None
x_width = screen_x / x_division
y_height = screen_y / y_division
target_x_index = (target_index - 1) % x_division
target_y_index = (target_index - 1) // x_division
target_x = int(target_x_index * x_width)
target_y = int(target_y_index * y_height)
target_x_end = int(target_x + x_width)
target_y_end = int(target_y + y_height)
return target_x, target_y, target_x_end, target_y_end
x = 2560
y = 1440
msg = 'x2y2g1'
print(find_area(search_=msg, screen_x=x, screen_y=y))
2.获取屏幕当前截图中某块区域里面指定像素的坐标集
from typing import Union, List, Tuple
from PIL import ImageGrab, Image
def find_pixel(pixel_rgb: Tuple[int, int, int], pic: Union[None, str] = None, search_area: Union[None, str] = None) -> \
List[Union[Tuple[int, int]]]:
"""
# 获取指定像素点的坐标集
:param pic 图片路径,不传该参数表示截取屏幕
:param search_area : 设置搜索范围功能 x3y2g2: 整个图片表示把x轴划分为3等分,y轴划分为2等分,然后从左往右,从上到下选择第2个部分
:param pixel_rgb: 指定的像素
:return: 指定颜色的像素点的列表
"""
if pic:
screenshot = Image.open(pic)
else:
screenshot = ImageGrab.grab()
_x, _y = screenshot.size
if search_area is not None:
left, upper, right, lower = find_area(search_area, _x, _y)
screenshot = screenshot.crop(box=(left, upper, right, lower))
else:
left, upper, right, lower = 0, 0, _x, _y
return [(left + x, upper + y) for x in range(screenshot.width) for y in
range(screenshot.height) if screenshot.getpixel((x, y)) == pixel_rgb]