Python - Pytesseract适用于一个文件而不适用于其他文件

时间:2018-04-15 21:27:41

标签: python

我最近安装了Pytesseract并确保其正常运行我运行了以下测试/代码:

from PIL import Image
from pytesseract import image_to_string

print(image_to_string(Image.open('test.tiff')))

我看到Tesseract从CMD加载并在完成它之后关闭了。之后,Python shell打印出'test.tiff'的内容。伟大的工作...或者我认为。我遇到的问题是当我再次尝试为另一个tiff文件'test2.tiff'运行测试时出现以下错误:

Traceback (most recent call last):
  File "C:\Users\Freeware Sys\Desktop\OTF.py", line 22, in <module>
    print(image_to_string(Image.open('test2.tiff')))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pytesseract\pytesseract.py", line 193, in image_to_string
    return run_and_get_output(image, 'txt', lang, config, nice)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pytesseract\pytesseract.py", line 130, in run_and_get_output
    temp_name, img_extension = save_image(image)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pytesseract\pytesseract.py", line 86, in save_image
    image.save(input_file_name, format=img_extension, **image.info)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\PIL\Image.py", line 1935, in save
    save_handler(self, fp, filename)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\PIL\TiffImagePlugin.py", line 1535, in _save
    raise IOError("encoder error %d when writing image file" % s)
OSError: encoder error -2 when writing image file

这很奇怪。所以我尝试添加额外的pytesseract快速入门代码,因为pytessseract可能没有调用tesseract。

from PIL import Image
from pytesseract import image_to_string

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'

print(image_to_string(Image.open('test2.tiff')))

仍然无效。有趣的是,如果我直接从CMD运行tesseract并通过它推送'test2.tiff' - 它确实有效。有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

打开图像时的用户转换方法。

Products(this is the comboBox) | Quantity |
-------------------------------+----------+
Water                                2        
Juice                                1        
Juice                                2       
Candy                                5 
Cake                                 1
Water                                4  

来自[https://stackoverflow.com/a/52115274/3728540]的引用。