python提取图片文字,保存在表格
时间: 2024-09-12 17:04:12 浏览: 186
在Python中,你可以利用Tesseract OCR库(开源版的Google Tesseract)结合Pandas库来提取图片的文字,并将其保存到CSV或Excel表格中。以下是一个基本步骤:
1. 安装必要的库:首先需要安装`pytesseract`(Python接口的Tesseract)、`Pillow`(图像处理库)和数据分析库如`pandas`。
```bash
pip install pytesseract pillow pandas
```
2. 使用PIL(Pillow)加载图片并进行预处理,确保文字清晰可见。
```python
from PIL import Image
img = Image.open('image.jpg')
gray_img = img.convert('L') # 将彩色转为灰度
```
3. 使用pytesseract提取文字并转换为字符串列表。
```python
import pytesseract
import cv2
text = pytesseract.image_to_string(gray_img, lang='chi_sim') # 语言选择,此处是简体中文
```
4. 创建Pandas DataFrame,将提取的文字存入其中,然后保存为CSV或Excel文件。
```python
data = {'Text': [text]}
df = pd.DataFrame(data)
# 保存为CSV
df.to_csv('output.csv', index=False)
# 或者保存为Excel
df.to_excel('output.xlsx', index=False)
```
阅读全文
相关推荐


















