check_frame.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. check_frame.py --similar a.png b.png [max] - are two frames of the same wallpaper alike (a hotswap must look like a fresh start)?
  4. check_frame.py --crop from,to [--baseline base.png] shot.png - only the part of the width between the two fractions (one
  5. monitor of a multi monitor screenshot)
  6. Without --baseline a screenshot only fails when it is pure black. With the baseline (the desktop's own screenshot taken
  7. before the engine started) it must also differ from it along the right edge and the top, where windows, panels and
  8. desktop icons rarely are, so a desktop that still shows its default background fails. Prints one "PASS|FAIL <file>: <why>"
  9. line per file, exit code 1 when any failed. Reference frames per wallpaper (upside down, wrong crop) would be the next
  10. step, see RELEASE_TESTING.md.
  11. """
  12. import sys
  13. from PIL import Image, ImageChops, ImageStat
  14. def strips(image):
  15. w, h = image.size
  16. return [
  17. image.crop((w * 88 // 100, h * 15 // 100, w * 99 // 100, h * 80 // 100)),
  18. image.crop((w * 30 // 100, h * 2 // 100, w * 85 // 100, h * 10 // 100)),
  19. ]
  20. CROP = None
  21. def crop(image):
  22. if CROP is None:
  23. return image
  24. w, h = image.size
  25. return image.crop((int(w * CROP[0]), 0, int(w * CROP[1]), h))
  26. def check(path, baseline):
  27. image = crop(Image.open(path).convert("RGB"))
  28. parts = strips(image)
  29. stats = [ImageStat.Stat(part.convert("L")) for part in parts]
  30. mean = sum(stat.mean[0] for stat in stats) / len(stats)
  31. stddev = max(stat.stddev[0] for stat in stats)
  32. if mean < 1 and stddev < 1:
  33. return False, "black"
  34. if baseline is not None:
  35. base_parts = strips(crop(baseline).resize(image.size))
  36. diffs = [ImageStat.Stat(ImageChops.difference(a, b).convert("L")).mean[0] for a, b in zip(parts, base_parts)]
  37. diff = sum(diffs) / len(diffs)
  38. # a mostly black wallpaper has a small mean difference but still some lit pixels where a bare desktop has none
  39. histograms = [ImageChops.difference(a, b).convert("L").histogram() for a, b in zip(parts, base_parts)]
  40. lit = sum(sum(h[9:]) for h in histograms) / sum(sum(h) for h in histograms)
  41. if diff < 6 and lit < 0.003:
  42. return False, f"same as the desktop without a wallpaper (difference {diff:.1f})"
  43. return True, f"mean {mean:.0f}, difference from the baseline {diff:.0f}"
  44. return True, f"mean {mean:.0f}, stddev {stddev:.0f}"
  45. def differ(a, b):
  46. """Two frames of the same desktop must differ clearly (a hotswap changed the picture)."""
  47. first, second = Image.open(a).convert("RGB"), Image.open(b).convert("RGB").resize(Image.open(a).size)
  48. parts = list(zip(strips(first), strips(second)))
  49. diff = sum(ImageStat.Stat(ImageChops.difference(x, y).convert("L")).mean[0] for x, y in parts) / len(parts)
  50. ok = diff >= 6
  51. print(f"{'PASS' if ok else 'FAIL'} {b}: {'differs from' if ok else 'same as'} {a} (difference {diff:.1f})")
  52. sys.exit(0 if ok else 1)
  53. def similar(a, b, limit):
  54. size = (96, 54)
  55. first, second = Image.open(a).convert("RGB").resize(size), Image.open(b).convert("RGB").resize(size)
  56. diff = ImageStat.Stat(ImageChops.difference(first, second).convert("L")).mean[0]
  57. ok = diff <= limit
  58. print(f"{'PASS' if ok else 'FAIL'} {b}: {'like' if ok else 'not like'} {a} (difference {diff:.1f}, limit {limit:g})")
  59. sys.exit(0 if ok else 1)
  60. def main():
  61. global CROP
  62. args = sys.argv[1:]
  63. if args[:1] == ["--similar"]:
  64. similar(args[1], args[2], float(args[3]) if len(args) > 3 else 10)
  65. if args[:1] == ["--crop"]:
  66. CROP = [float(x) for x in args[1].split(",")]
  67. args = args[2:]
  68. if args[:1] == ["--differ"]:
  69. differ(args[1], args[2])
  70. baseline = None
  71. if args[:1] == ["--baseline"]:
  72. baseline = Image.open(args[1]).convert("RGB")
  73. args = args[2:]
  74. failed = False
  75. for path in args:
  76. ok, why = check(path, baseline)
  77. failed |= not ok
  78. print(f"{'PASS' if ok else 'FAIL'} {path}: {why}")
  79. sys.exit(1 if failed else 0)
  80. main()