跳過到頁腳內容
OCR 工具

如何將圖片轉換為文本

在當今的數位時代,將基於圖像的內容轉換為易於閱讀、編輯和搜尋的文字至關重要。 這在紙本文件歸檔、從圖像中提取關鍵資訊或將印刷材料數位化等場景中尤其重要。 光學字元辨識(OCR)技術為實現此轉換過程的自動化提供了一種解決方案。 IronOCR是一個功能強大的 .NET OCR 函式庫,是實現此目標的非常可靠且有效率的工具。

本文將解釋如何使用IronOCR將圖片轉換為文本,並探討這種轉換如何節省時間、減少錯誤以及簡化資料提取、歸檔和文件處理等流程。

如何將圖片轉換為文字

1.下載用於 OCR 工作的 C# 庫

  1. 建立一個新的IronTesseract實例
  2. 使用OcrImageInput載入圖像
  3. 使用OcrRead讀取影像內容
  4. 將 OCR 結果匯出到文字文件

為什麼要將圖片轉換為文字?

您可能出於多種原因想要將圖像轉換為文本,包括:

*資料提取:*從掃描文件和圖像中提取文本,用於存檔或資料處理目的。 編輯掃描內容:編輯或更新先前掃描文件中的文本,節省手動重新輸入內容的時間。 提高可訪問性:將印刷材料轉換為數位文本,使其可供螢幕閱讀器或文字轉語音應用程式存取。 自動化:**透過讀取發票、收據或名片上的文本,自動輸入和處理資料。

如何開始將圖像轉換為文本

在探討如何利用 IronOCR 強大的圖像轉文字功能從圖像中提取文字之前,讓我們先使用線上工具Docsumo來了解一般的逐步過程。 線上OCR工具無需任何手動設置,對於那些只想進行一些零星OCR任務的用戶來說,是一個不錯的選擇。當然,如果您需要定期執行OCR任務,那麼像IronOCR這樣功能強大的OCR工具可能更適合您。

  1. 開啟線上 OCR 工具
  2. 上傳圖片並開始擷取過程
  3. 將結果資料下載為文字文件

第一步:存取線上OCR工具

要開始使用 OCR 技術從圖像檔案中提取文本,我們首先要找到要使用的線上圖像 OCR 工具。

如何將圖片轉換為文字:圖 1 - Docsumo OCR 工具

第二步:上傳圖片並開始擷取過程

現在,點擊"上傳檔案"按鈕,我們就可以上傳要從中提取文字的圖像檔案了。 該工具將立即開始處理影像。

如何將圖片轉換為文字:圖 2 - Docsumo - 檔案處理

第三步:將結果資料下載為文字文件

圖像處理完成後,我們可以將提取的文字下載為新的文字文件,以便進一步使用或處理。

如何將圖片轉換為文字:圖 3 - Docsumo - 圖片處理完成

您也可以查看文件,突出顯示各個部分以查看其中包含的文字。 如果您只想查看特定章節中的文本,這將特別有幫助。 然後,您仍然可以將文字下載為文字檔案、XLS 或 JSON 格式。

如何將圖片轉換為文字:圖 4

IronOCR入門指南

IronOCR是一個功能強大的 .NET 函式庫,可用於對影像執行 OCR 操作。 它具備多種功能,可以處理各種文件格式(如PNG、JPEG、TIFF和PDF),進行圖像校正,掃描專業文檔(護照、車牌等),提供有關掃描文件的高級信息,轉換掃描文檔,以及突出顯示文本。

安裝 IronOCR 庫

在使用 IronOCR 讀取映像之前,如果您的專案中尚未安裝 IronOCR,則需要先安裝它。 您可以使用 Visual Studio 中的 NuGet 輕鬆安裝 IronOCR。 開啟 NuGet 套件管理器控制台並執行以下命令:

Install-Package IronOcr

或者,您也可以透過 NuGet 套件管理器在解決方案頁面上搜尋 IronOCR 來安裝 IronOCR。

如何將圖片轉換為文字:圖 5

若要在程式碼中使用 IronOCR,請確保在程式碼頂部新增正確的導入語句:

using IronOcr;
using IronOcr;
Imports IronOcr
$vbLabelText   $csharpLabel

將圖像轉換為文字:一個基本範例

首先,讓我們來看一個使用 IronOCR 的基本圖像轉文字範例。 這是任何 OCR 工具的核心功能,在本例中,我們將使用我們在線上工具中使用的 PNG 檔案。 在這個例子中,我們首先實例化了IronTesseract類,並為其分配了變數ocr 。 然後我們使用OcrImageInput類,根據提供的映像檔建立一個新的 OcrImageInput 物件。 最後,使用Read方法從圖像中讀取文字並傳回OcrResult物件。 然後我們可以使用ocrResult.Text存取提取的文字並將其顯示在控制台中。

using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load the image from which to extract text
using OcrImageInput image = new OcrImageInput("example.png");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load the image from which to extract text
using OcrImageInput image = new OcrImageInput("example.png");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
Imports IronOcr

Private ocr As New IronTesseract()

' Load the image from which to extract text
Private OcrImageInput As using

' Perform OCR to extract text
Private ocrResult As OcrResult = ocr.Read(image)

' Output the extracted text to the console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

輸出影像

如何將圖片轉換為文字:圖 6

處理不同的圖片格式

IronOCR 支援多種影像格式,如 PNG、JPEG、BMP、GIF 和 TIFF。 從不同圖像格式讀取文字的過程保持不變,只需載入具有正確擴展名的檔案即可。

using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load a BMP image
using OcrImageInput image = new OcrImageInput("example.bmp");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load a BMP image
using OcrImageInput image = new OcrImageInput("example.bmp");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
Imports IronOcr

Private ocr As New IronTesseract()

' Load a BMP image
Private OcrImageInput As using

' Perform OCR to extract text
Private ocrResult As OcrResult = ocr.Read(image)

' Output the extracted text to the console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

提高OCR準確率

透過優化影像和配置語言、影像解析度和影像雜訊等級等選項,可以提高 OCR 效能。 以下是如何透過使用DeNoise()Sharpen()方法微調 OCR 以提高影像品質的文字擷取準確率的方法:

using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load the image and apply image processing to improve accuracy
using OcrImageInput image = new OcrImageInput("example.png");
image.DeNoise();
image.Sharpen();

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Load the image and apply image processing to improve accuracy
using OcrImageInput image = new OcrImageInput("example.png");
image.DeNoise();
image.Sharpen();

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Output the extracted text to the console
Console.WriteLine(ocrResult.Text);
Imports IronOcr

Private ocr As New IronTesseract()

' Load the image and apply image processing to improve accuracy
Private OcrImageInput As using
image.DeNoise()
image.Sharpen()

' Perform OCR to extract text
Dim ocrResult As OcrResult = ocr.Read(image)

' Output the extracted text to the console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

導出提取的文本

現在我們已經了解了圖像轉文本過程的基本原理,接下來讓我們看看如何匯出生成的文字以供後續使用。 在這個例子中,我們將使用與之前相同的流程來載入影像並進行掃描。 然後,使用File.WriteAllText("output.txt", ocrResult.Text) ,我們建立一個名為output.txt的新文字文件,並將擷取的文字儲存到該文件中。

using IronOcr;
using System.IO;

IronTesseract ocr = new IronTesseract();

// Load the image
using OcrImageInput image = new OcrImageInput("example.png");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Save the extracted text to a file
File.WriteAllText("output.txt", ocrResult.Text);
using IronOcr;
using System.IO;

IronTesseract ocr = new IronTesseract();

// Load the image
using OcrImageInput image = new OcrImageInput("example.png");

// Perform OCR to extract text
OcrResult ocrResult = ocr.Read(image);

// Save the extracted text to a file
File.WriteAllText("output.txt", ocrResult.Text);
Imports IronOcr
Imports System.IO

Private ocr As New IronTesseract()

' Load the image
Private OcrImageInput As using

' Perform OCR to extract text
Private ocrResult As OcrResult = ocr.Read(image)

' Save the extracted text to a file
File.WriteAllText("output.txt", ocrResult.Text)
$vbLabelText   $csharpLabel

如何將圖片轉換為文字:圖 7

IronOCR的主要特點

1.高精度:IronOCR 使用先進的Tesseract OCR 演算法,並包含處理複雜影像的內建工具,確保高精度。 2.多語言支援支援 125 種以上的語言,包括拉丁字母、西里爾字母、阿拉伯字母和亞洲字元等多種書寫系統。 但要注意的是,IronOCR 僅安裝了英語。 要使用其他語言,您需要安裝該語言的附加語言套件。

  1. PDF OCR :IronOCR 可以從掃描的 PDF中提取文本,使其成為文件數位化的寶貴工具。 4.影像清理:它提供去傾斜去雜訊反轉等預處理工具,以提高影像質量,從而提高 OCR 的準確性。 5.易於整合:此 API 可與任何 .NET 專案無縫集成,無論是控制台應用程式、Web 應用程式或桌面軟體。

圖片轉文字的常見用例

*資料輸入自動化*:企業可以使用 OCR 自動從表格、收據或名片中擷取資料。 文件歸檔:組織可以將紙本文件數位化,使其可搜尋且更易於儲存。 可訪問性:將印刷材料轉換為文本,以便在螢幕閱讀器或其他輔助技術中使用。 研究與分析**:快速將掃描的研究資料轉換為文本,以便進行分析或整合到其他軟體工具中。 *學習:將掃描的學習筆記轉換為可編輯的文本,然後您可以將其儲存為 Word 文檔,以便在 IronWord、Microsoft Word 或 Google 文檔等工具中進行進一步處理。

結論

使用 IronOCR 將圖像中的文字轉換為文字是一種快速、準確、高效的文件處理方法。 無論您是處理掃描文件、數位影像還是 PDF 文檔,IronOCR 都能簡化流程,提供高精度、多語言支援和強大的影像處理工具。 對於希望簡化文件管理工作流程、自動擷取資料或增強可存取性的企業而言,這款工具是理想之選。

立即使用免費試用版,親自體驗 IronOCR 的強大功能。 只需幾分鐘即可在您的工作區中完全啟用它,這樣您就可以立即開始處理 OCR 任務!

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。