asweigart / pyscreeze

PyScreeze is a simple, cross-platform screenshot module for Python 2 and 3.
BSD 3-Clause "New" or "Revised" License
194 stars 96 forks source link

Bug in opencv `locateAll` #97

Open lucaswiman opened 1 year ago

lucaswiman commented 1 year ago

This bug appears to have been introduced in version 0.1.29. When opencv is installed the locateAll methods don't work correctly, failing to find the needle image, even though it is in pixel-for-pixel present at position (0,0) in the haystack.

How to reproduce

The pillow version finds it, but the cv2 version does not:

>>> import pyscreeze
>>> list(pyscreeze._locateAll_pillow(needleImage='corner-icon.png', haystackImage='./screenshot.png'))
[Box(left=0, top=0, width=16, height=16)]
>>> list(pyscreeze._locateAll_opencv(needleImage='corner-icon.png', haystackImage='./screenshot.png'))
[]

But the image is present in the upper left corner:

>>> import cv2
>>> needle = cv2.imread('corner-icon.png')
>>> haystack = cv2.imread('screenshot.png')
>>> (haystack[:16,:16] == needle).all()
True

System information

# python --version
Python 3.8.16
# pip freeze
EasyProcess==1.1
entrypoint2==1.1
jeepney==0.8.0
mss==9.0.1
numpy==1.24.3
opencv-python-headless==4.7.0.72
Pillow==9.5.0
pyscreenshot==3.1
PyScreeze==0.1.29
# head -n 2 /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"

This also reproduced on MacOS (Python 3.9.12).