簡體   English   中英

無法在python中使用pytesseract從tif圖像中提取文本

[英]unable to extract text from tif image using pytesseract in python

我無法在 Python 中使用 pytesseract & PIL 從 .tif 圖像文件中提取文本。 它適用於 .png、.jpg 圖像文件,它只在 .tif 圖像文件中出錯。 我使用的是 Python 3.7.1 版本

它在運行 .tif 圖像文件的 Python 代碼時出現以下錯誤。 請讓我知道我做錯了什么。

Fax3SetupState: Bits/sample must be 1 for Group 3/4 encoding/decoding.
Traceback (most recent call last):
  File "C:/Users/u88ltuc/PycharmProjects/untitled1/Image Processing/Prog1.py", line 13, in <module>
    image_to_text = pytesseract.image_to_string(image, lang='eng')
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\pytesseract\pytesseract.py", line 347, in image_to_string
    }[output_type]()
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\pytesseract\pytesseract.py", line 346, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\pytesseract\pytesseract.py", line 246, in run_and_get_output
    with save(image) as (temp_name, input_filename):
  File "C:\Program Files\Python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\pytesseract\pytesseract.py", line 171, in save
    image.save(input_file_name, format=extension, **image.info)
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\PIL\Image.py", line 2102, in save
    save_handler(self, fp, filename)
  File "C:\Users\u88ltuc\PycharmProjects\untitled1\venv\lib\site-packages\PIL\TiffImagePlugin.py", line 1626, in _save
    raise OSError("encoder error %d when writing image file" % s)
OSError: encoder error -2 when writing image file

下面是它的 Python 代碼。

#Import modules
from PIL import Image
import pytesseract

# Include tesseract executable in your path
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Create an image object of PIL library
image = Image.open(r'C:\Users\u88ltuc\Desktop\12110845-e001.tif')

# pass image into pytesseract module
image_to_text = pytesseract.image_to_string(image, lang='eng')

# Print the text
print(image_to_text)

以下是 tif 圖像及其鏈接:

在此處輸入圖片說明

https://ecat.aptiv.com/docs/default-source/ecatalog-documents/12110845-e001-tif.tif?sfvrsn=3ee3b8a1_0

首先,您應該更改您的圖片擴展名。 這也許可以解決您的問題:

from PIL import Image
from io import BytesIO
import pytesseract

img = Image.open(r"C:\Users\u88ltuc\Desktop\12110845-e001.tif")
TempIO = BytesIO()
img.save(TempIO,format="JPEG")
img = Image.open(BytesIO(TempIO.getvalue()))

print(pytesseract.image_to_string(img))

或者如果你不介意你的桌面有兩張相同的圖片,你不需要import BytesIO ,這里是:

from PIL import Image
import pytesseract

img = Image.open(r"C:\Users\u88ltuc\Desktop\12110845-e001.tif")
img.save(r"C:\Users\u88ltuc\Desktop\12110845-e001.jpg")
img = Image.open(r"C:\Users\u88ltuc\Desktop\12110845-e001.jpg")

print(pytesseract.image_to_string(img))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM