Skip to content

Commit b471a05

Browse files
fix: Adjust images payload for image comparison API (#1170)
1 parent dcdc1dd commit b471a05

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

appium/webdriver/extensions/images_comparison.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
from ..mobilecommand import MobileCommand as Command
2020

21+
Base64Payload = Union[str, bytes]
22+
2123

2224
class ImagesComparison(CanExecuteCommands):
23-
def match_images_features(self, base64_image1: bytes, base64_image2: bytes, **opts: Any) -> Dict[str, Any]:
25+
def match_images_features(self, base64_image1: Base64Payload, base64_image2: Base64Payload, **opts: Any) -> Dict[str, Any]:
2426
"""Performs images matching by features.
2527
2628
Read
@@ -66,11 +68,16 @@ def match_images_features(self, base64_image1: bytes, base64_image2: bytes, **op
6668
rect2 (dict): The bounding rect for the `points2` array or a zero rect if not enough matching points
6769
were found. The rect is represented by a dictionary with 'x', 'y', 'width' and 'height' keys
6870
"""
69-
options = {'mode': 'matchFeatures', 'firstImage': base64_image1, 'secondImage': base64_image2, 'options': opts}
71+
options = {
72+
'mode': 'matchFeatures',
73+
'firstImage': _adjust_image_payload(base64_image1),
74+
'secondImage': _adjust_image_payload(base64_image2),
75+
'options': opts,
76+
}
7077
return self.execute(Command.COMPARE_IMAGES, options)['value']
7178

7279
def find_image_occurrence(
73-
self, base64_full_image: bytes, base64_partial_image: bytes, **opts: Any
80+
self, base64_full_image: Base64Payload, base64_partial_image: Base64Payload, **opts: Any
7481
) -> Dict[str, Union[bytes, Dict]]:
7582
"""Performs images matching by template to find possible occurrence of the partial image
7683
in the full image.
@@ -97,13 +104,15 @@ def find_image_occurrence(
97104
"""
98105
options = {
99106
'mode': 'matchTemplate',
100-
'firstImage': base64_full_image,
101-
'secondImage': base64_partial_image,
107+
'firstImage': _adjust_image_payload(base64_full_image),
108+
'secondImage': _adjust_image_payload(base64_partial_image),
102109
'options': opts,
103110
}
104111
return self.execute(Command.COMPARE_IMAGES, options)['value']
105112

106-
def get_images_similarity(self, base64_image1: bytes, base64_image2: bytes, **opts: Any) -> Dict[str, Union[bytes, Dict]]:
113+
def get_images_similarity(
114+
self, base64_image1: Base64Payload, base64_image2: Base64Payload, **opts: Any
115+
) -> Dict[str, Union[bytes, Dict]]:
107116
"""Performs images matching to calculate the similarity score between them.
108117
109118
The flow there is similar to the one used in
@@ -125,8 +134,17 @@ def get_images_similarity(self, base64_image1: bytes, base64_image2: bytes, **op
125134
score (float): The similarity score as a float number in range [0.0, 1.0].
126135
1.0 is the highest score (means both images are totally equal).
127136
"""
128-
options = {'mode': 'getSimilarity', 'firstImage': base64_image1, 'secondImage': base64_image2, 'options': opts}
137+
options = {
138+
'mode': 'getSimilarity',
139+
'firstImage': _adjust_image_payload(base64_image1),
140+
'secondImage': _adjust_image_payload(base64_image2),
141+
'options': opts,
142+
}
129143
return self.execute(Command.COMPARE_IMAGES, options)['value']
130144

131145
def _add_commands(self) -> None:
132146
self.command_executor.add_command(Command.COMPARE_IMAGES, 'POST', '/session/$sessionId/appium/compare_images')
147+
148+
149+
def _adjust_image_payload(payload: Base64Payload) -> str:
150+
return payload if isinstance(payload, str) else payload.decode('utf-8')

0 commit comments

Comments
 (0)