代码在运行重采样图像时出现问题

时间:2019-01-07 21:37:07

标签: python selenium

当我仅使用它来解决我使用的验证码时,此代码运行良好,但实施后无法正常工作。 收到零退出状态错误。不确定这是否有帮助,但是它可以在Ubuntu上运行。

代码:

def check_captcha():
    while True:
        try:
            image = driver.find_element_by_xpath('//[@id="image_captcha"]/img')
        except:
            break
        else:
            print('There is a captcha')
            src = image.get_attribute('src')
            urllib.urlretrieve(src, "captcha.jpg")
            solve_captcha()


# Captcha Solving
def solve_captcha():
    path = 'captcha.jpg'
    try:
        import Image
    except ImportError:
        from PIL import Image
    from subprocess import check_output
    check_output(['convert', path, '-resample', '600', path])
    captcha_output =  pytesseract.image_to_string(Image.open(path))
    input_captcha_text(captcha_output)

    if os.path.exists('captcha.jpg'):
        os.remove('captcha.jpg')
    else:
        print("The file does not exist")

错误:

convert: no decode delegate for this image format `JPG' @ 
error/constitute.c/ReadImage/556.
convert: no images defined `captcha.jpg' @ 
error/convert.c/ConvertImageCommand/3300.
Traceback (most recent call last):
  File "bot.py", line 275, in <module>
    order()
  File "bot.py", line 32, in order
    runBotOnPage(giveawayNum)
  File "bot.py", line 60, in runBotOnPage
    check_title(title, giveawayNum, giveawayType)
  File "bot.py", line 101, in check_title
    clickListing(giveawayNum, giveawayType)
  File "bot.py", line 124, in clickListing
    check_captcha()
  File "bot.py", line 208, in check_captcha
    solve_captcha()
  File "bot.py", line 219, in solve_captcha
    check_output(['convert', path, '-resample', '600', path])
  File "/usr/lib/python2.7/subprocess.py", line 219, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['convert', 'captcha.jpg', 
'-resample', '600', 'captcha.jpg']' returned non-zero exit status 1

1 个答案:

答案 0 :(得分:0)

p=subprocess.Popen(['tesseract','captcha.jpg',"captcha"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p.wait()
f=open('captcha.txt','r')
captchaResponce=f.read().replace(" ","").replace("\n","")

这将在其中创建一个文本文件,即您的图像文本(在tesseract读取后),captchaResponce是您的验证码(如果您以后要使用它的话)