首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Selenium从画布标签创建PIL图像对象时出现问题

使用Selenium从画布标签创建PIL图像对象时出现问题
EN

Stack Overflow用户
提问于 2016-06-22 00:18:50
回答 2查看 537关注 0票数 0

我正在尝试创建一个PIL图像对象基于从this网站上使用Selenium提取的canvas标签。目标是使用pytesseract并获取验证码内容。我的代码没有引发任何错误,但是创建的图像全是黑色的。

到目前为止我的代码如下:

代码语言:javascript
复制
# 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教程的,它保存了图像。我不想保存图像,我只想把它保存在内存中。

编辑:

我已将图像保存在文件系统中,图像保存正常,但背景透明,因此在显示时显示为黑色。怎样才能把背景变成白色呢?

EN

回答 2

Stack Overflow用户

发布于 2016-06-22 00:36:23

我所需要做的就是提取下面的this答案的背景:

代码语言:javascript
复制
def remove_transparency(im, bg_colour=(255, 255, 255)):

    # Only process if image has transparency (https://stackoverflow.com/a/1963146)
    if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info):

        # Need to convert to RGBA if LA format due to a bug in PIL (https://stackoverflow.com/a/1963146)
        alpha = im.convert('RGBA').split()[-1]

        # Create a new background image of our matt color.
        # Must be RGBA because paste requires both images have the same format
        # (https://stackoverflow.com/a/8720632  and  https://stackoverflow.com/a/9459208)
        bg = Image.new("RGBA", im.size, bg_colour + (255,))
        bg.paste(im, mask=alpha)
        return bg

    else:
        return im

然后是完整的代码:

代码语言:javascript
复制
png_url = driver.execute_script(
            'return document.getElementsByTagName("canvas")[0].toDataURL("image/png");')
str_base64 = re.search(r'base64,(.*)', png_url).group(1)
# Convert it to binary
str_decoded = str_base64.decode('base64')
image = Image.open(StringIO(str_decoded))
image = remove_transparency(image)
recaptcha = pytesseract.image_to_string(image).replace(" ", "")
票数 1
EN

Stack Overflow用户

发布于 2016-06-22 00:45:32

您应该创建一个RGB白色图像,并将您的RGBA图像粘贴到其中。解决方案可能是this,但也有其他方法。我推荐numpy和opencv。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37949643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档