导入base64
def img_base64(img_path): 如果 os.path.exists(img_path): 将 open(img_path, 'rb') 作为 f_obj: image_data = f_obj.read() base64_d = base64.b64encode(image_data) 返回base64_d
master.save_screenshot(".\screenshots est.png")
full_screenshot_base64 = img_base64(".\screenshots est.png") partial_screenshot_base64 = img_base64(". esource\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)
当我运行上面的Python代码时,错误消息是: raise TypeError(f'类型为 {o.class.name} 的对象' 类型错误:字节类型的对象不可 JSON 序列化
我该如何解决这个问题?请帮助我,谢谢!
根据方法说明,参数的类型需要是bytes,但是提示是:Object of type bytes is not JSON Serialabilize
如果您从未最终解决这个问题,我遇到了类似的问题,并且不幸的是,有关实现的文档还有一些不足之处。
对我来说,诀窍是使用
.decode("ascii")
。
此外,不要忘记使用 --use-plugins=images
参数启动 Appium 服务器(无论是手动还是编程)。
作为示例,以下是我的实现方式(代码不完整):
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)
希望这对(您或将来的任何其他人)有帮助!