在本教程中,您将学习如何使用 Python 在图片中查找文本。它包含设置环境的所有细节、步骤列表以及使用 Python 进行图像文本搜索的示例代码。您将学习设置标志以自定义不同类型的图像中的搜索操作,例如 PNG、JPG、BMP、TIFF 等。
使用 Python 在图像中查找文本的步骤
- 设置环境以使用 Aspose.OCR for Python via .NET 在图像中搜索单词
- 使用 AsposeOcr 类创建 Aspose OCR 引擎的实例
- 使用所需语言配置识别设置
- 使用 image_has_text() 方法检查图像中是否存在所需单词
- 根据搜索结果显示相应消息
这些步骤总结了如何使用 Python 从图像中搜索文本。创建 Aspose OCR 引擎的实例,配置识别设置,并定义要搜索的目标词。最后,使用输入图像名称、目标词、忽略大小写的标志和用于搜索单词的自动倾斜标志调用 image_has_text() 方法。
使用 Python 在图像中查找文本的代码
import aspose.ocr as api # Import the Aspose OCR library | |
from aspose.ocr import License # Import the License class from the Aspose OCR library | |
# Set the Aspose OCR license | |
license = License() | |
license.set_license("license.lic") # Load the license file to activate the full features of Aspose OCR | |
# Create an instance of the Aspose OCR engine | |
asposeOcr = api.AsposeOcr() | |
# Configure recognition settings | |
settings = api.RecognitionSettings() # Create a settings object for OCR recognition | |
settings.language = api.Language.ENG # Set the recognition language to English (ENG) | |
# Define the target word to search for in the image | |
targetWord = "Restaurant" | |
# Check if the target word is present in the image | |
if asposeOcr.image_has_text("Receipt1.png", targetWord, settings, True, False): | |
# If the word is found, print a success message | |
print("The image contains the word " + targetWord) | |
else: | |
# If the word is not found, print a failure message | |
print("The image does not contain the word " + targetWord) |
此示例代码展示了使用 Python 在图像中搜索文本。您可以设置自定义项,例如使用其他算法进行小字体识别、使用标志识别单行、设置文本区域列表以及使用标志在表格中搜索文本。还可以设置其他参数,例如在图像中选择文本语言、黑名单符号列表以及允许的符号和字符列表。
本文教我们如何使用 Python 从图片中搜索文本。如果您想从发票中提取文本,请参阅 使用 Python 从发票中提取数据 上的文章。