skip_test_image.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding: utf-8
  2. #
  3. import os
  4. import cv2
  5. import numpy as np
  6. import pytest
  7. from PIL import Image
  8. import uiautomator2.image as u2image
  9. TESTDIR = os.path.dirname(os.path.abspath(__file__)) + "/testdata" # if set to DIR__, pytest will fail with TypeError: 'str' object is not callable
  10. @pytest.fixture
  11. def path_ae86():
  12. filepath = os.path.join(TESTDIR, "./AE86.jpg")
  13. return filepath
  14. @pytest.fixture
  15. def im_ae86(path_ae86: str) -> np.ndarray:
  16. """ 使用opencv打开的图片 """
  17. im = cv2.imread(path_ae86)
  18. return im
  19. def test_imread(im_ae86, path_ae86):
  20. # Path
  21. im = u2image.imread(path_ae86)
  22. assert im.shape == (193, 321, 3)
  23. # URL
  24. im = u2image.imread("https://www.baidu.com/img/bd_logo1.png")
  25. assert im.shape == (258, 540, 3)
  26. # Opencv
  27. im = u2image.imread(im_ae86)
  28. assert im.shape == (193, 321, 3), "图片格式变化"
  29. # PIL.Image
  30. pilim = Image.open(path_ae86)
  31. im = u2image.imread(pilim)
  32. assert pilim.size == (321, 193)
  33. assert im.shape == (193, 321, 3), "图片格式变化"
  34. @pytest.mark.skip("missing test images")
  35. def test_image_match():
  36. class MockDevice():
  37. def __init__(self):
  38. self.x = None
  39. self.y = None
  40. def click(self, x, y):
  41. self.x = x
  42. self.y = y
  43. def screenshot(self, *args, **kwargs):
  44. return cv2.imread(TESTDIR + "/screenshot.jpg")
  45. d = MockDevice()
  46. ix = u2image.ImageX(d)
  47. template = Image.open(TESTDIR + "/template.jpg")
  48. res = ix.match(template)
  49. x, y = res['point']
  50. assert (x, y) == (409, 659), "Match position is wrong"
  51. ix.click(template)
  52. assert d.x == 409
  53. assert d.y == 659
  54. if False: # show position
  55. pim = Image.open(TESTDIR+"/screenshot.jpg")
  56. nim = u2image.draw_point(pim, x, y)
  57. nim.show()