【问题标题】:How to find a image with transparent background on the screen?如何在屏幕上找到具有透明背景的图像?
【发布时间】:2021-01-31 10:31:17
【问题描述】:

所以,我有一张具有透明背景和尺寸(Width_1High_1)的图像,而我要做的是在屏幕上找到这张图像(但不必具有相同的尺寸)比方说尺寸(Width_2High_2),所以考虑到这一点,我编写了以下代码:

def resize_untill_find(image,bigger_or_smaller, min_size = 50 , max_size = 150 , one_match_per_point = 'on'): #(imagem, diminuir ou aumentar, min_size = reduce the image until min_size%, max_size = expand the image until max_size%)
    print('Press CTRL+ALT to start:')
    while True:
        if keyboard.is_pressed('control+alt'):
            break
    image_info = Ferramentas.get_image_first_info(image)# get width, high, and pixels number off the image
    image = cv2.imread(image)
    image_rate = image_info['whidth'] / image_info['high'] #proportion Width/High
    ocurrencies = {} # [x,y,w,h]
    match_number = 1 # number of the ocurrency
    high = image_info['high']
    if bigger_or_smaller == 'smaller':
        while int((high/image_info['high'])*100) >= int(min_size): # while the resize are not under min_size
            print('{}%'.format(round((high/image_info['high'])*100)))
            width = round(image_info['whidth']-((image_info['high'] - high) * image_rate))
            dimension = (width,high) # new dimension of the image
            print('Dimension:', dimension)
            image_resized = cv2.resize(image,dimension) #resize the image
                matches_position = pyautogui.locateAllOnScreen(image_resized, confidence = 0.9)
                for region in matches_position:
                    region = list(region)
                    ocurrencies['Match_{}'.format(match_number)] = region # region = [X,Y, new_Width , new_High]
                    match_number += 1 
            if one_match_per_point == 'on':
                match_position = pyautogui.locateOnScreen(image_resized, confidence = 0.9)
                if match_position != None:
                    match_position = list(match_position)
                    ocurrencies['Match_{}'.format(match_number)] = match_position # region = [X,Y, new_Width , new_High]
                    match_number += 1
            high -= 1
    if bigger_or_smaller == 'bigger':
        while int((high/image_info['high'])*100) <= int(max_size):
            print('{}%'.format(round((high/image_info['high'])*100)))
            width = round(image_info['whidth']+((high - image_info['high']) * image_rate))
            dimension = (width,high) # image new dimension
            print('Dimension:', dimension)
            image_resized = cv2.resize(image,dimension) #resize the imge with new dimensions
            if one_match_per_point == 'off':
                matches_position = pyautogui.locateAllOnScreen(image_resized, confidence = 0.9) # lista todos os matches 90% iguais ao da imagem redimensionada
                for region in matches_position:
                    region = list(region)
                    ocurrencies['Match_{}'.format(match_number)] = region # region = [X,Y, new_Width , new_High]
                    match_number += 1   
            if one_match_per_point == 'on':
                match_position = pyautogui.locateOnScreen(image_resized, confidence = 0.9)
                if match_position != None:
                    match_position = list(match_position)
                    ocurrencies['Match_{}'.format(match_number)] = match_position # region = [X,Y, new_Width , new_High]
                    match_number += 1 
            high += 1
    print(ocurrencies)
    cv2.waitKey(0)
    return ocurrencies

我不知道是否有更好的方法来做到这一点,如果有,请告诉我,我很乐意改进代码(必须尽可能快),但主要问题是:它无法匹配具有透明度的模板,以及透明度渐变背景。

所以我有这张图片:

并且程序无法找到图像并获取它的坐标。

*编辑:

所以我可以拍下这张照片:

并调整大小,直到在此图像上找到匹配项:

但是当我尝试拍摄具有透明背景的 .png 图片时,它不起作用:

(模板)并尝试在这个上找到匹配项 (screen1) 或这个(screen2)。

您可以注意到第一个模板的尺寸与 screen1 相同,但背景为深灰色,而 screen2 的背景也不同且较小(但保持原来的Width/High比例)比原来的。

所以我想要的是能够使用 template 在屏幕的某个位置找到 screen1screen2

【问题讨论】:

    标签: python python-3.x image image-processing pyautogui


    【解决方案1】:
    def search(screen, img):
    sx, sy = screen.size
    ix, iy = img.size
    for xstart in range(sx - ix): 
        for ystart in range(sy - iy):
            #search for the pixel on the screen that equals the pixel at img[0:0]
            if img.getpixel((0,0)) == screen.getpixel((xstart, ystart)):
                match = 1 #temporary
                for x in range(ix): #check if first row of img is on this coords
                    if img.getpixel((x,0)) <> screen.getpixel((xstart+x, ystart)):
                        match = 0 #if there's any difference, exit the loop
                        break 
                if match == 1: #otherwise, if this coords matches the first row of img
                    for x in range(ix): 
                        for y in range(iy):
                            #check every pixel of the img
                            if img.getpixel((x,y)) <> screen.getpixel((xstart+x, ystart+y)):
                                match = 0 #any difference - break
                                break
                    if match == 1: return (xstart, ystart) #return top-left corner coordinates
    return (-1,-1) #or this, if not found
    #i found this somewhere else
    

    【讨论】:

    • 非常感谢您的帮助,确实比我的快,但我仍然无法找到大小和透明度不同的图像,当我尝试它时,代码返回 (-1,-1) ,我将编辑我的帖子以提供更多我找不到匹配的图片示例
    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 2014-06-18
    • 2021-04-28
    • 1970-01-01
    • 2015-05-15
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多