简体   繁体   English

如何获取 python 区域中所有像素的颜色

[英]How to get color of all pixels in an area in python

I'm trying to write a program on linux that does something if the pixels in an area aren't all the same color, for example:我正在尝试在 linux 上编写一个程序,如果一个区域中的像素不是全部相同的颜色,例如:

if color not "255, 255, 255":
    #do something

this is what i have for one pixel:这就是我为一个像素所拥有的:

import time, pyautogui
time.clock()
image = pyautogui.screenshot()
color = image.getpixel((1006, 553))
print(time.clock())
print(color)

I know how to get the color of a pixel using .getpixel() but that only gets one pixel我知道如何使用.getpixel()获取像素的颜色,但只能获取一个像素

Basically, how do i get the color of an area of pixels when i know all the pixels in that area are the same color.基本上,当我知道该区域中的所有像素都是相同颜色时,我如何获得像素区域的颜色。

Also, as quick as possible, like 0.5s or under.此外,尽可能快,例如 0.5 秒或以下。

I keep recommending it, but the scikit-image library is pretty great, and they have some really solid documentation and examples.我一直在推荐它,但是scikit-image库非常棒,而且它们有一些非常可靠的文档和示例。 I would recommend a combo of that and using numpy arrays directly.我会推荐一个组合并直接使用 numpy arrays。 It is just a lot faster when working directly with pixels.直接使用像素时速度要快得多。 You will have to convert the PIL image to a numpy array ...but this should work with that:您必须将PIL 图像转换为 numpy 数组...但这应该适用于:

import pyautogui
import numpy as np

image = pyautogui.screenshot()
np_image = np.array(image)

You can slice the image:您可以对图像进行切片:

red_slice = np_image[0:50, 0:50,0]
red_mask = red_slice == 200

This would give you the values for red in the upper right 50x50 pixel area.这将为您提供右上角 50x50 像素区域中的红色值。 red_mask is an array of True / False values whether each red value in that area is equal to 200 . red_maskTrue / False值的数组,无论该区域中的每个红色值是否等于200 This can be repeated for the other channels as you see fit.如果您认为合适,可以对其他通道重复此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM