drov0 / python-imagesearch

A wrapper around opencv2 and pyautogui to do image searching easily.
MIT License
278 stars 99 forks source link

Why only primary screen is supported? #14

Open skazichris opened 4 years ago

skazichris commented 4 years ago

Why only primary screen is supported?

drov0 commented 4 years ago

If I remember well, this is a limitation from opencv. But I might wrong, I'm open to PR's if you want to add this feature :)

skazichris commented 4 years ago

Actually I have 4 screens. I was really enthusiastic when I found your imagesearch class, but then... it does not work on multi screen.

There might be some workarounds according to this: https://stackoverflow.com/questions/43015077/how-to-display-different-windows-in-different-monitors-with-opencv?noredirect=1&lq=1 and this: https://stackoverflow.com/questions/24540091/opencv-fullscreen-windows-on-multiple-monitors

drov0 commented 4 years ago

Mmmh actually I just ran a test and multiple screens are supported on linux at least. which os are you using ?

skazichris commented 4 years ago

Windows 10.

sob., 4 sty 2020 o 23:20 Martin Lees notifications@github.com napisał(a):

Mmmh actually I just ran a test and multiple screens are supported on linux at least. which os are you using ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/drov0/python-imagesearch/issues/14?email_source=notifications&email_token=AFK2W2VTGZQLWBREJGME3ZDQ4EDTNA5CNFSM4JS7TEAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIDBQOQ#issuecomment-570824762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFK2W2REI33HF2PWJ7KY5LLQ4EDTNANCNFSM4JS7TEAA .

-- Pozdrawiam / S pozdravem / Best regards, Chris Zamarski, Oracle Certified Professional & SAP BC Specialist, MS&T

Please note: The information contained in this message may be legally privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, you are hereby notified that any unauthorized use, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer.

drov0 commented 4 years ago

Can you try to do an image search while uncommenting the im.save in the imagesearch function see how the captured area is ? It should be all of your screens.


def imagesearch(image, precision=0.8):
    im = pyautogui.screenshot()
    if is_retina:
        im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
    im.save('testarea.png') # useful for debugging purposes, this will save the captured region as "testarea.png"
    img_rgb = np.array(im)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(image, 0)
    template.shape[::-1]

    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if max_val < precision:
        return [-1, -1]
    return max_loc
martinnaj commented 4 years ago

Hi, I tried the above but it doesn't work, just returns [-1, -1]

Edit: the screenshot only contains the primary montor

martinnaj commented 4 years ago

The issue actually lies within pyautogui: https://github.com/asweigart/pyautogui/issues/9

Solution: https://github.com/asweigart/pyautogui/issues/9#issuecomment-603176347

martinnaj commented 4 years ago

Here is my solution

Install the following:

pip install desktopmagic
pip install pywin32

Add the following to the start of imagesearch.py

from __future__ import print_function

Add the following after import subprocess in imagesearch.py

from desktopmagic.screengrab_win32 import getScreenAsImage

Now, change the imagesearch function to:

def imagesearch(image, precision=0.8):
    im = getScreenAsImage()
    if is_retina:
        im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
    #im.save('testarea.png') # useful for debugging purposes, this will save the captured region as "testarea.png"
    img_rgb = np.array(im)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(image, cv2.IMREAD_COLOR)
    template.shape[::-1]
    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if max_val < precision:
        return [-1, -1]
    return max_loc

You can now find images on other monitors