2017-04-07 46 views
1

在Pytesseract的以下代碼中遇到此錯誤代碼的問題。從PIL進口的ImageFilter (Python的3.6.1,Mac OSX版)Pytesseract轉換過程中出現「ValueError:無法過濾調色板圖像」

進口pytesseract 導入請求 從PIL進口圖片 從IO導入StringIO的,BytesIO

def process_image(url): 
    image = _get_image(url) 
    image.filter(ImageFilter.SHARPEN) 
    return pytesseract.image_to_string(image) 


def _get_image(url): 
    r = requests.get(url) 
    s = BytesIO(r.content) 
    img = Image.open(s) 
    return img 

process_image("https://www.prepressure.com/images/fonts_sample_ocra_medium.png") 

錯誤:

/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/g/pyfo/reddit/ocr.py 
Traceback (most recent call last): 
    File "/Users/g/pyfo/reddit/ocr.py", line 20, in <module> 
    process_image("https://www.prepressure.com/images/fonts_sample_ocra_medium.png") 
    File "/Users/g/pyfo/reddit/ocr.py", line 10, in process_image 
    image.filter(ImageFilter.SHARPEN) 
    File "/usr/local/lib/python3.6/site-packages/PIL/Image.py", line 1094, in filter 
    return self._new(filter.filter(self.im)) 
    File "/usr/local/lib/python3.6/site-packages/PIL/ImageFilter.py", line 53, in filter 
    raise ValueError("cannot filter palette images") 
ValueError: cannot filter palette images 

Process finished with exit code 1 

似乎很簡單,但不起作用。任何幫助將不勝感激。

+0

可能重複的[Python3錯誤:初始\ _value必須是str或None](http://stackoverflow.com/questions/31064981/python3-error-initial-value-must-be-str-or-none) – Craig

+0

@克雷格我看到一個和不幸的答案並沒有解決我的問題。我正在使用Python 3.6.1順便說一句。 – gmonz

+1

所以你用'BytesIO'替換了'StringIO'並且你得到了同樣的錯誤信息?如果是這樣,那麼將Image.open(StringIO(requests.get(url).content))''分成幾個單獨的行(基本調試)以確定哪個調用正在拋出錯誤。 – Craig

回答

2

您擁有的圖像是基於托盤的圖像。您需要將其轉換爲完整的RGB圖像才能使用PIL濾鏡。

import pytesseract 
import requests 
from PIL import Image, ImageFilter 
from io import StringIO, BytesIO 

def process_image(url): 
    image = _get_image(url) 
    image = image.convert('RGB') 
    image = image.filter(ImageFilter.SHARPEN) 
    return pytesseract.image_to_string(image) 


def _get_image(url): 
    r = requests.get(url) 
    s = BytesIO(r.content) 
    img = Image.open(s) 
    return img 

process_image("https://www.prepressure.com/images/fonts_sample_ocra_medium.png") 

你也應該注意到,在.convert().filter()方法返回圖像的副本,只要不改變現有的圖像對象。您需要將返回值分配給變量,如上面的代碼所示。

注:我沒有pytesseract,所以我無法檢查最後一行process_image()

+0

嗯,現在我得到 '/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/g/pyfo/reddit/ocr2.py 回溯(最近呼叫最後): 文件「/ Users/g/pyfo/reddit/ocr2。py「,第19行,在 process_image(」https://www.prepressure.com/images/fonts_sample_ocra_medium.png「) 文件」/Users/g/pyfo/reddit/ocr2.py「,第10行,在process_image return pytesseract.image_to_string(image) AttributeError:模塊'pytesseract'沒有任何屬性'image_to_string'' – gmonz

+0

我知道我接近另一個了,只需將image.enhance(2.0)設置爲?pastebin.com/ uRfhsi8J @Craig或者我確實需要過濾器和增強功能,所以如下:?pastebin.com/GbUkqp9c?我需要在進行銳化和過濾之前進行明確的增強 – gmonz

+0

我無法幫助您使用pytesseract,我建議您繼續試驗代碼,如果你仍然堅持,發佈一個新的問題,指導如何在你的圖像中使用pytesseract。 – Craig