[英]Is it possible to detect pixels color with python?
I am trying to detect an exact pixel by its color, and then do something whether that pixel is being found or not.我试图通过它的颜色来检测一个确切的像素,然后无论是否找到该像素都做一些事情。 I think you (obviously) cannot make this with vanilla python, but I have not found any module in order to do this.
我认为你(显然)不能用香草 python 做这个,但我还没有找到任何模块来做到这一点。 If this is possible, I'd like to get some examples.
如果这是可能的,我想举一些例子。 Thanks in advice.
谢谢指教。
Edit: Basically, I need to scan a pixel (1920x1080 screen) and get the RGB Values编辑:基本上,我需要扫描一个像素(1920x1080 屏幕)并获取 RGB 值
You can use Pillow to do what you want.你可以使用Pillow做你想做的事。
You can try the following function.你可以试试下面的function。 It's a modified version of the code from https://stackoverflow.com/a/765774/8581025 .
这是来自https://stackoverflow.com/a/765774/8581025的代码的修改版本。
from PIL import Image
def detect_color(rgb, filename):
img = Image.open(filename)
img = img.convert('RGBA')
data = img.getdata()
for item in data:
if item[0] == rgb[0] and item[1] == rgb[1] and item[2] == rgb[2]:
return True
return False
For example, if you want to know if there is a red pixel (255, 0, 0) in example.png
file in the current working directory, you can use the following code.比如想知道当前工作目录下的
example.png
文件中是否有红色像素(255,0,0),可以使用如下代码。
detect_color((255, 0, 0), 'example.png') # returns True if there is a red pixel, False otherwise.
For getting a pixel color at specific coordinate, you can refer Get pixel's RGB using PIL .要获取特定坐标处的像素颜色,可以参考Get pixel's RGB using PIL 。
You can use numpy
and pyautogui
:您可以使用
numpy
和pyautogui
:
import numpy as np
import pyautogui
image = np.array(pyautogui.screenshot())
np.argwhere(image == [255, 255, 255])
It will return all points who are of the specified color.它将返回具有指定颜色的所有点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.