OCR image recognition can often use the TESSEROCR module to recognize the contents of the picture and convert it to text and output
TESSEROCR is an OCR recognition library for Python, a layer of Python apt encapsulation for tesseract. Before installing the TESSEROCR, you need to install the Tesseract
Tessrtact file:
https://digi.bib.uni-mannheim.de/tesseract/
Python installation tessocr: Download the corresponding. WHL file installation (this package PIP way error prone)
Tesseract with the corresponding TESSEROCR:
Https://github.com/simonflueckiger/tesserocr-windows_build/releases
The implementation code is as follows:
From PIL import Image
Import TESSEROCR
#tesserocr识别图片的2种方法
img = Image.open ("code.jpg")
Verify_code1 = Tesserocr.image_to_text (IMG)
#print (VERIFY_CODE1)
Verify_code2 = Tesserocr.file_to_text ("code.jpg")
#print (Verify_code2)
However, when the image of more interference, such as the verification code in excess of line interference image recognition, the content is not very accurate, you need to do a bit of processing, such as turning grayscale, binary operation.
You can use the CONVERT () method of the image object to pass in "L", convert the image to grayscale, and pass in 1 for binary processing of the image (default threshold 127)
Original Verification Code:
img = Image.open ("code.jpg")
img_l = Img.convert ("L")
Img_l.show ()
You can also set your own threshold value
Threshold = #设置二值的阈值100
Table = []
For I in range (256):
If I < threshold:
Table.append (0)
Else
Table.append (1)
#point () Returns a copy of the image pixel value for the given lookup table, the variable table sets 256 values for each channel of the image, assigns a new pattern to the output image, and the image of the "L" and "P" is further converted to an image with the pattern "1"
Image = Img_l.point (table, "1")
Image.show ()
img_1 = Tesserocr.image_to_text (image)
Print (img_1)
>>5sa6
[Python] [crawler] Using OCR technology to identify graphics verification code