I am trying to create a PIL Image object based on a canvas
tag extracted with Selenium from this website. The goal is to use pytesseract
and get the captcha content. My code doesn't raise any errors, but the image created is all black.
My code so far:
# 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)
I don't know why the image is all black. My code is based on this tutorial, which saves the image. I dont want to save the image, I want it to be in memory only.
Edit:
I have saved the image in filesystem and the image is saving ok, but with transparent background, thus appearing black when showed. How can I make the background white?