在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
似乎很簡單,但不起作用。任何幫助將不勝感激。
可能重複的[Python3錯誤:初始\ _value必須是str或None](http://stackoverflow.com/questions/31064981/python3-error-initial-value-must-be-str-or-none) – Craig
@克雷格我看到一個和不幸的答案並沒有解決我的問題。我正在使用Python 3.6.1順便說一句。 – gmonz
所以你用'BytesIO'替換了'StringIO'並且你得到了同樣的錯誤信息?如果是這樣,那麼將Image.open(StringIO(requests.get(url).content))''分成幾個單獨的行(基本調試)以確定哪個調用正在拋出錯誤。 – Craig