Tools,

Deskew a PDF for free: 6 tools tested

40 of 40

tilted pages straightened by 3 free tools

Get straighten.py

3 tools straightened every page

40 simulated scans, tilted 1 to 15 degrees both ways
Tool Straight Words found
OpenCV snippet 40 of 40 98.8%
straighten.py 40 of 40 98.6%
deskew (pip) 40 of 40 97.8%
ImageMagick-deskew 40% 24 of 40 79.6%
OCRmyPDF--deskew 20 of 40 61.9%
unpaper 7 of 40 66.7%
No tool 0 of 40 60.1%

Straight means within half a degree. Words found is what Tesseract read after each tool, of 3,480 true words.

straighten.py: PDF in, straight PDF out

It tries every angle from -45 to 45 degrees, turns each page, and saves a new PDF: about 1 second a page.

straighten.py

python3 -m venv venv && . venv/bin/activate
pip install pymupdf pillow numpy
python3 straighten.py in.pdf out.pdf

Pages come out gray, as JPEG at 300 dpi. They grow to fit the turned corners: up to 11.1 by 12.8 inches here. Keep your original.

To make it searchable, run OCRmyPDF on the new file, as in our deskew test. MIT license.

PNG pages: the deskew package

With Poppler, ImageMagick and pip install deskew, these 3 lines straightened all 3 pages of our test PDF:

pdftoppm -r 300 -gray -png in.pdf page
for f in page-*.png; do deskew -o "fixed-$f" "$f"; done
magick -density 300 fixed-page-*.png out.pdf

Where the other 3 fall short

Pages straightened
Tilt OCRmyPDF ImageMagick
1 to 4 degrees 16 of 16 16 of 16
5 and 6 degrees 4 of 8 8 of 8
8 to 15 degrees 0 of 16 0 of 16

Our Actor straightened all 40

Our Apify Actor turns scanned PDFs into text or Markdown. Its own code turned all 40 pages to within 0.05 degrees of the true tilt, and found 98.5% of the words.

It gives text, not a PDF. A scanned page costs $0.006, and pages with a text layer are free.

Try the Actor

How I tested it
  • A made-up letter and invoice in 8 fonts, drawn at 300 dpi with blur, gray paper and noise, saved as JPEG. Renders, not real scans.
  • Each tilted from 1 to 15 degrees in both directions: 40 one-page PDFs.
  • Every tool at its defaults, then the same Tesseract read. The tilt left comes from the slope of the word boxes on each line.
  • The OpenCV row is the common minAreaRect snippet, as posted. It boxes every dark pixel.
  • Times are medians at low priority, one thread each, 3 pages at a time, with other work on the Mac.
  • straighten.py and OCRmyPDF read and write the PDF in that time, and OCRmyPDF does its OCR too. For the other 4, page images and the new PDF are extra.
  • Read by Tesseract 5.5.4. Page images by Poppler 26.03.0.
  • The Actor's code at build 0.1.97 read the same 40 files on our Mac.
  • Not tested: real scans, color pages, dark page edges or stamps, tilts between whole degrees or over 15, other OpenCV versions, online tools, Windows.
Version, and seconds a page
Tool Version Seconds
OpenCV snippet 5.1.0 0.8
straighten.py PyMuPDF 2.1 1.1
deskew (pip) 1.6.1 2.9
unpaper 7.0.0 3.4
OCRmyPDF 17.13.0 4.4
ImageMagick 7.1.2 6.5

Questions people ask

How do I deskew a PDF for free?

Run python3 straighten.py in.pdf out.pdf. It straightened all 40 test pages, tilted up to 15 degrees. The deskew package and an OpenCV snippet did too.

Why does OCRmyPDF --deskew not straighten my PDF?

It asks Tesseract for the angle, and from 5 degrees it often got 0 or no angle. It straightened every test page up to 4 degrees and none past 6.

Can I deskew a PDF online?

Online tools need an upload of your file, so I did not test them. Every tool in the table runs on your own computer, for free.