check_frame.py 2.1 KB

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