简体   繁体   中英

How to Get an Average Pixel Value of a Gray Scale Image in Python Using PIL\Numpy?

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.

在此输入图像描述

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))

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))

This can be done using PIL by looping over the pixels, accumulating all pixel values and dividing by the number of pixels (ie width * height)

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)

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.

by contrast, a color image is a 2D NumPy array in which an rgb tuple sits in each cell.

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)

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 mean because 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)

>>> import matplotlib.pyplot as MPL
>>> MPL.imshow(img, cmap=MPL.cm.gray, interpolation='nearest')
>>> MPL.show()

在此输入图像描述

Maybe the shortest answer:

from PIL import Image

im = Image.open(...)
im.thumbnail((1, 1))
avg_color = im.getpixel(0, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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