You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Python实现图片转文字时出现NameError: pytesseract未定义问题求助

Fixing NameError: name 'pytesseract' is not defined in Your Image-to-Speech Script

Let's break down how to resolve this error and get your script up and running smoothly:

First, the immediate issue:

  • You imported only the image_to_string function from pytesseract with from pytesseract import image_to_string, but then tried to call it using pytesseract.image_to_string(img). Since you didn't import the full pytesseract module, Python doesn't recognize that name.

Step 1: Correct the Import

You have two simple fixes here:

Option 1: Import the entire pytesseract module

Update your import lines to:

import pytesseract
from PIL import Image

Now pytesseract.image_to_string(img) will work exactly as you wrote it.

Option 2: Use the imported function directly

Keep your original import but remove the module prefix when calling the function:

from PIL import Image
from pytesseract import image_to_string
img = Image.open('hlo.png')
text = image_to_string(img)  # No `pytesseract.` needed here

Step 2: Install All Required Dependencies

Even after fixing the import, you might run into other errors if you haven't set up all necessary tools:

  • Install the Python packages via pip:
    pip install pytesseract pillow gTTS
    
  • Critical Note: pytesseract is just a wrapper for the Tesseract OCR engine. You need to install the engine itself:
    • Windows: Download the official installer, install it, and add its path (e.g., C:\Program Files\Tesseract-OCR) to your system's PATH variable.
    • macOS: Use Homebrew with brew install tesseract
    • Linux: Use your package manager, like sudo apt install tesseract-ocr

Step 3: Fix the Missing gTTS Import

Your code uses gTTS but never imports it! Add this line at the top of your script:

from gtts import gTTS

Full Corrected Code

Here's the complete, working version of your script:

from PIL import Image
import pytesseract
from gtts import gTTS

# Open the target image
img = Image.open('hlo.png')

# Extract text from the image
text = pytesseract.image_to_string(img)
print("Extracted Text:", text)

# Generate speech and save as MP3
speech = gTTS(text=text, lang="en", slow=False)
speech.save('hello.mp3')

内容的提问来源于stack exchange,提问作者Harikrishnan K H

火山引擎 最新活动