check_frame.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """check_frame.py [--baseline base.png] shot.png [shot.png ...] - did the wallpaper reach the screen?
  2. check_frame.py --differ a.png b.png - do two frames differ (hotswap)?
  3. Without --baseline a screenshot only fails when it is pure black. With the baseline (the desktop's own screenshot taken
  4. before the engine started) it must also differ from it along the right edge and the top, where windows, panels and
  5. desktop icons rarely are, so a desktop that still shows its default background fails. Prints one "PASS|FAIL <file>: <why>"
  6. line per file, exit code 1 when any failed. Reference frames per wallpaper (upside down, wrong crop) would be the next
  7. step, see RELEASE_TESTING.md.
  8. """
  9. import sys
  10. from PIL import Image, ImageChops, ImageStat
  11. def strips(image):
  12. w, h = image.size
  13. return [
  14. image.crop((w * 88 // 100, h * 15 // 100, w * 99 // 100, h * 80 // 100)),
  15. image.crop((w * 30 // 100, h * 2 // 100, w * 85 // 100, h * 10 // 100)),
  16. ]
  17. def check(path, baseline):
  18. image = Image.open(path).convert("RGB")
  19. parts = strips(image)
  20. stats = [ImageStat.Stat(part.convert("L")) for part in parts]
  21. mean = sum(stat.mean[0] for stat in stats) / len(stats)
  22. stddev = max(stat.stddev[0] for stat in stats)
  23. if mean < 1 and stddev < 1:
  24. return False, "black"
  25. if baseline is not None:
  26. base_parts = strips(baseline.resize(image.size))
  27. diffs = [ImageStat.Stat(ImageChops.difference(a, b).convert("L")).mean[0] for a, b in zip(parts, base_parts)]
  28. diff = sum(diffs) / len(diffs)
  29. if diff < 6:
  30. return False, f"same as the desktop without a wallpaper (difference {diff:.1f})"
  31. return True, f"mean {mean:.0f}, difference from the baseline {diff:.0f}"
  32. return True, f"mean {mean:.0f}, stddev {stddev:.0f}"
  33. def differ(a, b):
  34. """Two frames of the same desktop must differ clearly (a hotswap changed the picture)."""
  35. first, second = Image.open(a).convert("RGB"), Image.open(b).convert("RGB").resize(Image.open(a).size)
  36. parts = list(zip(strips(first), strips(second)))
  37. diff = sum(ImageStat.Stat(ImageChops.difference(x, y).convert("L")).mean[0] for x, y in parts) / len(parts)
  38. ok = diff >= 6
  39. print(f"{'PASS' if ok else 'FAIL'} {b}: {'differs from' if ok else 'same as'} {a} (difference {diff:.1f})")
  40. sys.exit(0 if ok else 1)
  41. def main():
  42. args = sys.argv[1:]
  43. if args[:1] == ["--differ"]:
  44. differ(args[1], args[2])
  45. baseline = None
  46. if args[:1] == ["--baseline"]:
  47. baseline = Image.open(args[1]).convert("RGB")
  48. args = args[2:]
  49. failed = False
  50. for path in args:
  51. ok, why = check(path, baseline)
  52. failed |= not ok
  53. print(f"{'PASS' if ok else 'FAIL'} {path}: {why}")
  54. sys.exit(1 if failed else 0)
  55. main()