from PIL import Image, ImageFile
def get_position ():
image_path = os.path.abspath(__file__)
image_path = image_path + "\\resource\\images\\logo.png"
position = search_position(image_path)
if position is None:
return
MAIN_X = position[0]
MAIN_Y = position[1]
def search_position(file_name):
im1 = pyautogui.screenshot()
im2 = Image.open(file_name)
pix1 = im1.load()
pix2 = im2.load()
width1 = im1.size[0]
height1 = im1.size[1]
width2 = im2.size[0]
height2 = im2.size[1]
threshold = 5 # 设置阈值
rgb2 = pix2[0, 0][:3]
for x in range(width1 - width2):
for y in range(height1 - height2):
rgb1 = pix1[x, y][:3]
if rgb1 == rgb2:
status = 0
for s in range(x, x + width2, 2): # 每隔一个点检查一次
for j in range(y, y + height2, 2):
if abs(pix2[s - x, j - y][0] - pix1[s, j][0]) > threshold and abs(
pix2[s - x, j - y][1] - pix1[s, j][:3][1]) > threshold and abs(
pix2[s - x, j - y][2] - pix1[s, j][:3][2]) > threshold:
status = 1
if status:
continue
else:
position = [x, y]
return position
return None
python 不用opencv和aircv解决图片在屏幕中的位置
最新推荐文章于 2024-05-24 09:40:59 发布