使用PIL(Python图像库)检测屏幕上的图像 [英] Using PIL (Python Image Library) to detect image on screen

查看:1034
本文介绍了使用PIL(Python图像库)检测屏幕上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在Python 2.7中使用PIL在整个屏幕上搜索特定图像,然后单击它.我一直在搜索,却找不到解决方案.我想创建一个带有一个按钮的小型GUI,单击该按钮时,它将在整个屏幕上搜索预定义的图像.找到图像后,程序将在其中心单击并结束.简而言之,程序将检测用户屏幕上是否存在图像,然后单击它.

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.

我确实在Sikuli上找到了一个有趣的地方,但这对我没有帮助,因为它无法导出到.exe.

I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.

程序每次搜索时将查找的图像很可能位于同一位置,但是我不想对位置进行硬编码,因为它有可能移动,因此我不希望这样以后会成为问题.

The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.

我需要的是用于在屏幕上搜索图像并将编码线发送回变量的编码方法.

What I need is the code method I would use to search for the image on screen and send back the cords to a variable.

图片说明/示例:

步枪的参考图片:

推荐答案

PIL是此作业的错误工具.相反,您应该研究具有出色的python绑定的openCV(开源计算机视觉).这是指向示例的链接(使用C语言,但应该很容易通过python绑定重做),该示例可以满足您的需求,甚至可以旋转,缩放图像等.

PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.

http://docs.opencv.org/doc/tutorials /features2d/feature_homography/feature_homography.html http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html

我假设您正在使用Windows,因为示例图像看起来像窗口.在这种情况下,您可以使用:

I assume you are using windows, as your example image looks like window. In this case you can use:

from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)

然后使用opencv处理图像以查找所需的子图像.

then use opencv to process the image to find sub image you are looking for.

如果要跨平台运行,则需要使用wxWidgets进行屏幕抓取: https://stackoverflow. com/a/10089645/455532

If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532

这篇关于使用PIL(Python图像库)检测屏幕上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆