pytesseract——驗證碼的識別——PIL庫的介紹


1、簡介

Python-tesseract是一款用於光學字符識別(OCR)的python工具,即從圖片中識別出其中嵌入的文字。Python-tesseract是對Google Tesseract-OCR的一層封裝。它也同時可以單獨作為對tesseract引擎的調用腳本,支持使用PIL庫(Python Imaging Library)讀取的各種圖片文件類型,包括jpeg、png、gif、bmp、tiff和其他格式,。作為腳本使用它將打印出識別出的文字而非寫入到文件。所以安裝pytesseract前要先安裝PIL和tesseract-orc這倆依賴庫

2、安裝

PIL 下載地址:https://github.com/lightkeeper/lswindows-lib/blob/master/amd64/python/PIL-1.1.7.win-amd64-py2.7.exe?raw=true

tesseract-ocr下載地址:https://sourceforge.net/projects/tesseract-ocr-alt/files/

然后在cmd 中安裝pytesseract

cd C:\Python27\Scripts
pip install pytesseract

 3.用pytesseract識別驗證碼

 1 try:
 2     import requests
 3     import pytesseract
 4     from PIL import Image
 5 except ImportError:
 6     raise SystemExit('cuole');
 7 addr = raw_input()
 8 image = Image.open(addr)
 9 vcode = pytesseract.image_to_string(image)
10 print vcode

4.PIL的Image模塊

本文是節選自 PIL handbook online 並做了一些簡單的翻譯

只能保證自己看懂,不保證翻譯質量。歡迎各位給出意見。

------------------------------------------------------

    Image 模塊提供了一個同名類(Image),也提供了一些工廠函數,包括從文件中載入圖片和創建新圖片。例如,以下的腳本先載入一幅圖片,將它旋轉 45 度角,並顯示出來:

1 >>>from PIL import Image
2  >>>im = Image.open("j.jpg")
3  >>>im.rotate(45).show()

    下面這個腳本則創建了當前目錄下所有以 .jpg 結尾的圖片的縮略圖。

from PIL import Image
import glob, os

size = 128, 128
for infile in glob.glob("*.jpg"):
   file, ext = os.path.splitext(infile)
   im = Image.open(infile)
   im.thumbnail(size, Image.ANTIALIAS)
   im.save(file + ".thumbnail", "JPEG")

 

    Image 類中的函數

0.    new : 這個函數創建一幅給定模式(mode)和尺寸(size)的圖片。如果省略 color 參數,則創建的圖片被黑色填充滿,如果 color 參數是 None 值,則圖片還沒初始化。

1 Image.new( mode, size ) => image
2 Image.new( mode, size, color ) => image

1.    open : 打開並識別所提供的圖像文件。不過,使用這函數的時候,真正的圖像數據在你進行數據處理之前並沒有被讀取出來。可使用 load 函數進行強制加載。 mode 參數可以省略,但它只能是 "r" 值。

1 Image.open( infile ) => image
2 Image.open( infile, mode ) => image

2.    blend : 使用兩幅給出的圖片和一個常量 alpha 創建新的圖片。兩幅圖片必須是同樣的 size 和 mode 。

1  Image.blend( image1, image2, alpha ) => image
2  # 結果 與 運算過程
3  # out = image1 * ( 1.0 - alpha ) + image2 * alpha

3.    composite : 使用兩幅給出的圖片和一個與 alpha 參數相似用法的 mask 參數,其值可為:"1", "L", "RGBA" 。兩幅圖片的 size 必須相同。

Image.composite( image1, image2, mask ) => image

4.    eval : 使用帶一個參數的函數作用於給定圖片的每一個像素。如果給定的圖片有超過一個的 頻段(band),則該函數也會作用於每一個頻段。注意,該函數是每一個像素計算一次,所以不能使用一些隨機組件或其他的生成器。

1 Image.eval( image, function ) => image

5.    frombuffer : (PIL 1.1.4 中新添加的)使用標准 "raw" 解碼器在像素數據或是對象緩存中創建一個圖像副本。不是所有的模式都支持這種用法。支持的 mode 有"L", "RGBX", "RGBA", "CMYK"。

1 Image.frombuffer( mode, size, data ) => image

6.    fromstring : 注意,這個函數只對像素數據進行解碼,而不是一整張圖片。如果你有一整張字符串格式的圖片,使用 StringIO 對其進行包裝並用 open 函數載入它。

1  # 使用字符串類型的像素數據和標准解碼器 "raw" 來創建圖像
2  Image.fromstring( mode, size, data ) => image
3  # 同上。不過允許你使用其他 PIL 支持的像素解碼器。
4  Image.fromstring( mode, size, data, decoder, parameters ) => image

7.    merge : 使用一系列單一頻段(band)的圖像來創建新的一幅圖像。頻段是以一些圖像組成的元組或列表,所有的 band 必須有相同大小的 size 。

1 Image.merge( mode, bands ) =>image

    Image 類中的方法

0.    convert : 返回一個轉換后的圖像的副本。

1 convert
2 # If mode is omitted, a mode is chosed so that all information in the image and the palette can be representedwithout a palette .
3 # when from a colour image to black and white, the library uses the ITU-R 601-2 luma transfrom:
4 # L = R * 299/1000 + G * 587/1000 + B * 114/1000
5  im.convert( mode ) => image
6 
7  # Converts an "RGB" image to "L" or "RGB" using a conversion matrix. The matrix is 4- or 16-tuple.
8  im.convert( mode, matrix ) => image

    下面是一個例子:轉換 RGB 為 XYZ 。

1 rgb2xyz = (
2        0.412453, 0.357580, 0.180423, 0,
3        0.212671, 0.715160, 0.072169, 0,
4        0.019334, 0.119193, 0.950227, 0 )
5 out = im.convert("RGB", rgb2xyz)

1.    copy : 復制圖像。如果你希望粘貼一些東西進圖像里面的話可以使用這個方法,但仍然會保留原圖像。

1 im.copy() => image

2.    crop : 返回圖像某個給定區域。box 是一個 4 元素元組,定義了 left, upper, right, lower 像素坐標。使用這個方法的時候,如果改變原始圖像,可能會,也可能不會改變裁剪生成的圖像。創建一個完全的復制,裁剪復制的時候使用 load 方法。

1 im.crop( box ) => image

3.    draft : 按給出的 mode 和 size 進行配置。可以使用這個方法將彩色JPEG圖片轉為灰度圖。

1 im.draft(mode, size)

4.    filter : 返回圖像使用濾波器后的副本。可以看 這里 獲取更多有用的濾波器。

1 im.filter( filter ) => image

5.    fromstring : 和前面的函數是一樣的功能,不過這個方法是將數據載入到當前圖像。

1 im.fromstring( data ) 
2 im.fromstring( data, decoder, parameters )

6.    getbands : 返回一個元組,包含每一個 band 的名字,比如,在一幅 RGB 格式的圖像上使用 getbands 則返回("R", "G", "B")。

1 im.getbands(  ) => tuple of strings

7.    getbbox : 計算圖像邊框值,返回一個 4-元組 ,值為(左,上,右,下)。

1 im.getbbox()  => 4-tuple or None

8.    getcolors : 在 1.1.5 版本中新添加的。返回一個未排序列表,其元素是元組(count, color)。the count is the number of times the corresponding color occurs in the image 。If the maxcolors value is exceeded, the method stops counting and returns None。

1 im.getcolors() => a list of (count, color) tuples or None
2 im.getcolors( maxcolors ) => a list of (count, color) tuples or None

9.    getdata : 返回一個圖像內容的像素值序列。不過,這個返回值是 PIL 內部的數據類型,只支持確切的序列操作符,包括迭代器和基本序列方法。我們可以通過 list(im.getdata())  為其生成普通的序列。

1 im.getdata() => sequence

10.    getextrema : 返回一個 2-元組 ,值為圖像的最小最大值。在當前PIL版本中,僅支持單一頻段(single-band)的圖像。

1 im.getextrema() => 2-tuple

11.    getpixel : 返回指定位置的像素,如果所打開的圖像是多層次的圖片,那這個方法就返回一個元組。

1 im.getpixel( xy ) => value or tuple

 

12.    histogram : 返回圖像直方圖,值為像素計數組成的列表。如果有參數 mask ,則返回圖像所有部分的直方圖。

1 im.histogram() => list
2 im.histogram( mask ) => list

13.    load : 版本 1.1.6 新添加的。load 返回對象的像素值,可以用來修改像素值。

1 im.load()
2 pix = im.load()
3 print pix[x, y]
4 pix[x, y] = value

 

14.    paste : 1). 粘貼新圖片至圖片中,box 參數可以為 2-元組(upper, left)或是 4-元組(left, upper, right, lower),或者是 None(0, 0)。2). 功能同上。不過是將指定位置填充為某種顏色。

1 im.paste( image, box )
2 
3 im.paste( colour, box )
4 
5 im.paste( image, box, mask )
6 
7 im.paste( colour, box, mask )

15.    point : 

1 im.point( bable ) => image
2 im.point( function ) => image

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM