Code Monkey home page Code Monkey logo

python-imagesearch's Introduction

Python-ImageSearch

it's a wrapper around pyautogui and opencv2, to allow you to easily add cross-platform image searching capabilities to your project.

See the documentation for examples. If you have any questions feel free to contact me.

Documentation : https://brokencode.io/how-to-easily-image-search-with-python/

This is the currently maintained package, so if you've come from https://pypi.org/project/python-imagesearch-drov0/

you are at the right place

Easy Example:

Install:

pip install python-imagesearch

You may need to install dependencies depending on your OS:

Linux :

sudo pip3 install python3-xlib
sudo apt-get install scrot python3-tk python3-dev python3-opencv

Windows :

No setup should be needed

MacOs :

brew install opencv
pip3 install -U pyobjc-core
pip3 install -U pyobjc

I have not tested myself as I don't own a mac, but it was tested and documented in this issue : #5

python-imagesearch's People

Contributors

blubambu avatar bws0013 avatar calvindo95 avatar dashblack avatar davide710 avatar drov0 avatar hellbrandsdigital avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-imagesearch's Issues

Leak of memory

After many 'call' to "imagesearch" i got the error message :

pos = imagesearch(nom_xxx, PREC_yyy)
File "......", line nn, in imagesearch
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\core\src\alloc.cpp:72: error: (-4:Insufficient memory) Failed to allocate 14133848 bytes in function 'cv::OutOfMemoryError'

From my point of view, this memory leak is due to opencv cv2.
Then this problem is solved by adding the following lines of Python code in "imagesearch" :

import gc # import the garbage collector, at the beginning
.................... def imagesearch(image, precision=0.8):
....................
....................min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
del img_gray # free memory
del template
del res
gc.collect()
del gc.garbage[:]

Then , there is also about the same problem with "click_image".

SystemError: tile cannot extend outside image

First I call imagesearch, which returns coordinates of 2082 478. Everything good there ๐Ÿ‘.

Next, I call imagesearcharea using 2060, 460, 400, 400, which based on the first search returning (2082, 478) and size of the image (156, 151) - it should be just the right position and plenty of area.

Instead, I get a tile cannot extend outside image SystemError.

Here's the error and code:

$ python3 my_example.py 
position :  2082 478
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/PIL/ImageFile.py", line 488, in _save
    fh = fp.fileno()
AttributeError: '_idat' object has no attribute 'fileno'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "my_example.py", line 10, in <module>
    pos = imagesearcharea("github.png", 2060, 460, 400, 400)
  File "/Users/Username/Projects/Python/python-imagesearch/imagesearch.py", line 47, in imagesearcharea
    im = region_grabber(region=(x1, y1, x2, y2))
  File "/Users/Username/Projects/Python/python-imagesearch/imagesearch.py", line 24, in region_grabber
    return pyautogui.screenshot(region=(x1,y1,width,height))
  File "/usr/local/lib/python3.7/site-packages/pyscreeze/__init__.py", line 375, in _screenshot_osx
    im.save(tmpFilename)
  File "/usr/local/lib/python3.7/site-packages/PIL/Image.py", line 2007, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python3.7/site-packages/PIL/PngImagePlugin.py", line 896, in _save
    [("zip", (0, 0)+im.size, 0, rawmode)])
  File "/usr/local/lib/python3.7/site-packages/PIL/ImageFile.py", line 496, in _save
    e.setimage(im.im, b)
SystemError: tile cannot extend outside image

my_example.py

from imagesearch import *

# search entire screen
pos = imagesearch("github.png")
if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0], pos[1])
else:
    print("image not found")

# search region
pos = imagesearcharea("github.png", 2060, 460, 400, 400)
if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0], pos[1])
else:
    print("image not found")

imagesearch returns wrong coordinates

I searched for the position of an image, as your example shows, and if it is on the top-left corner the position is ok, but as it gets far from there, it will increase the coordinates even doubling them.

I divided them by 2 and it is close to the actual position but not enough:

from imagesearch import *

pos = imagesearch('notif.jpg')

if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0]/2, pos[1]/2)
    pyautogui.click(button="left")
else:
    print("image not found")

I am using Mac OS, also I use 2 monitors, but the problem is still there if I only use one.

NameError: name 'r' is not defined

When I try to click on an image I get this error message on the click_image function

pyautogui.moveTo(pos[0] + r(width / 2, offset), pos[1] + r(height / 2,offset), timestamp)
NameError: name 'r' is not defined

'ScreenShot' object has no attribute 'thumbnail'

I use MacBook with Retina display, so when I ran the simple code below:

from python_imagesearch import imagesearch

img = "pics/testimg.png"
print(imagesearch.imagesearch(img))

I got this error:

.venv/lib/python3.11/site-packages/python_imagesearch/imagesearch.py", line 124, in imagesearch 
im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
^^^^^^^^^^^^
AttributeError: 'ScreenShot' object has no attribute 'thumbnail'

It looks like conditions with the is_retina variable doesn't work properly, because if I comment these lines:

If platform.system() == "Darwin":
    is_retina = subprocess.call("system_profiler SPDisplaysDataType | grep -i 'retina'", shell=True) == 0

The code will execute without errors, but I'm not sure that trick won't break anything. ๐Ÿค”

Search image that appear in multiple positions

First, thanks very much for this project! But I have a question, that I have more than one of the search image in the screen, but imagesearch seems only return random one.

So If it possible to return all the pos of an image by one search, or exclude a pos while searching?

libpng warning: sBIT: invalid

Hello,

I'm trying to use this library to automate a game. I've taken a screenshot of the game with Print Screen on Windows 10, opened it in Paint, cropped the thing I want to identify later on when I'm trying to automate the game, added it to my Python project workspace.

But when I try to sanity check things and by first attempting to find the image while the game is running, the console repeatedly prints out "libpng warning: sBIT: invalid"
What I'm trying to find:
brgsoda

What the screen looks like as a whole:
ss1

I'm only searching the upper half of the screen, where the chat bubbles appear.
the code:
pos = imagesearch_region_loop("./brgsoda.png", 0.5, 0, 0 , 1100 ,350) if pos[0] != -1: print("position : ", pos[0], pos[1]) pyautogui.moveTo(pos[0], pos[1]) exit(0) else: print("image not found")

Resolution Change

Is the library capable of searching images irrespective of resolution change

Can imagesearch handle an image on a different background?

I want to search for an image that could appear on different backgrounds in a game, however imagesearch does not appear to be able to do this. My screenshot plus 3 examples of the image I am looking for are given here,

https://imgur.com/a/pUNVKET

In the last image, I removed the background to isolate the feature I want. Here is my script:

#!/usr/bin/python3

from python_imagesearch.imagesearch import imagesearch_loop

pos = imagesearch_loop("./exchange_sand.png", 0.2, 0.8)
print("position : ", pos[0], pos[1])

When I use the exact file (exchange_sand.png) it works fine. But either of the other 2 images (exchange_forest.png and exchange_transp.png) it fails to find the image. I have tried adjusting the threshold from 0.8 downward but it does not work either.

Any ideas?

NameError: name 'imagesearch_from_folder' is not defined

I'm not sure if I'm doing something wrong, but all example on the documentations works (https://brokencode.io/how-to-easily-image-search-with-python/) as expected, with the exception of the last one (imagesearch_from_folder)

from python_imagesearch.imagesearch import imagesearch_count

results = str(imagesearch_from_folder('./', 0.8))
print(results)

The output is the following:

python test.py
Traceback (most recent call last):
  File "C:\Users\blank\****\test.py", line 3, in <module>
    results = str(imagesearch_from_folder('./', 0.8))
NameError: name 'imagesearch_from_folder' is not defined

Module not found (Windows)

For the past couple of days, this module has been working, now it does not work. Says module not found, Tried to pip install it but it could not find the module.

imagesearcharea bug

Im using imagesearcharea to just get the game screen using:

wHandler = win32gui.FindWindow(None,'Yu-Gi-Oh! DUEL LINKS')
windowRect = win32gui.GetWindowRect(wHandler)

when i use:

pos = imageRecognition.imagesearcharea('OK.jpg',windowRect[0],windowRect[1],windowRect[2],windowRect[3],0.9)
    if pos != [-1,-1]:
        imageRecognition.click_image('OK.jpg',pos,'left',0)

It returns [-1,-1], but if i usea:

pos = imageRecognition.imagesearcharea('OK.jpg',0,0,windowRect[2],windowRect[3],0.9)
    if pos != [-1,-1]:
        imageRecognition.click_image('OK.jpg',pos,'left',0)

it works fine. There is something im doing wrong?

Exception with imagesearch area

Likely I am doing something wrong here, but this snippet:

screen_size = pyautogui.size()

pos_local = imagesearch("./Local.png")
if pos_local[0] != -1:
    print("pos_local : ", pos_local[0], pos_local[1])
else:
    print("image not found")

print("inputs to imagesearch area: ",pos_local[0] - 50, pos_local[1] -50, pos_local[0]+200, screen_size[1]-50)

pos = imagesearcharea("./win-bottom-strip.png", 3416, 399, 3666, 2110, 0.9)
if pos[0] != -1:
    print("position : ", pos[0], pos[1])
else:
    print("image not found")

produces this output + exception:

pos_local : 3466 449
inputs to imagesearch area: 3416 399 3666 2110

Traceback (most recent call last):
File "\findMoveWin.py", line 45, in
pos = imagesearcharea("./win-bottom-strip.png", pos_local[0] - 50, pos_local[1] -50, pos_local[0]+200, screen_size[1]-50)
File "C:\Program Files\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 60, in imagesearcharea
im = region_grabber(region=(x1, y1, x2, y2))
File "C:\Program Files\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 36, in region_grabber
return sct.grab(region)
File "C:\Program Files\Python39\lib\site-packages\mss\base.py", line 88, in grab
return self._grab_impl(monitor)
File "C:\Program Files\Python39\lib\site-packages\mss\windows.py", line 264, in _grab_impl
self.data = ctypes.create_string_buffer(width * height * 4) # [2]
File "C:\Program Files\Python39\lib\ctypes_init
.py", line 63, in create_string_buffer
buftype = c_char * init
ValueError: Array length must be >= 0, not -16615168

These work fine:
pos = imagesearcharea("./win-bottom-strip.png", 0,0,100,100)
pos = imagesearcharea("./win-bottom-strip.png", 100,220,2000,2000)
pos = imagesearcharea("./win-bottom-strip.png", 0,0,screen_size[0],screen_size[1])
pos = imagesearcharea("./win-bottom-strip.png", 0, 0, screen_size[0],screen_size[1])
pos = imagesearcharea("./win-bottom-strip.png", 100,0,2000,2160)

This fails with a different exception:
(note only diff is X1 = 1,000 when fail and x1 = 100 when ok)

pos = imagesearcharea("./win-bottom-strip.png", 1000,0,2000,2160)

screeb_size: Size(width=3840, height=2160)
Traceback (most recent call last):
File "D:\dev\eve\eve-scan\src\findMoveWin.py", line 55, in
pos = imagesearcharea("C:/Users/d/OneDrive/Personal/misc-docs/eve/scan-images/win-bottom-strip.png", 1000,0,2000,2160)
File "C:\Program Files\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 60, in imagesearcharea
im = region_grabber(region=(x1, y1, x2, y2))
File "C:\Program Files\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 36, in region_grabber
return sct.grab(region)
File "C:\Program Files\Python39\lib\site-packages\mss\base.py", line 88, in grab
return self._grab_impl(monitor)
File "C:\Program Files\Python39\lib\site-packages\mss\windows.py", line 285, in _grab_impl
raise ScreenShotError("gdi32.GetDIBits() failed.")
mss.exception.ScreenShotError: gdi32.GetDIBits() failed.
Please advise?

Thanks

Just dropping in to say thanks for sharing. Was able to drop these functions into existing web automation framework classes to enable GUI functionality quickly and easily... Awesome.

imagesearcharea with nonzero x1 and y1

def imagesearcharea(image, x1, y1, x2, y2, precision=0.8, im=None):
    if im is None:
        im = region_grabber(region=(x1, y1, x2, y2))
        if is_retina:
            im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
        # im.save('testarea.png') usefull 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)

    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

so I tried to speed up the search by specifying the region with nonzero x1 and y1, which, as expected, cuts off left and top of the searched area;
this function returns the "absolute" coordinates (with reference to (0,0))

wouldn't it be better if it returned "relative" coordinates (with reference to (x1,y1))?
max_loc[0] += x1 and max_loc[1] += y1 ? (of course you cannot do that for tuples, it's just to make it clear what I mean)
especially since click_image() expects "relative" coordinates

Cannot Install on Linux, Missing Requirements.txt

When attempting to install on Linux I get the following error and am not able to install:

root@localhost:~# python3 -m pip install python-imagesearch --no-cache-dir
Collecting python-imagesearch
  Downloading https://files.pythonhosted.org/packages/0d/45/95e2adbdbcbc0281a2ba1014c1acf278cb7a63f203853f11f261d058dfd2/python-imagesearch-1.2.1.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-3i3k34y0/python-imagesearch/setup.py", line 6, in <module>
        with open('requirements.txt') as f:
    FileNotFoundError: [Errno 2] No such file or directory: 'requirements.txt'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-3i3k34y0/python-imagesearch/
root@localhost:~#

Way to turn down minimum resemblance?

When image scanning, it uses a system to check if it looks somewhat alike, is there a way to make it more strict? My images are very similar.

Thanks!
notgo
go

Sometimes need delay after pyautogui mouse click before calling imagesearch

I'm using 'pyautogui' along with various 'imagesearch' routines (usually imagesearch_loop) to automate the creating of Google Photo albums. I've found that it is sometimes necessary to add a time delay (e.g. time.sleep(2)) AFTER calling pyautogui to click on a button that brings up a sub-menu of some type and BEFORE using imagesearch_loop to look for a item on the new menu. If I don't add this delay, the sub-menu doesn't come up and, of course, imagesearch_loop keeps looking forever! This behavior doesn't seem to occur every time I click on a menu item, but it does seem to happen consistently when working with certain menus...

I've had to use a trial-and-error approach to determine where time delays are required, and I probably have a lot more delays than necessary in my program. I was hoping to always just click on a menu item and then immediately call imageseach_loop to wait for the new sub-menu to pop up. Unfortunately, that approach doesn't always work!

Does anyone know why imagesearch appears to "block" certain clicks from pyautogui?

Thanks, Wayne

Missing imagesearch_region_numLoop

I'm missing that function. I'd have opened a PR for it, but that's not possible sadly.
Here is the code:

'''
Searchs for an image on a region of the screen continuously until it's found or max number of samples reached.

input :
image : path to the image file (see opencv imread for supported types)
time : Waiting time after failing to find the image
maxSamples: maximum number of samples before function times out.
x1 : top left x value
y1 : top left y value
x2 : bottom right x value
y2 : bottom right y value
precision : the higher, the lesser tolerant and fewer false positives are found default is 0.8

returns :
the top left corner coordinates of the element as an array [x,y]
'''


def imagesearch_region_numLoop(image, timesample, maxSamples, x1, y1, x2, y2, precision=0.8):
    pos = imagesearcharea(image, x1, y1, x2, y2, precision)
    count = 0
    while pos[0] == -1:
        time.sleep(timesample)
        pos = imagesearcharea(image, x1, y1, x2, y2, precision)
        count = count + 1
        if count > maxSamples:
            break
    return pos

Coords of 2nd image found

Hello,

I need to search one image un my screen

This image can be at multiple position and can be dupplicate

how i can get goords of this image at first position, second position, etc ?

Thanks

and thank you for you work

Can you use this to search for sprites and not just icons ?

I read all of your tutorial about you wakfu-bot, and i was wondering if you used this image-search function to actually search ressources on the screen, or if you just click on every position at random and wait for a collect icon to appear ?

Thx in advance for your answer !

Process finished with exit code -1073741819 (0xC0000005)

Hi,

I've just recently found this from google and I am trying to make an auto clicker for an online game. The program runs fine for sometime, but after 20 minutes or so, it crashes with the following exception:

Process finished with exit code -1073741819 (0xC0000005)

I have tried updating every packages, but the result is still the same. The crash happens at random times. I notice it happens when it calls "imagesearch" function so I assume there is something wrong in there.

while True:
    print("Checking quest objectives")
    pos = imagesearch("images/QuestComplete_Indicator.png")
    if pos[0] != -1:
        log("Turning it to NPC")
        pos = imagesearch("images/QuestSkip_Btn.png")
        if pos[0] != -1:
            log("NPC Found")
            if pos[0] != -1:
                click_image("images/QuestSkip_Btn.png", pos, "left", 0, 0)
                time.sleep(1.5)
                pos = searchAndClickImage('images/QuestComplete_Btn.png', 0.95)
                if pos[0] != -1:
                    log("Quest completed")
                    break

        press("space")
        pos = imagesearch("images/ClickableQuest.png")
        if pos[0] != -1:
            if (customFunction is not None):
                customFunction()
            pyautogui.moveTo(pos[0] + 50, pos[1] + 3)
            click()

    time.sleep(1)

And this is the console log's output:

Checking quest objectives
Turning it to NPC
Checking quest objectives
Turning it to NPC
Checking quest objectives
Turning it to NPC
Checking quest objectives
Turning it to NPC

Process finished with exit code -1073741819 (0xC0000005)

finding coordinates of multiple images on display

Hello,

using the sample in example.py, you run a check for multiple images on the screengrab (im), but it does not return the coordinates of the matches.

should imagesearcharea return the coordinates or is this not possible?

for example:

i have 5 possibles images that i would like matches to the screen,
return the coordinates when 1 of the 5 is found.

thank you!

imagesearch | AttributeError

When trying to use the imagesearch I get an AttributeError
AttributeError: 'NoneType' object has no attribute 'shape'
VS Code says the error is located in imagesearch.py on line 125, where the file reads template.shape[::-1]

My piece of code triggers the error

import pyautogui
import python_imagesearch.imagesearch as imgSearcher

imgSearcher.imagesearch('./images/PlayAgainButton.PNG')

Earlier today, I downloaded python 3.9 from the Windows Store, and used pip to install python-imagesearch
I have no other versions of Python installed

downloading error

I am using the latest version of pip (20.0.2) on Windows 10 and wanted to run the installation command (pip3 install -r requirements.txt). I don't know why, but for some reason I have to use "py -m pip" instead of "pip" or "pip3", so I tried the command like that:
"py -m pip install -r requirements.txt"
and got an error message.

73757219-32f0c700-4769-11ea-9cfa-932c07006d8f

If I use "pip" or "pip3" instead of "py -m pip" and try the command like that:
"pip install -r requirements.txt"
I get an error message too.

73757788-0b4e2e80-476a-11ea-805a-2381065e77f9
This message translated from German into English means:
"The command "pip" is either misspelled or could not be found."

Is there an alternative way to download the needed packages? Or do you know what I did wrong? I am very grateful for any solution or help.

-Jojo934

Frame

Hi, this is an interesting idea. If i am using NOX and minimize the window i would like the tasks to be continued but it will stop or maybe to put the script on server. What do we have to add to execute it.
Another way to make the script quicker is to define a region "where to look" to make the search quicker

Trouble with imagesearcharea when region x2 and y2 is less than double the size of x1 and y1?

I've got this program with a for loop which runs through a list.
However when I put the x1 and y1 cords in at non-zero I start getting problems.

I threw together a gui where I input the region to grab, but as soon as I put in x1 and y1 as something non-zero I start getting issues, different issues depending on values.

400, 400, 400, 400 gives me:
...Programs\Python\Python39\lib\site-packages\mss\windows.py", line 285, in _grab_impl
raise ScreenShotError("gdi32.GetDIBits() failed.")
mss.exception.ScreenShotError: gdi32.GetDIBits() failed.

200, 200, 400, 400 gives me
...Programs\Python\Python39\lib\site-packages\python_imagesearch\imagesearch.py", line 73, in imagesearcharea
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Exception: Line: 0 Callable failed || error occured in a callback

if I put in something like 200, 200, 500, 500 it does run but it doesn't seem to find the images.

def findPicOpt3(sender, data):
    t = Timer(name="random stuff")
    t.start()
    confidence = get_value("inputint4")
    print("searching with confidence level", confidence)
    # im = region_grabber((confidence[0], confidence[1], confidence[2], confidence[3]))
    for num, name in enumerate(imagelist.testImages):
        pos = imagesearcharea(
            imagelist.testImages[num],
            confidence[0],
            confidence[1],
            confidence[2],
            confidence[3],
            #    im,
        )
        if pos[0] != -1:
            print("Found", imagelist.testNames[num], "at", pos)
        else:
            print("image not found")
    t.stop()

I wonder if it has to do with the fact that when I try to debug using im.save it tells me screenshot has no attribute called save:
python_imagesearch\imagesearch.py", line 70, in imagesearcharea
im.save(
AttributeError: 'ScreenShot' object has no attribute 'save'
Exception: Line: 0 Callable failed || error occured in a callback

Might I be missing a package or an import? If so I haven't been able to spot which and what
I've seen this being reported when people have their path wrong, but it works with regular imagesearch and makes an absolute path

I have triple monitor 1920x1080 setup (one vertical) but I tried disconnecting two of them and this was still a problem.
I'm on windows.

is this a bug or should I just give up programming and become a truck driver?

edit: btw lets say I want to grap the middle 800x800 region on a (theoretical) monitor of 1200x1200, I assume I should put in 200, 200, 1000,1000? or 200,200,800,800?.. ive tried both anyway, both don't seem to work.. it's like the region_grabber is accepting widthxheight, and something else is working on topx, topy, bottomx, bottomy? is this possible?

Check for is_retina opposite of intended behavior

When attempting to run the initial quickstart example I got an error that I have tracked down to a small code issue:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    pos = imagesearch("./test.jpeg")
  File "/usr/local/lib/python3.8/site-packages/python_imagesearch/imagesearch.py", line 120, in imagesearch
    im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))
AttributeError: 'ScreenShot' object has no attribute 'thumbnail'

I verified the path was correct and tried using the full image path but got the same issue. Reviewing the code I noticed that the platform check for macs is being used incorrectly.

The following if statement is run if the system is darwin and passes an output to is_retina:

is_retina = False
if platform.system() == "Darwin":
    is_retina = subprocess.call("system_profiler SPDisplaysDataType | grep 'retina'", shell=True)

The issue is that is_retina is being given the status code of the grep operation. When a grep match is found a 0 (Grep True) is returned and when nothing is found a 1 (Grep False) is returned. In the code this means that if the grep match for retina is found then is_retina will be set to 0 (Python False) and if it isn't found is_retina is set to 1 (Python True).

This can be fixed by modifying the is_retina assignment to be as follows:

is_retina = subprocess.call("system_profiler SPDisplaysDataType | grep 'retina'", shell=True) == 0

I have tested this on my mac successfully.

I am getting error while executing first script

File "/python_imagesearch/imagesearch.py", line 120, in imagesearch
im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))

AttributeError: 'ScreenShot' object has no attribute 'thumbnail'

region_grabber(region) uses wrong coordinates for mss().grab(region) (with possible fix)

Hello,

When I use region_grabber(region), the region captured is not the one expected.
The function seems to call mss().grab(region) with arguments x1, y1, width, height, whereas it should call it with x1, y1, x2, y2.

the test I runned :

from PIL import Image
from python_imagesearch.imagesearch import *

screenshot = region_grabber((200, 200, 410, 410))
# mss.tools.to_png(screenshot.rgb, screenshot.size, output="screenshot.png") # to save the image
img = Image.frombytes("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX")
img.show()

it should show a 210x210 image on screen, not a 10x10 image.

When I run it with original region_grabber that calls mss().grab(region) with arguments x1, y1, width, height, I get :
screenshot
which is the unexpected result

When I comment the line region = x1, y1, width, height from the region_grabber function code, so that it calls mss().grab(region) with original arguments x1,y1, x2,y2, I get :
screenshot
which is the attended result.

This python-mss.readthedocs.io example also says to use x1, y1, x2, y2 as arguments for mss().grab(region).

I did not do a Pull Request with this fix because I m rather new to Python and I dont really if I imported your project with the right dependencies. My pip freeze tells me I have version mss==6.1.0 installed.
I dont want to break behavior with old versions.

Thanks for your read

Not Working

using this exact code with the image on screen and it just returns image not found. Any help would be appreciated. I have tried on all my screens and with only one plugged in but it isn't working.

from python_imagesearch.imagesearch import imagesearch

pos = imagesearch("./github.png")
if pos[0] != -1:
print("position : ", pos[0], pos[1])
else:
print("image not found")

Image should be found, but is not :/

Hi, i'm following your tutorial on Medium.

I'm kinda lost, the script can't find the image that should easily be found.

Here is the screen shot taken by pyautogui

testarea

And here is my lookup image :

sort_pression

The image fit perfectly it photoshop.. but can't be found by the script :

pos = imagesearch("img/sort_pression.png")

print(pos)

if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0], pos[1])
else:
    print("image not found")

And result :

โฏ python3 main.py                                                                                                                                                              
[-1, -1]
image not found

Any idea of what i can't be doing wrong ?

I tested this on a macbook retina, the is_retina is set to 1..

Thanks a lot :)

Cant install module

If i try to install the updated version it gives me this error:
Screenshot_10
But if i try to install the older version (1.2.0) it works perfectly fine
Screenshot_11

Can not search color image file.

I want to recognize a different color image, but now it is recognized as gray, so different colors are considered the same image.

img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

I have an error

I have pasted all the examples and put in the correct image address.

**
from imagesearch import *

count = imagesearch_count("./dex61.png")
print(count)
**

But this code doesn't work.
and I get the following error in terminal

**
from imagesearch import *

count = imagesearch_count("./dex61.png")
print(count)
**

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.