顯示具有 ocr 標籤的文章。 顯示所有文章
顯示具有 ocr 標籤的文章。 顯示所有文章

2024年10月13日 星期日

Combining Scanned Images into a PDF with OCR on Mac OS X

Combining Scanned Images into a PDF with OCR on Mac OS X

This guide will walk you through the process of combining multiple scanned images of book pages into a single PDF file on Mac OS X and using OCR (Optical Character Recognition) to convert the text into searchable, selectable electronic text.

1. Installing Required Tools

First, we need to install a few tools to handle image and PDF processing, as well as perform OCR. The following tools are required:

  • ImageMagick: For image format conversion and processing.
  • ocrmypdf: For performing OCR on PDF files.
  • Tesseract: OCR engine used for text recognition.
  • Poppler: A PDF utilities suite, which includes tools like pdfunite for merging PDFs.

Install these tools using Homebrew:

brew install imagemagick    # Install ImageMagick
brew install ocrmypdf       # Install OCRMyPDF
brew install tesseract-lang # Install Tesseract language packs
brew install poppler        # Install Poppler

2. Merging Images into a PDF

If your scanned output is in image format (e.g., JPG), you can use ImageMagick to combine multiple image files into a single PDF document. This ensures that the subsequent OCR process works on a single PDF file.

To combine all .jpg images in the current directory into a single 001.pdf file:

magick *.jpg 001.pdf

This command uses the magick tool from ImageMagick to merge all .jpg files in the directory into one PDF file named 001.pdf.

3. Running OCR on the PDF

Next, we use the ocrmypdf tool to perform OCR on the generated PDF file and save the output as a new file. In this step, we specify the OCR languages as English (eng) and Traditional Chinese (chi_tra).

Run the following command:

ocrmypdf -l eng+chi_tra+chi_tra_vert 001.pdf 001a.pdf

This command will apply OCR to the 001.pdf file and save the result as 001a.pdf. The -l eng+chi_tra+chi_tra_vert option indicates that both English and Traditional Chinese will be used for text recognition.

4. Merging Multiple OCR Processed PDFs

If you have multiple PDF files that need to be combined into a single final PDF, the pdfunite tool can do this easily. To merge the processed PDF files into a single output named combined_pdf.pdf, use the following command:

pdfunite 001a.pdf 002a.pdf 003a.pdf combined_pdf.pdf

This command merges 001a.pdf, 002a.pdf, and 003a.pdf into a single combined_pdf.pdf file.

Summary

By following these steps, you can:

  1. Merge scanned images into a PDF file.
  2. Use the Tesseract engine via ocrmypdf to perform OCR text recognition.
  3. Combine multiple PDF files into a final merged PDF.

This allows you to convert scanned images of book pages into a searchable, selectable, and copyable PDF document.

Mac OS X 環境下將圖片合併為PDF並進行OCR處理

將圖片合併為PDF並進行OCR處理

在這份說明文件中,我們將介紹如何在Mac OS X環境下,將多個掃描成圖片的書籍頁面合併為一個PDF檔案,並使用OCR(光學文字識別)將其中的文字轉換為可搜索和選取的電子文字。

1. 安裝必要的工具

首先,我們需要安裝一些工具來處理圖片和PDF,並進行OCR。以下是需要的工具:

  • ImageMagick:用於圖片格式轉換與處理。
  • ocrmypdf:用於將PDF檔案進行OCR處理。
  • Tesseract:OCR引擎,用於實際進行文字識別。
  • Poppler:PDF工具集,包含 pdfunite 等工具,用於合併PDF。

使用 Homebrew 安裝這些工具:

brew install imagemagick    # 安裝 ImageMagick
brew install ocrmypdf       # 安裝 OCRMyPDF
brew install tesseract-lang # 安裝 Tesseract 語言包
brew install poppler        # 安裝 Poppler

2. 圖片合併為PDF

當掃描結果以圖片格式(如 JPG)儲存時,我們可以使用 ImageMagick 將多個圖片檔案合併為單一 PDF 文件。這樣做可以保證後續的 OCR 處理僅在一個 PDF 檔案上進行。

將當前目錄下的所有 .jpg 圖片檔合併為 001.pdf:

magick *.jpg 001.pdf

這條指令將使用 ImageMagick 中的 magick 工具,將目錄中的所有 .jpg 檔案依序合併成一個 PDF 文件 001.pdf。

3. 執行 OCR 文字識別

接下來,我們使用 ocrmypdf 工具來對生成的 PDF 檔案進行 OCR 處理,並將處理後的 PDF 儲存為新的文件。在此步驟中,我們指定使用英語(eng)和繁體中文(chi_tra)作為 OCR 的語言。

執行指令:

ocrmypdf -l eng+chi_tra+chi_tra_vert 001.pdf 001a.pdf

這條指令會對 001.pdf 文件進行 OCR 文字識別,並將結果儲存為 001a.pdf。其中,-l eng+chi_tra+chi_tra_vert 表示同時使用英語和繁體中文進行文字識別。

4. 合併多個 OCR 處理後的 PDF 檔案

如果你有多個 PDF 檔案需要合併為一個最終的 PDF 檔案,pdfunite 工具可以輕鬆做到這點。將處理過的 PDF 檔案合併為單一檔案 combined_pdf.pdf:

pdfunite 001a.pdf 002a.pdf 003a.pdf combined_pdf.pdf

這條指令將 001a.pdf、002a.pdf 和 003a.pdf 三個檔案合併成一個 combined_pdf.pdf。

總結

透過這些步驟,你可以:

  1. 將掃描的圖片合併成 PDF 檔案。
  2. 使用 Tesseract 引擎透過 ocrmypdf 進行 OCR 文字識別。
  3. 將多個 PDF 檔案合併成一個完整的 PDF。

這樣,你就能將掃描成圖片的書籍內容轉換為可以搜索、選取和複製文字的 PDF 電子檔。