Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: use the comparison mask for screendiff output

SlivenRedca3a9c99b53c
diff --git a/tests/test_screendiff.py b/tests/test_screendiff.py
index 2249b19f2..cbac80bd2 100644
--- a/tests/test_screendiff.py
+++ b/tests/test_screendiff.py
@@ -75,6 +75,47 @@ class CompareTest(unittest.TestCase):
self.assertEqual(code, 1)
self.assertIn("inside 150,60,191,91", text)
+ def test_a_masked_only_change_writes_an_empty_diff(self):
+ a, b = self.shot("a.png", "12:00"), self.shot("b.png", "12:01")
+ out = os.path.join(self.tmp.name, "diff.png")
+ for masks in (["--mask", "0,0,60,20"], ["--mask", "0,0,60,20", "--mask", "0,0,60,20"]):
+ with self.subTest(masks=masks):
+ code, text = self.run_tool([a, b, *masks, "--diff", out])
+ self.assertEqual(code, 0)
+ self.assertIn("identical", text)
+ with self.Image.open(out) as diff:
+ self.assertEqual(set(diff.convert("RGB").getdata()), {(0, 0, 0)})
+
+ def test_diff_pixels_match_the_report_in_every_channel_and_at_the_threshold(self):
+ before = self.Image.new("RGB", (9, 4), "black")
+ after = before.copy()
+ for point, colour in (((1, 1), (255, 0, 0)), ((3, 1), (255, 255, 255)),
+ ((4, 1), (9, 0, 0)), ((5, 1), (0, 9, 0)), ((6, 1), (0, 0, 9)),
+ ((7, 1), (8, 8, 8)), ((8, 1), (7, 0, 0))):
+ after.putpixel(point, colour)
+ a, b, out = [os.path.join(self.tmp.name, name) for name in ("a.png", "b.png", "diff.png")]
+ before.save(a)
+ after.save(b)
+ code, text = self.run_tool([a, b, "--mask", "0,0,4,4", "--threshold", "8", "--diff", out])
+ self.assertEqual(code, 1)
+ self.assertIn("3 pixels differ by more than 8, inside 4,1,7,2", text)
+ with self.Image.open(out) as exported:
+ diff = exported.convert("RGB")
+ self.assertEqual(set(diff.getdata()), {(0, 0, 0), (255, 255, 255)})
+ self.assertEqual(diff.getbbox(), (4, 1, 7, 2))
+ self.assertEqual(sum(pixel != (0, 0, 0) for pixel in diff.getdata()), 3)
+ for point in ((4, 1), (5, 1), (6, 1)):
+ self.assertEqual(diff.getpixel(point), (255, 255, 255))
+
+ def test_compare_keeps_its_three_value_result_and_does_not_change_the_inputs(self):
+ before = self.Image.new("RGB", (8, 4), "black")
+ after = before.copy()
+ after.putpixel((1, 1), (255, 0, 0))
+ after.putpixel((5, 1), (0, 9, 0))
+ original = before.tobytes(), after.tobytes()
+ self.assertEqual(screendiff.compare(before, after, [(0, 0, 4, 4)], 8), (1, (5, 1, 6, 2), 9))
+ self.assertEqual((before.tobytes(), after.tobytes()), original)
+
def test_compression_noise_under_the_threshold_is_ignored(self):
a, b = self.shot("a.png"), self.shot("b.png", noise=3)
self.assertEqual(self.run_tool([a, b])[0], 0)
diff --git a/tools/screendiff.py b/tools/screendiff.py
index 060a324dc..4dd135717 100644
--- a/tools/screendiff.py
+++ b/tools/screendiff.py
@@ -31,6 +31,11 @@ def box(text):
def compare(a, b, masks, threshold):
"""(pixels that differ by more than threshold in any channel, their bounding box or None, the largest difference)."""
+ return _compare(a, b, masks, threshold)[:3]
+
+
+def _compare(a, b, masks, threshold):
+ """The comparison numbers and the binary pixel mask they describe."""
from PIL import ImageChops, ImageDraw
a, b = a.convert("RGB").copy(), b.convert("RGB").copy()
for im in (a, b):
@@ -43,7 +48,7 @@ def compare(a, b, masks, threshold):
worst = ImageChops.lighter(ImageChops.lighter(r, g), bl)
over = worst.point(lambda v: 255 if v > threshold else 0)
count = over.histogram()[255]
- return count, over.getbbox(), max(worst.getdata())
+ return count, over.getbbox(), max(worst.getdata()), over
def redact(im, region):
@@ -118,13 +123,12 @@ def main(argv=None):
if outside:
print(f"a box is outside the {a.size[0]}x{a.size[1]} screenshot: {outside[0]}", file=sys.stderr)
return 2
- count, bbox, worst = compare(a, b, args.mask, args.threshold)
+ count, bbox, worst, diff = _compare(a, b, args.mask, args.threshold)
if args.out:
pair(a, b, args.blur, args.label_pair, args.scale).save(args.out)
print(f"{args.out}: written")
if args.diff:
- from PIL import ImageChops
- ImageChops.difference(a.convert("RGB"), b.convert("RGB")).point(lambda v: 255 if v > args.threshold else 0).save(args.diff)
+ diff.save(args.diff)
masked = f" ({len(args.mask)} region{'s' if len(args.mask) != 1 else ''} masked)" if args.mask else ""
if count == 0:
print(f"identical{masked}: no pixel differs by more than {args.threshold}")