2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# global image statistics
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1996-04-05 fl Created
|
|
|
|
# 1997-05-21 fl Added mask; added rms, var, stddev attributes
|
|
|
|
# 1997-08-05 fl Added median
|
|
|
|
# 1998-07-05 hk Fixed integer overflow error
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# This class shows how to implement delayed evaluation of attributes.
|
|
|
|
# To get a certain value, simply access the corresponding attribute.
|
|
|
|
# The __getattr__ dispatcher takes care of the rest.
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997.
|
|
|
|
# Copyright (c) Fredrik Lundh 1996-97.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
import math
|
|
|
|
import operator
|
2012-10-16 06:15:25 +04:00
|
|
|
from functools import reduce
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Stat:
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
def __init__(self, image_or_list, mask=None):
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
if mask:
|
|
|
|
self.h = image_or_list.histogram(mask)
|
|
|
|
else:
|
|
|
|
self.h = image_or_list.histogram()
|
|
|
|
except AttributeError:
|
2014-08-26 17:47:10 +04:00
|
|
|
self.h = image_or_list # assume it to be a histogram list
|
2012-10-17 07:39:56 +04:00
|
|
|
if not isinstance(self.h, list):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise TypeError("first argument must be image or list")
|
2012-10-16 01:18:27 +04:00
|
|
|
self.bands = list(range(len(self.h) // 256))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __getattr__(self, id):
|
|
|
|
"Calculate missing attribute"
|
|
|
|
if id[:4] == "_get":
|
2012-10-11 07:52:53 +04:00
|
|
|
raise AttributeError(id)
|
2010-07-31 06:52:47 +04:00
|
|
|
# calculate missing attribute
|
|
|
|
v = getattr(self, "_get" + id)()
|
|
|
|
setattr(self, id, v)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getextrema(self):
|
|
|
|
"Get min/max values for each band in the image"
|
|
|
|
|
|
|
|
def minmax(histogram):
|
|
|
|
n = 255
|
|
|
|
x = 0
|
|
|
|
for i in range(256):
|
|
|
|
if histogram[i]:
|
|
|
|
n = min(n, i)
|
|
|
|
x = max(x, i)
|
2014-08-26 17:47:10 +04:00
|
|
|
return n, x # returns (255, 0) if there's no data in the histogram
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in range(0, len(self.h), 256):
|
|
|
|
v.append(minmax(self.h[i:]))
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getcount(self):
|
|
|
|
"Get total number of pixels in each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in range(0, len(self.h), 256):
|
|
|
|
v.append(reduce(operator.add, self.h[i:i+256]))
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getsum(self):
|
|
|
|
"Get sum of all pixels in each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in range(0, len(self.h), 256):
|
|
|
|
sum = 0.0
|
|
|
|
for j in range(256):
|
2014-05-10 08:36:15 +04:00
|
|
|
sum += j * self.h[i + j]
|
2010-07-31 06:52:47 +04:00
|
|
|
v.append(sum)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getsum2(self):
|
|
|
|
"Get squared sum of all pixels in each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in range(0, len(self.h), 256):
|
|
|
|
sum2 = 0.0
|
|
|
|
for j in range(256):
|
2014-05-10 08:36:15 +04:00
|
|
|
sum2 += (j ** 2) * float(self.h[i + j])
|
2010-07-31 06:52:47 +04:00
|
|
|
v.append(sum2)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getmean(self):
|
|
|
|
"Get average pixel level for each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in self.bands:
|
|
|
|
v.append(self.sum[i] / self.count[i])
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getmedian(self):
|
|
|
|
"Get median pixel level for each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in self.bands:
|
|
|
|
s = 0
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
l = self.count[i]//2
|
2010-07-31 06:52:47 +04:00
|
|
|
b = i * 256
|
|
|
|
for j in range(256):
|
|
|
|
s = s + self.h[b+j]
|
|
|
|
if s > l:
|
|
|
|
break
|
|
|
|
v.append(j)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getrms(self):
|
|
|
|
"Get RMS for each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in self.bands:
|
|
|
|
v.append(math.sqrt(self.sum2[i] / self.count[i]))
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getvar(self):
|
|
|
|
"Get variance for each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in self.bands:
|
|
|
|
n = self.count[i]
|
|
|
|
v.append((self.sum2[i]-(self.sum[i]**2.0)/n)/n)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def _getstddev(self):
|
|
|
|
"Get standard deviation for each layer"
|
|
|
|
|
|
|
|
v = []
|
|
|
|
for i in self.bands:
|
|
|
|
v.append(math.sqrt(self.var[i]))
|
|
|
|
return v
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
Global = Stat # compatibility
|