如何使用PIL \ Numpy在Python中获取灰度图像的平均像素值? [英] How to Get an Average Pixel Value of a Gray Scale Image in Python Using PIL\Numpy?

查看:1061
本文介绍了如何使用PIL \ Numpy在Python中获取灰度图像的平均像素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的灰度图像很少,因此我考虑了计算整个图像的平均像素值,以便可以使用单个值表示每个单独的图像.

I have few gray scale images and I thought of calculating the average pixel value of the total image, so that I can represent each individual image using a single value.

推荐答案

如果要执行此类操作,则应考虑使用scikit-image而不是原始的PIL或枕头. SciKit Image使用numpy数组存储图像,因此所有numpy方法都可以使用.

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.

from skimage import io
import numpy as np

image = io.imread('http://i.stack.imgur.com/Y8UeF.jpg')

print(np.mean(image))

您可能希望将所有图像都转换为float值,以获取01之间的值:

You might want to convert all images to float to get a value betwenn 0 and 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))

这篇关于如何使用PIL \ Numpy在Python中获取灰度图像的平均像素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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