that's not for TrOCR, it's just for OCR, which may not work for handwriting
I did try some of the GPT steps:
pip install --upgrade transformers pillow pdf2image
getting some errors:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━ 3/4 [transformers] WARNING: The scripts transformers and transformers-cli are installed in '/home/user/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
mistral-common 1.5.2 requires pillow<11.0.0,>=10.3.0, but you have pillow 12.1.0 which is incompatible.
moviepy 2.1.2 requires pillow<11.0,>=9.2.0, but you have pillow 12.1.0 which is incompatible.
this is what GPT said to run, but it makes no sense because I don't have TrOCR even downloaded or running at all.
Install packages: pip install --upgrade transformers pillow pdf2image
Ensure poppler is installed:
Ubuntu/Debian: sudo apt install -y poppler-utils
macOS: brew install poppler
Execute: python3 trocr_pdf.py input.pdf output.txt
That's the script to save and run.
#!/usr/bin/env python3
import sys
from pdf2image import convert_from_path
from PIL import Image
import torch
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
def main(pdf_path, out_path="output.txt", dpi=300):
device = "cuda" if torch.cuda.is_available() else "cpu"
model_name = "microsoft/trocr-base-handwritten"
processor = TrOCRProcessor.from_pretrained(model_name)
model = VisionEncoderDecoderModel.from_pretrained(model_name).to(device)
pages = convert_from_path(pdf_path, dpi=dpi)
results = []
for i, page in enumerate(pages, 1):
page = page.convert("RGB")
# downscale if very large to avoid OOM
max_dim = 1600
if max(page.width, page.height) > max_dim:
scale = max_dim / max(page.width, page.height)
page = page.resize((int(page.width*scale), int(page.height*scale)), Image.Resampling.LANCZOS)
pixel_values = processor(images=page, return_tensors="pt").pixel_values.to(device)
generated_ids = model.generate(pixel_values, max_length=512)
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
results.append(f"--- Page {i} ---\n{text.strip()}\n")
with open(out_path, "w", encoding="utf-8") as f:
f.write("\n".join(results))
print(f"Saved OCR text to {out_path}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 trocr_pdf.py input.pdf [output.txt]")
sys.exit(1)
pdf_path = sys.argv[1]
out_path = sys.argv[2] if len(sys.argv) > 2 else "output.txt"
main(pdf_path, out_path)