The image looks fine and OCR returns a blank page: three silent traps in EXIF orientation, 16-bit depth and transparency
The worst production failures are the ones that do not announce themselves. An image goes through preprocessing, the output is a valid PNG with normal dimensions and a normal file size, it opens fine on screen, and then OCR returns nothing, or a page of garbage. This post puts the three most common causes on one measurement setup: the orientation tag, the bit depth, and the alpha channel.
The official documentation draws the boundary first. Vision input accepts PNG, JPEG, WEBP and non-animated GIF, up to 512 MB total payload and 1,500 images per request, and it states plainly that the model does not process file names or metadata (checked 2026-12-04, applies to vision models such as gpt-6-astra). That last sentence is the root of the first trap.
How the test was run
- Sample: 3 synthetic documents, 1400 x 2000 px, black text on white, 8 lines and 102 characters each (order number, date, amounts, note).
- Variants: 6 files derived from each original (upright baseline, EXIF orientation, 16-bit full range, 16-bit scan, transparent with RGB=0, transparent with RGB=255).
- Paths: every variant goes through a naive path (
Image.open().convert('RGB'), then save) and a fixed path (order at the end). Output keeps the original dimensions, so the only variable is the preprocessing step. - Measurement: tesseract 4.1.1 (
chi_sim,--psm 4) on each output, punctuation folded to half width, whitespace removed, then Levenshtein distance divided by the ground-truth character count, which is the character error rate (CER). Python 3.12.11, Pillow 10.4.0.
Three traps, one shared symptom
| Variant | naive CER, three docs | fixed CER, three docs | How naive fails |
|---|---|---|---|
| Upright 8-bit RGB (baseline) | 0.00% / 0.00% / 1.02% | same | - |
| EXIF Orientation=6 | 100.00% / 100.00% / 100.00% | 0.00% x3 | all 3 blank, output 2000x1400 |
| 16-bit full range (ink=0) | 2.94% / 0.98% / 3.06% | 0.00% x3 | antialiasing flattened, strokes thin |
| 16-bit scan (3000-60000) | 100.00% / 100.00% / 100.00% | 0.00% x3 | all 3 blank, 100% of pixels = 255 |
| Transparent, RGB=0 under alpha | 100.00% / 100.00% / 100.00% | 0.00% x3 | all 3 blank, mean pixel value 0.0 |
| Transparent, RGB=255 under alpha | 0.00% / 0.00% / 1.02% | 0.98% / 0.00% / 0.00% | nothing breaks, reads fine |
Four rows sit at 100%. Not one of those four paths raised an exception, and every output was a standard PNG.
Trap one: the orientation lives in the tag, not in the pixels
The first variant is a 2000x1400 landscape bitmap that declares, through EXIF tag 274=6, that it should be rotated 90 degrees clockwise for display. Phone cameras, scanners and design tools export this shape all the time.
The naive path calls convert('RGB') and saves. The output is 2000x1400: not a pixel moved, and the tag is gone. A human looks at the file and sees a document lying on its side; a program looks at the dimensions and sees a landscape image; OCR gets a sideways document and returns nothing for all three.
The correct first step is ImageOps.exif_transpose. The Pillow documentation is explicit: if the image carries an orientation tag other than 1, it transposes accordingly and removes the orientation data. In the test the size came back to 1400x2000, the tag was empty, the mean absolute pixel difference from the baseline was 0.064 (JPEG re-encoding), and OCR returned to 0%.
Order matters here: transpose before you resize. Once you resize and re-save, the tag is dropped by the encoder while the pixels are still sideways, and nothing downstream can recover the direction. Do not count on the server either, since the documentation says metadata is not processed.
Trap two: black in a 16-bit image is not zero
A 16-bit grayscale PNG spans 0-65535, while convert('RGB') only understands 0-255 and truncates everything above it.
The full-range file (ink exactly 0, paper 65535) survives in a degraded form. 98.66% of pixels are clipped to 255, the ink stays at 0, and the result is effectively a binarized copy: still recognizably the same page, just with thinner strokes. CER went from 0.00% to 2.94% (3 wrong characters out of 102). Silent degradation.
The scan file is fatal. Real scanner ink is rarely pure black, so this variant was generated across 3000-60000. Every pixel is greater than 255, all of them clip to 255, the page turns entirely white, and all three documents come back empty. The file itself is fine: mode reads I;16, the maximum is 65535, and it displays correctly as black on white, because viewers honor the bit depth while your convert does not.
There is a second order trap: if you resize first, Image.open(png).thumbnail() raises ValueError: image has wrong mode on I;16. Same file, two outcomes: convert first and it silently degrades, resize first and it fails loudly. Which one you get depends on the order of two lines in your code.
The fix is not dividing by 257 but stretching to the observed maximum, 255 / max(pixel). For the 3000-60000 file the ink lands near 12 and the paper at 255, and recognition returns to 0%.
Trap three: transparency is an alpha question, RGB cannot be trusted
The same description, "black text on white with a transparent background", produces two different files depending on the export path:
- Design tool export: RGB under the transparent area is 0, alpha is 0.
convert('RGB')drops alpha, the whole image becomes black (mean 0.0), and all three documents come back empty. - A code path such as
putalpha: RGB under the transparent area stays 255, alpha is 0. The sameconvert('RGB')yields a normal black-on-white page with CER identical to the baseline.
Both files look the same in a viewer, both report RGBA in the header, and their sizes are close. The difference exists only in the RGB values nobody can see, which is exactly the part convert('RGB') reads and the alpha channel it ignores.
So the test cannot be "does it look white". Composite first: create Image.new('RGB', size, 'white') and paste through the alpha mask. After that both export paths return to 0%.
Order: geometry first, channels second, size last
All three traps live outside the pixels, so the fix has to follow the dependency order of that metadata and do it in one pass:
from PIL import Image, ImageOps
import numpy as np
def preprocess(path, out, max_side=1600):
im = Image.open(path)
im = ImageOps.exif_transpose(im) # 1 straighten first, tag is removed with it
if im.mode in ("I;16", "I"): # 2 normalize depth by the observed maximum
a = np.asarray(im, dtype=np.float32)
im = Image.fromarray(np.clip(a * (255.0 / max(a.max(), 1)), 0, 255).astype(np.uint8))
if im.mode in ("RGBA", "LA", "P"): # 3 composite transparency onto white paper
im = im.convert("RGBA")
bg = Image.new("RGB", im.size, (255, 255, 255))
bg.paste(im, mask=im.split()[-1])
im = bg
im = im.convert("RGB") # 4 normalize to 8-bit RGB
im.thumbnail((max_side, max_side), Image.LANCZOS) # 5 resize last
im.save(out)
return imThe order is not interchangeable. Straightening has to precede resizing, or the tag is lost and direction has no reference left. Compositing has to precede the RGB conversion, or alpha is already gone. Depth normalization has to precede resizing, or mid-grays are clipped first and the resize only interpolates wrong data.
Four assertions before the call
Add four checks between the preprocessing function and the model call. They cost milliseconds:
- Aspect ratio: the output aspect ratio matches the original after straightening, within 1%. A sideways image is caught here.
- Mode: must be
RGB.RGBA,I;16andPdo not pass. - Mean brightness: for white-background documents the grayscale mean should land between 200 and 254. The measured baseline was 250.4, the all-white page 255.0, the all-black page 0.0, so both dead outputs are blocked. This only applies to white-background documents; dark-mode screenshots need a range set by the business.
- Orientation tag: tag 274 must be absent from the output. If it is present, the straightening never happened.
Once all four pass, talk about compression and token budgets. Do it in the other order and every parameter you tune is being tuned on the wrong data.