我想创建一个基于canvas
标签的PIL图像对象,该标签用this网站的Selenium提取。目标是使用pytesseract
并获取验证码内容。我的代码不会产生任何错误,但创建的图像全是黑色的。从Selenium的画布标签创建PIL图像对象
我迄今为止代码:
# 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教程,它保存了图像。我不想保存图像,我希望它只在内存中。
编辑:
我已经在文件系统中保存的图像和图像保存好,但与透明的背景下,表现出这样的时候出现黑色。我怎样才能使背景变白?