Document Scanner (Photo to PDF)
Operator: Abdulla Al Fahad. How tools are built and checked
A photo of a page is rarely a good scan. The paper is tilted, the camera looks at it from an angle so the top is narrower than the bottom, and the desk shows round the edges. This scanner fixes that on your own device: you mark the four corners of the sheet, it stretches that four-sided patch back into a straight rectangle, cleans up the tones, and saves the result as a PDF or as JPG pictures.
Several photos can be gathered into one multi-page document, put into the order you want, and given a different clean-up each, so a coloured letterhead can stay in colour while the pages of text beside it go black and white. There is no app to install, no account and no watermark, and the photos are processed by this page rather than uploaded.
Key features
- Four draggable corner handles (touch, mouse or arrow keys) over the photo, with an outline that turns red if the corners cross
- Automatic edge suggestion for a light page on a darker surface, with a clearly labelled fallback
- Perspective correction by a homography, with smooth bilinear sampling
- Colour, grayscale and adaptive black-and-white modes, set per page, plus rotation in 90 degree steps
- Up to 30 pages, reordered with up and down buttons
- Export as one PDF (A4, Letter or fit the scan) or as JPG files, zipped when there are several
- A live preview of the flattened page, using the same code as the export
Corner detection, perspective correction and page output
Input is PNG, JPG or WEBP, checked by the file's first bytes, up to 25 MB and 50 megapixels per photo and up to 30 pages in all. A HEIC photo from an iPhone is refused with a request to export it as JPG. The camera's orientation tag is honoured, so a portrait photo appears upright. Each photo is read at no more than 4000 pixels on its long side, which is more than enough for a page and keeps memory use sensible.
The flattened page is sized from your corners: the width is the average of the top and bottom edge lengths, the height the average of the left and right edge lengths, measured in photo pixels. The page is never enlarged beyond what the photo holds, and if the long side would exceed 2480 pixels (the width of A4 at 300 DPI) it is scaled down to exactly 2480. The on-screen preview is drawn smaller, at up to 1100 pixels, so dragging stays quick; the saved file uses the full size.
Four clean-up modes are offered. Original changes nothing. Colour stretches the tones so the darkest 1 percent of pixels become black and the lightest 1 percent become white, using one setting for all three colour channels so hues do not shift. Grayscale does the same on brightness only. Black and white compares each pixel with the average of the area around it, in a window about an eighth of the page's short side, and turns it black when it is more than 10 percent darker than that average, which copes with a shadow falling across half a page. Each page keeps its own mode, plus a turn of 0, 90, 180 or 270 degrees applied after flattening.
The PDF holds one JPEG per page, saved at quality 88. The page size can be A4 (595 by 842 points), US Letter (612 by 792) or the scan's own shape. On A4 or Letter the sheet is turned to landscape for a wide scan, and the picture is centred and fitted inside it, which can leave white bars when the paper's shape differs from A4. The fit option uses a page exactly the size of the picture at 200 pixels per inch. The JPG option saves each page at the same size, as one file or as a ZIP of scan-01.jpg, scan-02.jpg and so on.
How it works
Edge suggestion shrinks the photo to about 240 pixels on its long side, blurs it with a 5 by 5 average so printed text does not split the page, and picks a brightness threshold by Otsu's method, which splits the pixels into light and dark so the two groups differ as much as possible. The largest connected light region is taken as the paper, and its extreme points (smallest and largest x plus y, smallest and largest y minus x) become the corners. The result is accepted only if the region covers between 15 and 97 percent of the photo and its corner shape covers most of it; otherwise the corners start 5 percent in from the photo's edges.
Perspective correction needs the transform that sends the four chosen corners to the corners of a straight rectangle. That transform, a 3 by 3 homography, has eight unknowns, and each corner pair gives two equations, so the page solves an 8 by 8 linear system by Gaussian elimination with row swapping. To draw the output, each of its pixels is sent back through the inverse transform to find where it came from in the photo, and the four photo pixels around that spot are blended. Anything that falls outside the photo is filled white.
Four corners that cross each other, or lie on a line, have no valid transform. The outline turns red, the preview explains the problem, and an export that includes such a page stops with its number rather than saving a distorted one.
When to run it
- Scanning a signed contract page with a phone at your desk and emailing it as an A4 PDF
- Turning five photographed receipts into one expense PDF with the pages in date order
- Flattening a whiteboard or a book page photographed from the side so it can be read and printed
- Making a black-and-white copy of a form for a portal that insists on a scan
- Saving a photographed ID letter as a single cleaned JPG
Running a check
- Add one or more photos. The page looks for each sheet's edges and places the numbered handles.
- Check handles 1 to 4 sit on the top-left, top-right, bottom-right and bottom-left corners of the paper; drag any that are off.
- Look at the preview. Pick Colour, Grayscale or Black and white, and rotate if the page came out sideways.
- Add and reorder other pages with the arrow buttons.
- Choose A4, Letter or Fit the scan, then press Download PDF, or Download JPGs.
Example
A 4032 by 3024 phone photo of a lease page on a dark table, saved as A4 PDF
decoded at 4000 x 3000
corners (px): TL (310, 240) TR (3620, 310) BR (3700, 2750) BL (240, 2690)
mode: Black and white, rotation 0What is worked out and saved
top edge 3310.7, bottom edge 3460.5 -> width 3385.6
left edge 2451.0, right edge 2441.1 -> height 2446.0
long side 3385.6 > 2480, scale 0.7325 -> flattened page 2480 x 1792
landscape, so the A4 page is turned: 841.89 x 595.28 pt, picture fitted and centred
scan.pdf, one JPEG page at quality 88Tips for best results
- Photograph on a dark, plain surface in even light. The automatic edges work best with high contrast between the paper and its background.
- Fill the frame with the paper but keep all four corners in view. A corner outside the photo cannot be marked.
- Use Black and white for text pages and Colour for anything with a stamp, a photo or a coloured logo.
- Zoom the preview mentally: if a line of text on the preview still slopes, one of the corners on that side is a few pixels off.
- Use Fit the scan when the PDF will only be read on screen, and A4 or Letter when it will be printed.
Limits
The edge guess is simple. It will not find a white sheet on a white desk, a dark paper on a dark table, or a page partly covered by a hand, and in those cases you place the corners yourself.
The PDF contains pictures, not text. You cannot search or select words in it; use the OCR tool on the saved pages if you need that.
Black and white drops light content. A pale stamp, a pencil mark or a faint photo may vanish, so check the preview before saving.
Curved paper stays curved. The correction is a straight perspective transform, so a book page that bows near the spine will still show bent lines.
Large photos need memory. Thirty pages of 12-megapixel photos at once is heavy for a phone, so work in smaller batches there.
Common errors and how to fix them
- The four corners cross each other
- Drag the handles back into the order 1 top-left, 2 top-right, 3 bottom-right, 4 bottom-left, so the outline is a four-sided shape that does not fold over itself.
- No clear page edge was found
- The guess needs a lighter page on a darker background. Drag the four corners onto the paper by hand; the page is flattened from wherever you place them.
- A HEIC photo is refused
- Export the picture as JPG from your phone's photo app or share it with Most Compatible turned on, then add it again.
- Text is cut off in the saved page
- A corner sat inside the paper. Move it out to the actual corner, since everything outside the four corners is discarded.
Best practices
- Check each page's preview before exporting, particularly after switching to black and white.
- Save the PDF with a name that describes the document once it is downloaded; the file is named scan.pdf.
- Photograph the page on a plain, darker surface with the flash off. The automatic edge guess looks for a light page against a darker background, and a flash hotspot or a shadow across the paper hurts the black-and-white mode more than a slightly tilted camera does.
What this does not do
There is no camera capture inside the page. On a phone the file picker offers the camera or the photo library, and the photo then comes in like any other file. There is also no automatic page-by-page capture, no shadow removal beyond the black-and-white mode and no text recognition.
Nothing is stored. The photos are held in memory while the page is open, and closing the tab discards them along with your corner positions and settings.
Why the corners matter more than the mode
A corner placed even 30 pixels inside the paper trims content, and one placed 30 pixels outside leaves a dark rim after flattening. Because the whole page is stretched between the four points, a single misplaced corner also tilts the lines of text on the edges next to it. Spending a few seconds on the handles does far more for the result than any of the clean-up modes.
Frequently asked questions
Are my photos uploaded?
No. The straightening, the clean-up and the PDF are all done by this page in your browser.
Can I scan several pages into one PDF?
Yes. Add up to 30 photos, order them with the arrow buttons, and the PDF has one page for each, in that order.
Why does the saved PDF have no selectable text?
Each page is saved as a picture. Run the scan through the Image to Text tool if you want the words.