【发布时间】:2016-06-22 00:45:31
【问题描述】:
我正在尝试基于使用 Selenium 从 this 网站提取的 canvas 标签创建 PIL Image 对象。目标是使用pytesseract 并获取验证码内容。我的代码没有引发任何错误,但创建的图像全是黑色的。
到目前为止我的代码:
# Run JS code to get data URI
png_url = driver.execute_script(
'return document.getElementsByTagName("canvas")[0].toDataURL("image/png");')
# Parse the URI to get only the base64 part
str_base64 = re.search(r'base64,(.*)', png_url).group(1)
# Convert it to binary
str_decoded = str_base64.decode('base64')
# Create and show Image object
image = Image.open(StringIO(str_decoded))
image.show()
# Read image with pytesseract
recaptcha = pytesseract.image_to_string(image)
我不知道为什么图像全黑。我的代码基于this 教程,它保存了图像。我不想保存图像,我希望它只在内存中。
编辑:
我已将图像保存在文件系统中,图像保存正常,但背景透明,因此显示时显示为黑色。如何让背景变白?
【问题讨论】:
标签: python selenium selenium-webdriver python-imaging-library pillow