Prompt
How do I fix OCR not reading skewed scanned documents?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
OCR usually fails on skewed scans because the text baseline isn’t horizontal. The fix is deskewing before OCR.
What to do
- Detect the angle of the page/text.
- Rotate the image to make lines horizontal.
- Run OCR on the corrected image.
Common deskew methods
- Hough transform: finds dominant text-line angles.
- Min-area bounding box: estimates the page angle from foreground pixels.
- Projection profiles: tries angles and picks the one with the sharpest horizontal text lines.
Quick example in Python with OpenCV
import cv2
import numpy as np
img = cv2.imread("scan.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
deskewed = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
cv2.imwrite("deskewed.jpg", deskewed)
Then OCR deskewed.jpg instead of the original.
Also check scan quality
- Increase resolution to 300 DPI or higher
- Ensure good contrast
- Remove shadows/noise
- Crop borders if needed
- If pages are curved or photographed, use dewarping too, not just deskewing
If you use Tesseract
Tesseract can benefit from preprocessing, but for small skew it may handle some automatically. For stronger skew, deskew first.
If you want, I can give you:
- a ready-to-run Python script
- a Tesseract-specific workflow
- or help diagnose a sample image.