drov0 / python-imagesearch

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

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

Closed vincenthavinh closed 3 years ago

vincenthavinh commented 3 years ago

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

vincenthavinh commented 3 years ago

I have python-imagesearch==1.2.0 and the bug is actually fixed in 1.2.1, just not yet available by pip install. My bad