python-imagesearch icon indicating copy to clipboard operation
python-imagesearch copied to clipboard

Why only primary screen is supported?

Open skazichris opened this issue 6 years ago • 8 comments

Why only primary screen is supported?

skazichris avatar Nov 29 '19 13:11 skazichris

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 :)

drov0 avatar Nov 29 '19 14:11 drov0

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

skazichris avatar Nov 29 '19 15:11 skazichris

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

drov0 avatar Jan 04 '20 22:01 drov0

Windows 10.

sob., 4 sty 2020 o 23:20 Martin Lees [email protected] 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.

skazichris avatar Jan 05 '20 09:01 skazichris

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

drov0 avatar Jan 05 '20 10:01 drov0

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

Edit: the screenshot only contains the primary montor

martinnaj avatar Apr 10 '20 18:04 martinnaj

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

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

martinnaj avatar Apr 10 '20 19:04 martinnaj

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

martinnaj avatar Apr 10 '20 19:04 martinnaj