~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/PIL/ImageStat.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (1.1.4)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130131204920-7tnuhqhlsqdza4c2
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# The Python Imaging Library.
 
3
# $Id$
 
4
#
 
5
# global image statistics
 
6
#
 
7
# History:
 
8
# 1996-04-05 fl   Created
 
9
# 1997-05-21 fl   Added mask; added rms, var, stddev attributes
 
10
# 1997-08-05 fl   Added median
 
11
# 1998-07-05 hk   Fixed integer overflow error
 
12
#
 
13
# Notes:
 
14
# This class shows how to implement delayed evaluation of attributes.
 
15
# To get a certain value, simply access the corresponding attribute.
 
16
# The __getattr__ dispatcher takes care of the rest.
 
17
#
 
18
# Copyright (c) Secret Labs AB 1997.
 
19
# Copyright (c) Fredrik Lundh 1996-97.
 
20
#
 
21
# See the README file for information on usage and redistribution.
 
22
#
 
23
 
 
24
import Image
 
25
import operator, math
 
26
 
 
27
##
 
28
# The <b>ImageStat</b> module calculates global statistics for an
 
29
# image, or a region of an image.
 
30
##
 
31
 
 
32
##
 
33
# Calculate statistics for the given image.  If a mask is included,
 
34
# only the regions covered by that mask are included in the
 
35
# statistics.
 
36
 
 
37
class Stat:
 
38
    "Get image or feature statistics"
 
39
 
 
40
    ##
 
41
    # Create a statistics object.
 
42
    #
 
43
    # @def __init__(image, mask=None)
 
44
    # @param image A PIL image, or a precalculate histogram.
 
45
    # @param mask An optional mask.
 
46
 
 
47
    def __init__(self, image_or_list, mask = None):
 
48
        try:
 
49
            if mask:
 
50
                self.h = image_or_list.histogram(mask)
 
51
            else:
 
52
                self.h = image_or_list.histogram()
 
53
        except AttributeError:
 
54
            self.h = image_or_list # assume it to be a histogram list
 
55
        if type(self.h) != type([]):
 
56
            raise TypeError, "first argument must be image or list"
 
57
        self.bands = range(len(self.h) / 256)
 
58
 
 
59
    def __getattr__(self, id):
 
60
        "Calculate missing attribute"
 
61
        if id[:4] == "_get":
 
62
            raise AttributeError, id
 
63
        # calculate missing attribute
 
64
        v = getattr(self, "_get" + id)()
 
65
        setattr(self, id, v)
 
66
        return v
 
67
 
 
68
    def _getextrema(self):
 
69
        "Get min/max values for each band in the image"
 
70
 
 
71
        def minmax(histogram):
 
72
            n = 255
 
73
            x = 0
 
74
            for i in range(256):
 
75
                if histogram[i]:
 
76
                    n = min(n, i)
 
77
                    x = max(x, i)
 
78
            return n, x # returns (255, 0) if there's no data in the histogram
 
79
 
 
80
        v = []
 
81
        for i in range(0, len(self.h), 256):
 
82
            v.append(minmax(self.h[i:]))
 
83
        return v
 
84
 
 
85
    def _getcount(self):
 
86
        "Get total number of pixels in each layer"
 
87
 
 
88
        v = []
 
89
        for i in range(0, len(self.h), 256):
 
90
            v.append(reduce(operator.add, self.h[i:i+256]))
 
91
        return v
 
92
 
 
93
    def _getsum(self):
 
94
        "Get sum of all pixels in each layer"
 
95
 
 
96
        v = []
 
97
        for i in range(0, len(self.h), 256):
 
98
            sum = 0.0
 
99
            for j in range(256):
 
100
                sum = sum + j * self.h[i+j]
 
101
            v.append(sum)
 
102
        return v
 
103
 
 
104
    def _getsum2(self):
 
105
        "Get squared sum of all pixels in each layer"
 
106
 
 
107
        v = []
 
108
        for i in range(0, len(self.h), 256):
 
109
            sum2 = 0.0
 
110
            for j in range(256):
 
111
                sum2 = sum2 + (j ** 2) * float(self.h[i+j])
 
112
            v.append(sum2)
 
113
        return v
 
114
 
 
115
    def _getmean(self):
 
116
        "Get average pixel level for each layer"
 
117
 
 
118
        v = []
 
119
        for i in self.bands:
 
120
            v.append(self.sum[i] / self.count[i])
 
121
        return v
 
122
 
 
123
    def _getmedian(self):
 
124
        "Get median pixel level for each layer"
 
125
 
 
126
        v = []
 
127
        for i in self.bands:
 
128
            s = 0
 
129
            l = self.count[i]/2
 
130
            b = i * 256
 
131
            for j in range(256):
 
132
                s = s + self.h[b+j]
 
133
                if s > l:
 
134
                    break
 
135
            v.append(j)
 
136
        return v
 
137
 
 
138
    def _getrms(self):
 
139
        "Get RMS for each layer"
 
140
 
 
141
        v = []
 
142
        for i in self.bands:
 
143
            v.append(math.sqrt(self.sum2[i] / self.count[i]))
 
144
        return v
 
145
 
 
146
 
 
147
    def _getvar(self):
 
148
        "Get variance for each layer"
 
149
 
 
150
        v = []
 
151
        for i in self.bands:
 
152
            n = self.count[i]
 
153
            v.append((self.sum2[i]-(self.sum[i]**2.0)/n)/n)
 
154
        return v
 
155
 
 
156
    def _getstddev(self):
 
157
        "Get standard deviation for each layer"
 
158
 
 
159
        v = []
 
160
        for i in self.bands:
 
161
            v.append(math.sqrt(self.var[i]))
 
162
        return v
 
163
 
 
164
Global = Stat # compatibility