Python downgrade with Pytesseract to identify a website verification code

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/zhongtang/p/5560361.html

First, Pytesseract introduction 1, pytesseract description

Pytesseract Latest Version 0.1.6, address: https://pypi.python.org/pypi/pytesseract

Python-tesseract is a wrapper for Google S TESSERACT-OCR
(http://code.google.com/p/tesseract-ocr/). It is also useful as a
Stand-alone invocation script to Tesseract, as it can read all image types
Supported by the Python Imaging Library, including JPEG, PNG, GIF, BMP, TIFF,
and others, whereas TESSERACT-OCR by default is only supports TIFF and BMP.
Additionally, if used as a script, Python-tesseract would print the recognized
Text in stead of writing it to a file. Support for confidence estimates and
Bounding box data is planned for the future releases.

Translate the main idea to the effect:

A, python-tesseract is a separate package based on Google's TESSERACT-OCR;

B, Python-tesseract function is to recognize the text in the picture file, and return the recognition result as a parameter;

C, python-tesseract default support TIFF, BMP format picture, only after installing PIL, can support JPEG, GIF, PNG and other image formats;

2, Pytesseract installation

Installation:

Prerequisites:
* Python-tesseract requires Python 2.5 or later or Python 3.
* You'll need the Python Imaging Library (PIL). Under Debian/ubuntu, this is
The package ' python-imaging ' or ' python3-imaging ' for Python3.
* Install Google TESSERACT-OCR from http://code.google.com/p/tesseract-ocr/.
You must is able to invoke the Tesseract command as "Tesseract". If this
Isn ' t the case, for example because tesseract isn ' t in your PATH, you'll
The "tesseract_cmd" variable at the top of ' tesseract.py '.
Under Debian/ubuntu You can use the package "TESSERACT-OCR".

Installing via PIP:
See the [Pytesseract Package page] (https://pypi.python.org/pypi/pytesseract)
```
$> sudo pip install pytesseract

Translation:

A, python-tesseract support python2.5 and later;

B, python-tesseract need to install PIL (Python Imaging Library), to support more image formats;

C, python-tesseract need to install TESSERACT-OCR installation package, specific see a blog post.

In summary, the principle of pytesseract:

1. As mentioned in the previous blog post, the command line Tesseract.exe 1.png output-l Eng can be used to identify 1.png characters and output the recognition results to output.txt.

2, pytesseract to the above process two times encapsulation, automatically call Tesseract.exe, and read the contents of the Output.txt file, as the return value of the function to return.

Second, the use of pytesseract

USAGE:
```
> Try:
> Import Image
> except Importerror:
> From PIL import Image
> Import pytesseract
> Print (pytesseract.image_to_string (Image.open (' test.png ')))
> Print (pytesseract.image_to_string (Image.open (' test-european.jpg '), lang= ' fra '))

can see:

1. The core code is the Image_to_string function, which also supports the-L eng parameter, which supports the-PSM parameter.

Usage:
Image_to_string (Image.open (' test.png '), lang= "eng" config= "-PSM 7")

2, Pytesseract call the image, so only need to PIL, in fact, Tesseract.exe itself is to support JPEG, PNG and other image formats.

Instance code, identify a public website verification code (we must not do bad things, think again and again, the end of the Web site domain name, everyone to find another site to try it ...) :

View Code

Third, Pytesseract code optimization

When the above program runs on the Windows platform, it is not very friendly to see the Black Console window flashing through the screen.

Slightly modified the pytesseract.py (C:\Python27\Lib\site-packages\pytesseract directory), the above process is hidden.

# Modified by Zhongtang Hide Console window
# new Code
Is_win32 = ' Win32 ' in STR (sys.platform). Lower ()
If Is_win32:
Startupinfo = subprocess. Startupinfo ()
Startupinfo.dwflags |= subprocess. Startf_useshowwindow
Startupinfo.wshowwindow = Subprocess.sw_hide
proc = subprocess. Popen (Command,
Stderr=subprocess. Pipe,startupinfo=startupinfo)
‘‘‘
# old Code
proc = subprocess. Popen (Command,
Stderr=subprocess. PIPE)
‘‘‘
# Modified End

For the convenience of beginners, the pytesseract.py also posted out, the master himself ignored.

View Code

Above......

Python downgrade with Pytesseract to identify a website verification code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.