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.
Use the average() Function of NumPy to Find the Average Color of Images in Python. In mathematics, we can find the average of a vector by dividing the sum of all the elements in the vector by the total number of elements.
Mean value is the sum of pixel values divided by the total number of pixel values. Pixel Values Each of the pixels that represents an image stored inside a computer has a pixel value which describes how bright that pixel is, and/or what color it should be.
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))
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With