如何使用 PIL\Numpy 在 Python 中获取灰度图像的平均像素值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35586206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get an Average Pixel Value of a Gray Scale Image in Python Using PIL\Numpy?
提问by Mdas
回答by MaxNoe
If you want to do stuff like this, you should consider using scikit-image
instead of raw PIL or pillow.
SciKit Image uses numpy arrays for images, so all the numpy methods work.
如果你想做这样的事情,你应该考虑使用scikit-image
而不是原始的 PIL 或枕头。SciKit Image 对图像使用 numpy 数组,因此所有 numpy 方法都有效。
from skimage import io
import numpy as np
image = io.imread('http://i.stack.imgur.com/Y8UeF.jpg')
print(np.mean(image))
You might want to convert all images to float to get a value betwenn 0
and 1
:
您可能希望将所有图像转换为浮点数以获取介于0
和 之间的值1
:
from skimage import io, img_as_float
import numpy as np
image = io.imread('http://i.stack.imgur.com/Y8UeF.jpg')
image = img_as_float(image)
print(np.mean(image))
回答by Yurrit Avonds
This can be done using PIL by looping over the pixels, accumulating all pixel values and dividing by the number of pixels (i.e. width * height)
这可以使用 PIL 来完成,方法是遍历像素,累加所有像素值并除以像素数(即宽度 * 高度)
from PIL import Image
im = Image.open('theimagefile.jpg')
im_grey = im.convert('LA') # convert to grayscale
width, height = im.size
total = 0
for i in range(0, width):
for j in range(0, height):
total += im_grey.getpixel((i,j))[0]
mean = total / (width * height)
print(mean)
回答by doug
the solution is much simpler than those offered in the comments and answers--ie, no computation over tuples and no need for nested loops to iterate over the cell values.
该解决方案比评论和答案中提供的解决方案简单得多——即,不需要对元组进行计算,也不需要嵌套循环来迭代单元格值。
specifically, if you have a gray scale image then you have a 2D array in which the array cells are filled with scalar values from 0 to 1.
具体来说,如果您有一个灰度图像,那么您就有一个二维数组,其中数组单元格填充了 0 到 1 的标量值。
by contrast, a color image is a 2D NumPy array in which an rgb tuplesits in each cell.
相比之下,彩色图像是一个 2D NumPy 数组,其中每个单元格中都有一个rgb 元组。
put another way: a NumPy array representation of a gray-scale image is a 2D array whose cells have float values between 0 (black) and 1 (white)
换句话说:灰度图像的 NumPy 数组表示是一个二维数组,其单元格的浮点值介于 0(黑色)和 1(白色)之间
given this, you can calculate the mean pixel value by calculating the mean along both axis of the image array, like so:
鉴于此,您可以通过计算沿图像数组的两个轴的平均值来计算平均像素值,如下所示:
>>> import numpy as NP
>>> img = NP.random.rand(100, 100)
>>> img[:5, :5]
array([[ 0.824, 0.864, 0.731, 0.57 , 0.127],
[ 0.307, 0.524, 0.637, 0.134, 0.877],
[ 0.343, 0.789, 0.758, 0.059, 0.374],
[ 0.693, 0.991, 0.458, 0.374, 0.738],
[ 0.237, 0.226, 0.869, 0.952, 0.948]])
this single line of code will do what you want--calculate the mean twice, once for each axis in the array (no need to specify an axis for the second call to meanbecause the return value from the first call is just a 1D array
这行代码可以做你想做的事——计算平均值两次,对数组中的每个轴计算一次(不需要为第二次调用指定一个轴来表示,因为第一次调用的返回值只是一个一维数组
>>> img.mean(axis=0).mean()
0.50000646872609511
the value of 0.5 seems correct because the array values were generated by calling NP.random.rand which returns values sampled from a uniform distribution over the half-open interval [0, 1)
0.5 的值似乎是正确的,因为数组值是通过调用 NP.random.rand 生成的,它返回从半开区间 [0, 1) 上的均匀分布采样的值
>>> import matplotlib.pyplot as MPL
>>> MPL.imshow(img, cmap=MPL.cm.gray, interpolation='nearest')
>>> MPL.show()
回答by hrmthw
Maybe the shortest answer:
也许最短的答案:
from PIL import Image
im = Image.open(...)
im.thumbnail((1, 1))
avg_color = im.getpixel(0, 0)