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

« back to all changes in this revision

Viewing changes to PIL/ImageMath.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# See the README file for information on usage and redistribution.
16
16
#
17
17
 
18
 
import Image
 
18
from . import Image
19
19
import _imagingmath
 
20
import sys
 
21
 
 
22
try:
 
23
    import builtins
 
24
except ImportError:
 
25
    import __builtin__
 
26
    builtins = __builtin__
20
27
 
21
28
VERBOSE = 0
22
29
 
23
30
def _isconstant(v):
24
 
    return isinstance(v, type(0)) or isinstance(v, type(0.0))
 
31
    return isinstance(v, int) or isinstance(v, float)
25
32
 
26
33
class _Operand:
27
34
    # wraps an image operand, providing standard operators
38
45
            elif im1.im.mode in ("I", "F"):
39
46
                return im1.im
40
47
            else:
41
 
                raise ValueError, "unsupported mode: %s" % im1.im.mode
 
48
                raise ValueError("unsupported mode: %s" % im1.im.mode)
42
49
        else:
43
50
            # argument was a constant
44
51
            if _isconstant(im1) and self.im.mode in ("1", "L", "I"):
55
62
            try:
56
63
                op = getattr(_imagingmath, op+"_"+im1.mode)
57
64
            except AttributeError:
58
 
                raise TypeError, "bad operand type for '%s'" % op
 
65
                raise TypeError("bad operand type for '%s'" % op)
59
66
            _imagingmath.unop(op, out.im.id, im1.im.id)
60
67
        else:
61
68
            # binary operation
65
72
                if im1.mode != "F": im1 = im1.convert("F")
66
73
                if im2.mode != "F": im2 = im2.convert("F")
67
74
                if im1.mode != im2.mode:
68
 
                    raise ValueError, "mode mismatch"
 
75
                    raise ValueError("mode mismatch")
69
76
            if im1.size != im2.size:
70
77
                # crop both arguments to a common size
71
78
                size = (min(im1.size[0], im2.size[0]),
79
86
            try:
80
87
                op = getattr(_imagingmath, op+"_"+im1.mode)
81
88
            except AttributeError:
82
 
                raise TypeError, "bad operand type for '%s'" % op
 
89
                raise TypeError("bad operand type for '%s'" % op)
83
90
            _imagingmath.binop(op, out.im.id, im1.im.id, im2.im.id)
84
91
        return _Operand(out)
85
92
 
86
93
    # unary operators
87
 
    def __nonzero__(self):
 
94
    def __bool__(self):
88
95
        # an image is "true" if it contains at least one non-zero pixel
89
96
        return self.im.getbbox() is not None
 
97
 
 
98
    if bytes is str:
 
99
        # Provide __nonzero__ for pre-Py3k
 
100
        __nonzero__ = __bool__
 
101
        del __bool__
 
102
 
90
103
    def __abs__(self):
91
104
        return self.apply("abs", self)
92
105
    def __pos__(self):
107
120
        return self.apply("mul", self, other)
108
121
    def __rmul__(self, other):
109
122
        return self.apply("mul", other, self)
110
 
    def __div__(self, other):
 
123
    def __truediv__(self, other):
111
124
        return self.apply("div", self, other)
112
 
    def __rdiv__(self, other):
 
125
    def __rtruediv__(self, other):
113
126
        return self.apply("div", other, self)
114
127
    def __mod__(self, other):
115
128
        return self.apply("mod", self, other)
120
133
    def __rpow__(self, other):
121
134
        return self.apply("pow", other, self)
122
135
 
 
136
    if bytes is str:
 
137
        # Provide __div__ and __rdiv__ for pre-Py3k
 
138
        __div__ = __truediv__
 
139
        __rdiv__ = __rtruediv__
 
140
        del __truediv__
 
141
        del __rtruediv__
 
142
 
123
143
    # bitwise
124
144
    def __invert__(self):
125
145
        return self.apply("invert", self)
175
195
    return _Operand(self.im.convert(mode))
176
196
 
177
197
ops = {}
178
 
for k, v in globals().items():
 
198
for k, v in list(globals().items()):
179
199
    if k[:10] == "imagemath_":
180
200
        ops[k[10:]] = v
181
201
 
195
215
    args = ops.copy()
196
216
    args.update(_dict)
197
217
    args.update(kw)
198
 
    for k, v in args.items():
 
218
    for k, v in list(args.items()):
199
219
        if hasattr(v, "im"):
200
220
            args[k] = _Operand(v)
201
221
 
202
 
    import __builtin__
203
 
    out =__builtin__.eval(expression, args)
 
222
    out = builtins.eval(expression, args)
204
223
    try:
205
224
        return out.im
206
225
    except AttributeError: