import base64
def img_base64(img_path):
if os.path.exists(img_path):
with open(img_path, 'rb') as f_obj:
image_data = f_obj.read()
base64_d = base64.b64encode(image_data)
return base64_d
master.save_screenshot(".\\screenshots\\test.png")
full_screenshot_base64 = img_base64(".\\screenshots\\test.png")
partial_screenshot_base64 = img_base64(".\\resource\\images\\suit_head.png")
master.update_settings({"getMatchedImageResult": True})
master.update_settings({"fixImageTemplateScale": True})
master.find_image_occurrence(base64_full_image=full_screenshot_base64, base64_partial_image=partial_screenshot_base64, visuallze=True)
When I run above python code, error message is:
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
How should I solve this problem? Please help me, thank you!
According to the method description, the type of the parameter needs to be bytes, but the prompt is: Object of type bytes is not JSON
serializable.
In case you never ended up solving this, I encountered a similar problem and the documentation around the implementation unfortunately leaves a bit to be desired.
The trick for me was using .decode("ascii")
.
Additionally, don't forget to start your Appium server (whether manually or programmatically) with the --use-plugins=images
argument.
As an example, here is how I implemented it (the code isn't complete):
def load_image_base64(path: str):
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode("ascii")
def compare_images(driver):
reference = load_image_base64("./reference_images/menu_icon.png")
target = load_image_base64("./runtime/dashboard_screenshot.png")
driver.find_image_occurrence(base64_partial_image=target, base64_full_image=reference)
Hope this helps (you or anyone else in the future)!