Python快速筛选包含关键字的图片
您可以使用Python的Pillow库来处理图像,并使用Tesseract库来提取图像中的文本。下面是一个示例代码,展示如何从100张照片中选取包含关键字的信息:
from PIL import Image
import pytesseract
import os
# 设置Tesseract的路径(根据您的安装路径进行更改)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 定义关键字
keyword = 'your_keyword'
# 定义图像文件夹路径
image_folder = 'path_to_your_image_folder'
# 遍历图像文件夹中的每张图片
for filename in os.listdir(image_folder):
if filename.endswith('.jpg') or filename.endswith('.png'):
# 打开图像文件
image_path = os.path.join(image_folder, filename)
image = Image.open(image_path)
# 使用Tesseract提取图像中的文本
text = pytesseract.image_to_string(image)
# 检查文本中是否包含关键字
if keyword in text:
print('Image', filename, 'contains the keyword:', keyword)
请注意,此代码需要Pillow和Tesseract库的安装。您可以使用以下命令来安装这些库:
pip install pillow pytesseract
此外,您还需要下载和安装Tesseract OCR引擎。您可以在Tesseract的官方网站上找到适用于您操作系统的安装程序。
希望这可以帮助您解决问题!
原文地址: https://www.cveoy.top/t/topic/lZ68 著作权归作者所有。请勿转载和采集!