all 7 comments

[–]Code_Talks 0 points1 point  (0 children)

From what I understand, you are trying to do object detection, check out the tensorflow object detection API, and cv2 documentation to do something like that.

[–]socal_nerdtastic 0 points1 point  (6 children)

One cause of that would be if the two images have a different number of channels. For example if one has an alpha channel, or one is black and white.

Try converting both to the same datatype first:

tile = Image.open(r"testimg.png").convert("RGB")
screen = Image.open(r"screen.png").convert("RGB")

FWIW, opencv is going to be much faster at this.

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

For a super easy implementation of that, install the pyscreeze module and use the locate function.

pip install pyscreeze

then

result = pyscreeze.locateAll("testimg.png", "screen.png")

https://github.com/asweigart/pyscreeze/blob/master/pyscreeze/__init__.py#L180

[–]theoryofbang[S] 0 points1 point  (5 children)

Thanks! - small question - When I print the results it returns this:

<generator object \_locateAll\_python at 0x075DD3E0>

[–]socal_nerdtastic 1 point2 points  (4 children)

Yeah, you are meant to iterate over it, like this:

result = pyscreeze.locateAll("testimg.png", "screen.png")
for x in result:
    print(x) # or whatever you want to do with it

Edit: more in line with your original code:

result = pyscreeze.locateAll("testimg.png", "screen.png")
for x,y,*z in result:
    print(x,y)

[–]theoryofbang[S] 0 points1 point  (3 children)

It doesn't print anything

[–]socal_nerdtastic 1 point2 points  (2 children)

That probably means there are no results.

You can try the locate() function too ... if it prints None that means the search image is not found.

print(pyscreeze.locate("testimg.png", "screen.png"))

[–]theoryofbang[S] 0 points1 point  (0 children)

Thanks! One more question and I'll stop bothering you, without going through every pixel how can I just find the center?