def test_convert(self): self.assertEqual(pixel( ImageMath.eval("convert(A+B, 'L')", images)), "L 3") self.assertEqual(pixel( ImageMath.eval("convert(A+B, '1')", images)), "1 0") self.assertEqual(pixel( ImageMath.eval("convert(A+B, 'RGB')", images)), "RGB (3, 3, 3)")
def assert_image_similar(self, a, b, epsilon, msg=None): epsilon = float(epsilon) self.assertEqual( a.mode, b.mode, msg or "got mode %r, expected %r" % (a.mode, b.mode)) self.assertEqual( a.size, b.size, msg or "got size %r, expected %r" % (a.size, b.size)) a, b = convert_to_comparable(a, b) diff = 0 for ach, bch in zip(a.split(), b.split()): chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert('L') diff += sum(i * num for i, num in enumerate(chdiff.histogram())) ave_diff = float(diff)/(a.size[0]*a.size[1]) try: self.assertGreaterEqual( epsilon, ave_diff, (msg or '') + " average pixel value difference %.4f > epsilon %.4f" % ( ave_diff, epsilon)) except Exception as e: if HAS_UPLOADER: try: url = test_image_results.upload(a, b) logger.error("Url for test images: %s" % url) except: pass raise e
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_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_sanity(self): self.assertEqual(ImageMath.eval("1"), 1) self.assertEqual(ImageMath.eval("1+A", A=2), 3) self.assertEqual(pixel(ImageMath.eval("A+B", A=A, B=B)), "I 3") self.assertEqual(pixel(ImageMath.eval("A+B", images)), "I 3") self.assertEqual(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0") self.assertEqual(pixel( ImageMath.eval("int(float(A)+B)", images)), "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_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_compare(self): self.assertEqual(pixel(ImageMath.eval("min(A, B)", images)), "I 1") self.assertEqual(pixel(ImageMath.eval("max(A, B)", images)), "I 2") self.assertEqual(pixel(ImageMath.eval("A == 1", images)), "I 1") self.assertEqual(pixel(ImageMath.eval("A == 2", images)), "I 0")
def test_logical(self): self.assertEqual(pixel(ImageMath.eval("not A", images)), 0) self.assertEqual(pixel(ImageMath.eval("A and B", images)), "L 2") self.assertEqual(pixel(ImageMath.eval("A or B", images)), "L 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_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")
def test_logical_lt(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")
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_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_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_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_ops(self): self.assertEqual(pixel(ImageMath.eval("-A", images)), "I -1") self.assertEqual(pixel(ImageMath.eval("+B", images)), "L 2") self.assertEqual(pixel(ImageMath.eval("A+B", images)), "I 3") self.assertEqual(pixel(ImageMath.eval("A-B", images)), "I -1") self.assertEqual(pixel(ImageMath.eval("A*B", images)), "I 2") self.assertEqual(pixel(ImageMath.eval("A/B", images)), "I 0") self.assertEqual(pixel(ImageMath.eval("B**2", images)), "I 4") self.assertEqual(pixel( ImageMath.eval("B**33", images)), "I 2147483647") self.assertEqual(pixel(ImageMath.eval("float(A)+B", images)), "F 3.0") self.assertEqual(pixel(ImageMath.eval("float(A)-B", images)), "F -1.0") self.assertEqual(pixel(ImageMath.eval("float(A)*B", images)), "F 2.0") self.assertEqual(pixel(ImageMath.eval("float(A)/B", images)), "F 0.5") self.assertEqual(pixel(ImageMath.eval("float(B)**2", images)), "F 4.0") self.assertEqual(pixel( ImageMath.eval("float(B)**33", images)), "F 8589934592.0")
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")