Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/PIL/ImageFilter.py
blob: 58576860d46a43f9bf2c0ecf201e97d5d36cd6f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#
# The Python Imaging Library.
# $Id: ImageFilter.py 2134 2004-10-06 08:55:20Z fredrik $
#
# standard filters
#
# History:
# 1995-11-27 fl   Created
# 2002-06-08 fl   Added rank and mode filters
# 2003-09-15 fl   Fixed rank calculation in rank filter; added expand call
#
# Copyright (c) 1997-2003 by Secret Labs AB.
# Copyright (c) 1995-2002 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#

class Filter:
    pass

##
# Convolution filter kernel.

class Kernel(Filter):

    ##
    # Create a convolution kernel.  The current version only
    # supports 3x3 and 5x5 integer and floating point kernels.
    # <p>
    # In the current version, kernels can only be applied to
    # "L" and "RGB" images.
    #
    # @def __init__(size, kernel, **options)
    # @param size Kernel size, given as (width, height).  In
    #    the current version, this must be (3,3) or (5,5).
    # @param kernel A sequence containing kernel weights.
    # @param **options Optional keyword arguments.
    # @keyparam scale Scale factor.  If given, the result for each
    #    pixel is divided by this value.  The default is the sum
    #    of the kernel weights.
    # @keyparam offset Offset.  If given, this value is added to the
    #    result, after it has been divided by the scale factor.

    def __init__(self, size, kernel, scale=None, offset=0):
        if scale is None:
            # default scale is sum of kernel
            scale = reduce(lambda a,b: a+b, kernel)
        if size[0] * size[1] != len(kernel):
            raise ValueError("not enough coefficients in kernel")
        self.filterargs = size, scale, offset, kernel

    def filter(self, image):
        if image.mode == "P":
            raise ValueError("cannot filter palette images")
        return apply(image.filter, self.filterargs)

class BuiltinFilter(Kernel):
    def __init__(self):
        pass

##
# Rank filter.

class RankFilter(Filter):
    name = "Rank"

    ##
    # Create a rank filter.  The rank filter sorts all pixels in
    # a window of the given size, and returns the rank'th value.
    #
    # @param size The kernel size, in pixels.
    # @param rank What pixel value to pick.  Use 0 for a min filter,
    #    size*size/2 for a median filter, size*size-1 for a max filter,
    #    etc.

    def __init__(self, size, rank):
        self.size = size
        self.rank = rank

    def filter(self, image):
        image = image.expand(self.size/2, self.size/2)
        return image.rankfilter(self.size, self.rank)

##
# Median filter.  Picks the median pixel value in a window with the
# given size.

class MedianFilter(RankFilter):
    name = "Median"

    ##
    # Create a median filter.
    #
    # @param size The kernel size, in pixels.

    def __init__(self, size=3):
        self.size = size
        self.rank = size*size/2

##
# Min filter.  Picks the lowest pixel value in a window with the given
# size.

class MinFilter(RankFilter):
    name = "Min"

    ##
    # Create a min filter.
    #
    # @param size The kernel size, in pixels.

    def __init__(self, size=3):
        self.size = size
        self.rank = 0

##
# Max filter.  Picks the largest pixel value in a window with the
# given size.

class MaxFilter(RankFilter):
    name = "Max"

    ##
    # Create a max filter.
    #
    # @param size The kernel size, in pixels.

    def __init__(self, size=3):
        self.size = size
        self.rank = size*size-1

##
# Mode filter.  Picks the most frequent pixel value in a box with the
# given size.  Pixel values that occur only once or twice are ignored;
# if no pixel value occurs more than twice, the original pixel value
# is preserved.

class ModeFilter(Filter):
    name = "Mode"

    ##
    # Create a mode filter.
    #
    # @param size The kernel size, in pixels.

    def __init__(self, size=3):
        self.size = size
    def filter(self, image):
        return image.modefilter(self.size)

##
# Blur filter.

class BLUR(BuiltinFilter):
    name = "Blur"
    filterargs = (5, 5), 16, 0, (
        1,  1,  1,  1,  1,
        1,  0,  0,  0,  1,
        1,  0,  0,  0,  1,
        1,  0,  0,  0,  1,
        1,  1,  1,  1,  1
        )

##
# Contour filter.

class CONTOUR(BuiltinFilter):
    name = "Contour"
    filterargs = (3, 3), 1, 255, (
        -1, -1, -1,
        -1,  8, -1,
        -1, -1, -1
        )

##
# Detail filter.

class DETAIL(BuiltinFilter):
    name = "Detail"
    filterargs = (3, 3), 6, 0, (
        0, -1,  0,
        -1, 10, -1,
        0, -1,  0
        )

##
# Edge enhancement filter.

class EDGE_ENHANCE(BuiltinFilter):
    name = "Edge-enhance"
    filterargs = (3, 3), 2, 0, (
        -1, -1, -1,
        -1, 10, -1,
        -1, -1, -1
        )

##
# Stronger edge enhancement filter.

class EDGE_ENHANCE_MORE(BuiltinFilter):
    name = "Edge-enhance More"
    filterargs = (3, 3), 1, 0, (
        -1, -1, -1,
        -1,  9, -1,
        -1, -1, -1
        )

##
# Embossing filter.

class EMBOSS(BuiltinFilter):
    name = "Emboss"
    filterargs = (3, 3), 1, 128, (
        -1,  0,  0,
        0,  1,  0,
        0,  0,  0
        )

##
# Edge-finding filter.

class FIND_EDGES(BuiltinFilter):
    name = "Find Edges"
    filterargs = (3, 3), 1, 0, (
        -1, -1, -1,
        -1,  8, -1,
        -1, -1, -1
        )

##
# Smoothing filter.

class SMOOTH(BuiltinFilter):
    name = "Smooth"
    filterargs = (3, 3), 13, 0, (
        1,  1,  1,
        1,  5,  1,
        1,  1,  1
        )

##
# Stronger smoothing filter.

class SMOOTH_MORE(BuiltinFilter):
    name = "Smooth More"
    filterargs = (5, 5), 100, 0, (
        1,  1,  1,  1,  1,
        1,  5,  5,  5,  1,
        1,  5, 44,  5,  1,
        1,  5,  5,  5,  1,
        1,  1,  1,  1,  1
        )

##
# Sharpening filter.

class SHARPEN(BuiltinFilter):
    name = "Sharpen"
    filterargs = (3, 3), 16, 0, (
        -2, -2, -2,
        -2, 32, -2,
        -2, -2, -2
        )