quick_check.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sys, os, base64, io
  2. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  3. from PIL import Image
  4. from stitch_core import stitch_images
  5. def make(color, w=100, h=80):
  6. img = Image.new("RGB", (w, h), color)
  7. buf = io.BytesIO()
  8. img.save(buf, "PNG")
  9. return base64.b64encode(buf.getvalue()).decode()
  10. # horizontal
  11. res = stitch_images([make("red", 100, 80), make("blue", 120, 80)], direction="horizontal")
  12. assert res["width"] == 220 and res["height"] == 80, f"horizontal failed: {res}"
  13. print("horizontal OK:", res["width"], "x", res["height"])
  14. # vertical with spacing
  15. res = stitch_images([make("green", 100, 80), make("yellow", 100, 60)], direction="vertical", spacing=10)
  16. assert res["width"] == 100 and res["height"] == 150, f"vertical failed: {res}"
  17. print("vertical OK:", res["width"], "x", res["height"])
  18. # grid 2x2
  19. imgs = [make(c, 50, 50) for c in ["red", "green", "blue", "yellow"]]
  20. res = stitch_images(imgs, direction="grid", columns=2, spacing=5)
  21. assert res["width"] == 105 and res["height"] == 105, f"grid failed: {res}"
  22. print("grid OK:", res["width"], "x", res["height"])
  23. # fit_width
  24. res = stitch_images([make("cyan", 100, 80), make("magenta", 60, 40)], direction="horizontal", resize_mode="fit_width")
  25. assert res["width"] == 200, f"fit_width failed: {res}"
  26. print("fit_width OK:", res["width"], "x", res["height"])
  27. # error on < 2 images
  28. try:
  29. stitch_images([make("red")], direction="horizontal")
  30. raise AssertionError("should have raised ValueError")
  31. except ValueError:
  32. print("error-check OK")
  33. print("ALL TESTS PASSED")