From 8a4081c5bc0c6fc12756109b87b6ac0f2b5ac5c9 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 5 Jul 2014 12:08:35 +0300 Subject: [PATCH 1/3] More tests for ImageMath --- Tests/test_imagemath.py | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py index 35d75dbbd..a03b25cce 100644 --- a/Tests/test_imagemath.py +++ b/Tests/test_imagemath.py @@ -14,9 +14,13 @@ def pixel(im): A = Image.new("L", (1, 1), 1) B = Image.new("L", (1, 1), 2) +Z = Image.new("L", (1, 1), 0) # Z for zero F = Image.new("F", (1, 1), 3) I = Image.new("I", (1, 1), 4) +A2 = A.resize((2, 2)) +B2 = B.resize((2, 2)) + images = {"A": A, "B": B, "F": F, "I": I} @@ -71,6 +75,86 @@ class TestImageMath(PillowTestCase): self.assertEqual(pixel(ImageMath.eval("A == 1", images)), "I 1") self.assertEqual(pixel(ImageMath.eval("A == 2", images)), "I 0") + def test_one_image_larger(self): + self.assertEqual(pixel(ImageMath.eval("A+B", A=A2, B=B)), "I 3") + self.assertEqual(pixel(ImageMath.eval("A+B", A=A, B=B2)), "I 3") + + def test_abs(self): + self.assertEqual(pixel(ImageMath.eval("abs(A)", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("abs(B)", B=B)), "I 2") + + def test_bitwise_invert(self): + self.assertEqual(pixel(ImageMath.eval("~Z", Z=Z)), "I -1") + self.assertEqual(pixel(ImageMath.eval("~A", A=A)), "I -2") + self.assertEqual(pixel(ImageMath.eval("~B", B=B)), "I -3") + + def test_bitwise_and(self): + self.assertEqual(pixel(ImageMath.eval("Z&Z", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z&A", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A&Z", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A&A", A=A, Z=Z)), "I 1") + + def test_bitwise_or(self): + self.assertEqual(pixel(ImageMath.eval("Z|Z", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z|A", A=A, Z=Z)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A|Z", A=A, Z=Z)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A|A", A=A, Z=Z)), "I 1") + + def test_bitwise_xor(self): + self.assertEqual(pixel(ImageMath.eval("Z^Z", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z^A", A=A, Z=Z)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A^Z", A=A, Z=Z)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A^A", A=A, Z=Z)), "I 0") + + def test_bitwise_leftshift(self): + self.assertEqual(pixel(ImageMath.eval("Z<<0", Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z<<1", Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A<<0", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A<<1", A=A)), "I 2") + + def test_bitwise_rightshift(self): + self.assertEqual(pixel(ImageMath.eval("Z>>0", Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z>>1", Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A>>0", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A>>1", A=A)), "I 0") + + def test_logical_eq(self): + self.assertEqual(pixel(ImageMath.eval("A==A", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("B==B", B=B)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A==B", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B==A", A=A, B=B)), "I 0") + + def test_logical_ne(self): + self.assertEqual(pixel(ImageMath.eval("A!=A", A=A)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B!=B", B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A!=B", A=A, B=B)), "I 1") + self.assertEqual(pixel(ImageMath.eval("B!=A", A=A, B=B)), "I 1") + + def test_logical_lt(self): + self.assertEqual(pixel(ImageMath.eval("AA", A=A)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B>B", B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A>B", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B>A", A=A, B=B)), "I 1") + + def test_logical_ge(self): + self.assertEqual(pixel(ImageMath.eval("A>=A", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("B>=B", B=B)), "I 1") + self.assertEqual(pixel(ImageMath.eval("A>=B", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B>=A", A=A, B=B)), "I 1") + + if __name__ == '__main__': unittest.main() From 5f2138d91588f792624ca5ebf599698be88f8879 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 5 Jul 2014 12:13:43 +0300 Subject: [PATCH 2/3] flake8 ImageMath.py --- PIL/ImageMath.py | 53 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/PIL/ImageMath.py b/PIL/ImageMath.py index adfcc4f6f..4dcc5125c 100644 --- a/PIL/ImageMath.py +++ b/PIL/ImageMath.py @@ -26,9 +26,11 @@ except ImportError: VERBOSE = 0 + def _isconstant(v): return isinstance(v, int) or isinstance(v, float) + class _Operand: # wraps an image operand, providing standard operators @@ -68,20 +70,25 @@ class _Operand: im2 = self.__fixup(im2) if im1.mode != im2.mode: # convert both arguments to floating point - if im1.mode != "F": im1 = im1.convert("F") - if im2.mode != "F": im2 = im2.convert("F") + if im1.mode != "F": + im1 = im1.convert("F") + if im2.mode != "F": + im2 = im2.convert("F") if im1.mode != im2.mode: raise ValueError("mode mismatch") if im1.size != im2.size: # crop both arguments to a common size size = (min(im1.size[0], im2.size[0]), min(im1.size[1], im2.size[1])) - if im1.size != size: im1 = im1.crop((0, 0) + size) - if im2.size != size: im2 = im2.crop((0, 0) + size) + if im1.size != size: + im1 = im1.crop((0, 0) + size) + if im2.size != size: + im2 = im2.crop((0, 0) + size) out = Image.new(mode or im1.mode, size, None) else: out = Image.new(mode or im1.mode, im1.size, None) - im1.load(); im2.load() + im1.load() + im2.load() try: op = getattr(_imagingmath, op+"_"+im1.mode) except AttributeError: @@ -101,34 +108,47 @@ class _Operand: def __abs__(self): return self.apply("abs", self) + def __pos__(self): return self + def __neg__(self): return self.apply("neg", self) # binary operators def __add__(self, other): return self.apply("add", self, other) + def __radd__(self, other): return self.apply("add", other, self) + def __sub__(self, other): return self.apply("sub", self, other) + def __rsub__(self, other): return self.apply("sub", other, self) + def __mul__(self, other): return self.apply("mul", self, other) + def __rmul__(self, other): return self.apply("mul", other, self) + def __truediv__(self, other): return self.apply("div", self, other) + def __rtruediv__(self, other): return self.apply("div", other, self) + def __mod__(self, other): return self.apply("mod", self, other) + def __rmod__(self, other): return self.apply("mod", other, self) + def __pow__(self, other): return self.apply("pow", self, other) + def __rpow__(self, other): return self.apply("pow", other, self) @@ -142,54 +162,77 @@ class _Operand: # bitwise def __invert__(self): return self.apply("invert", self) + def __and__(self, other): return self.apply("and", self, other) + def __rand__(self, other): return self.apply("and", other, self) + def __or__(self, other): return self.apply("or", self, other) + def __ror__(self, other): return self.apply("or", other, self) + def __xor__(self, other): return self.apply("xor", self, other) + def __rxor__(self, other): return self.apply("xor", other, self) + def __lshift__(self, other): return self.apply("lshift", self, other) + def __rshift__(self, other): return self.apply("rshift", self, other) # logical def __eq__(self, other): return self.apply("eq", self, other) + def __ne__(self, other): return self.apply("ne", self, other) + def __lt__(self, other): return self.apply("lt", self, other) + def __le__(self, other): return self.apply("le", self, other) + def __gt__(self, other): return self.apply("gt", self, other) + def __ge__(self, other): return self.apply("ge", self, other) + # conversions def imagemath_int(self): return _Operand(self.im.convert("I")) + + def imagemath_float(self): return _Operand(self.im.convert("F")) + # logical def imagemath_equal(self, other): return self.apply("eq", self, other, mode="I") + + def imagemath_notequal(self, other): return self.apply("ne", self, other, mode="I") + def imagemath_min(self, other): return self.apply("min", self, other) + + def imagemath_max(self, other): return self.apply("max", self, other) + def imagemath_convert(self, mode): return _Operand(self.im.convert(mode)) From e3911facbc84e2d17f04b190afb2f22091e4430b Mon Sep 17 00:00:00 2001 From: hugovk Date: Wed, 9 Jul 2014 21:53:13 +0300 Subject: [PATCH 3/3] Test ImageMath's mod, imagemath_equal and imagemath_notequal --- Tests/test_imagemath.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Tests/test_imagemath.py b/Tests/test_imagemath.py index a03b25cce..562a2f8f6 100644 --- a/Tests/test_imagemath.py +++ b/Tests/test_imagemath.py @@ -83,6 +83,14 @@ class TestImageMath(PillowTestCase): self.assertEqual(pixel(ImageMath.eval("abs(A)", A=A)), "I 1") self.assertEqual(pixel(ImageMath.eval("abs(B)", B=B)), "I 2") + def test_binary_mod(self): + self.assertEqual(pixel(ImageMath.eval("A%A", A=A)), "I 0") + self.assertEqual(pixel(ImageMath.eval("B%B", B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("A%B", A=A, B=B)), "I 1") + self.assertEqual(pixel(ImageMath.eval("B%A", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z%A", A=A, Z=Z)), "I 0") + self.assertEqual(pixel(ImageMath.eval("Z%B", B=B, Z=Z)), "I 0") + def test_bitwise_invert(self): self.assertEqual(pixel(ImageMath.eval("~Z", Z=Z)), "I -1") self.assertEqual(pixel(ImageMath.eval("~A", A=A)), "I -2") @@ -154,6 +162,24 @@ class TestImageMath(PillowTestCase): self.assertEqual(pixel(ImageMath.eval("A>=B", A=A, B=B)), "I 0") self.assertEqual(pixel(ImageMath.eval("B>=A", A=A, B=B)), "I 1") + def test_logical_equal(self): + self.assertEqual(pixel(ImageMath.eval("equal(A, A)", A=A)), "I 1") + self.assertEqual(pixel(ImageMath.eval("equal(B, B)", B=B)), "I 1") + self.assertEqual(pixel(ImageMath.eval("equal(Z, Z)", Z=Z)), "I 1") + self.assertEqual(pixel(ImageMath.eval("equal(A, B)", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("equal(B, A)", A=A, B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("equal(A, Z)", A=A, Z=Z)), "I 0") + + def test_logical_not_equal(self): + self.assertEqual(pixel(ImageMath.eval("notequal(A, A)", A=A)), "I 0") + self.assertEqual(pixel(ImageMath.eval("notequal(B, B)", B=B)), "I 0") + self.assertEqual(pixel(ImageMath.eval("notequal(Z, Z)", Z=Z)), "I 0") + self.assertEqual( + pixel(ImageMath.eval("notequal(A, B)", A=A, B=B)), "I 1") + self.assertEqual( + pixel(ImageMath.eval("notequal(B, A)", A=A, B=B)), "I 1") + self.assertEqual( + pixel(ImageMath.eval("notequal(A, Z)", A=A, Z=Z)), "I 1") if __name__ == '__main__':