1.tesserocr use
# From the document identification image characters the In [. 7]: tesserocr.file_to_text ( ' image.png ' ) Out [ . 7]: ' Python3WebSpider \ n-\ n- ' # view tesseract installed language pack the In [. 8 ]: tesserocr.get_languages ( ) Out [ . 8]: ( ' / usr / Share / Tesseract / tessdata / ' , [ ' ENG ' ]) # from the picture data identifying glyphs the In [. 9 ]: tesserocr.image_to_text (IM) Out [ . 9]: ' Python3WebSpider \ n-\ n- ' # version information the In [10 ]: tesserocr.tesseract_version () Out [ 10]:'tesseract 3.04.00\n leptonica-1.72\n libgif 4.1.6(?) : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0\n'
2.pytesseract use
Features:
- get_tesseract_version return Tesseract version installed on your system.
- image_to_string the Tesseract OCR results on the running string returned to the image
- image_to_boxes returns a result of the recognized character and frame boundary
- image_to_data returns a block boundary, and other information about the results of confidence. Need Tesseract 3.05+. For more information, please see the Tesseract TSV document
- image_to_osd return results that contain information about the direction and script detection.
parameter:
image_to_data(image, lang=None, config='', nice=0, output_type=Output.STRING)
- image object image object
- lang String, Tesseract language code string
- config String to string any other configuration, for example:
config='--psm 6'
- nice Integer modify processor priority Tesseract run. Windows does not support. Nice adjust the advantages similar to the unix process.
- output_type class attribute specifies the type of the output, by default
string
. For a complete list of all supported types, check pytesseract.Output class definition.
from the PIL Import Image Import pytesseract # If PATH tesseract not executable, specify the path tesseract pytesseract.pytesseract.tesseract_cmd = ' C: \ Program Files (the x86) \ \\ tesseract.exe the OCR-Tesseract ' # print image recognition string Print (pytesseract.image_to_string (Image.open ( ' test.png ' ))) # specified language identification character string image, eng English Print (pytesseract.image_to_string (Image.open ( ' Test-european.jpg ' ) , lang = ' ENG ' )) # Get bounding box Print (pytesseract.image_to_boxes (Image.open (' Test.png ' ))) # Gets the bounding box, confidence, and page detailed data line Print (pytesseract.image_to_data (Image.open ( ' test.png ' ))) # fetch direction and script detects Print (pytesseract .image_to_osd (Image.open ( ' test.png ' ))
Simple image recognition application
Usually authentication image processing, the image required by gradation processing, increasing recognizable image binarized text, the following is a simple image recognition processing codes, codes in case of an image such as an intermediate complex with multiple points scribe lines of the same size code word needs to be Jo cutting and other operations, but its recognition of only about 30 percent, so you have another think of another way to bypass validation
from the PIL Import Image Import pytesseract IM = Image.open ( ' 66.png ' ) # binarized image and the threshold image passed DEF erzhihua (Image, threshold): '' ' : Image type: Image.Image ' '' Image image.convert = ( ' L ' ) Table = [] for I in Range (256 ): IF I < threshold: table.append (0) the else : table.append ( . 1 ) return image.point(table,'1') image=erzhihua(im,127) image.show() result=pytesseract.image_to_string(image,lang='eng') print(result)
Analog automatic login identification codes:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 8:58 # @Author : Py.qi # @File : login.py # @Software: PyCharm from selenium import webdriver from selenium.common.exceptions import TimeoutException,WebDriverException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.remote.webelement import WebElement from io import BytesIO from PIL import Image import pytesseract import time user='zhang' password='123' url='http://10.0.0.200' driver=webdriver.Chrome() wait=WebDriverWait(driver,10) #识别验证码 def acker(content): im_erzhihua=erzhihua(content,127) result=pytesseract.image_to_string(im_erzhihua,lang='eng') return result #验证码二值化 def erzhihua(image,threshold): ''':type image:Image.Image''' image=image.convert('L') table=[] for i in range(256): if i < threshold: table.append(0) else: table.append ( . 1 ) return image.point (Table, ' . 1 ' ) # automatic landing DEF Login (): the try : driver.get (URL) # get user input box input = wait.until (EC.presence_of_element_located ((By .CSS_SELECTOR, ' #loginname ' ))) # type: WebElement input.clear () # sends the user name input.send_keys (user) # obtain the password box inpass = wait.until (EC.presence_of_element_located ((By.CSS_SELECTOR, ' # password '))) # Type: WebElement inpass.clear () # password is sent inpass.send_keys (password) # get a verification input box yanzheng = wait.until (EC.presence_of_element_located ((By.CSS_SELECTOR, ' #code ' ))) # type : WebElement # Get codes positions on the canvas codeimg = wait.until (EC.presence_of_element_located ((By.CSS_SELECTOR, ' #codeImg ' ))) # type: WebElement IMAGE_LOCATION = codeimg.location # intercept page image and mask taken code area image image = driver.get_screenshot_as_png () IM =Image.open(BytesIO(image)) imag_code=im.crop((image_location['x'],image_location['y'],488,473)) #输入验证码并登陆 yanzheng.clear() yanzheng.send_keys(acker(imag_code)) time.sleep(2) yanzheng.send_keys(Keys.ENTER) except TimeoutException as e: print('timeout:',e) except WebDriverException as e: print('webdriver error:',e) if __name__ == '__main__': login()
Original: https://www.cnblogs.com/-qing-/p/11027821.html