A orientação do documento tirado com o celular está errada. Processe o EXIF primeiro e depois envie-o para OCR.
A foto do contrato no meu telefone parecia vertical, mas depois de carregá-la, virou horizontalmente. O motivo pode ser que os próprios pixels não sejam girados, o visualizador simplesmente ajusta a exibição com base nos marcadores de orientação EXIF. Se a entrada do OCR ignorar esta marca, a página será lida na outra direção.
As fotos contratuais tiradas com telefones celulares ficam verticais no álbum, mas ficam horizontais após o upload. Muitas pessoas vão pensar que a foto está torta ou que o arquivo está errado. Este artigo foi escrito para pessoas que precisam enviar fotos de telefones celulares para o OCR ou processo de tradução para ajudá-lo a distinguir entre "direção de exibição" e "direção de pixel".
Depois de ler isto, você saberá o que é um sinalizador de orientação EXIF, qual função o Pillow usa para lidar com ele e quais problemas ele não resolve. Os exemplos utilizam amostras sintéticas e o código foi verificado em ambiente isolado, mas isso não representa garantia de todos os arquivos reais ou desempenho do produto. Antes de processar documentos importantes, guarde os originais e revise-os de acordo com os limites e itens de aceitação do texto.
O que devo fazer se o documento levado com meu celular estiver na direção errada? Conclusão de uma frase
Primeiro use ImageOps.exif_transpose para aplicar a marca de direção EXIF e depois envie a imagem ajustada para OCR; questões de inclinação e perspectiva precisam ser tratadas separadamente.
| sua situação | Recommended practice | Reason |
|---|---|---|
| O visualizador é exibido normalmente, mas os resultados da análise ficam de lado | Primeiro use ImageOps.exif_transpose para aplicar a direção EXIF | Os pixels não são girados, apenas a camada de exibição é ajustada |
| The photo is tilted slightly as a whole | Evaluate tilt correction separately | exif_transpose does not handle shooting angles. |
| The paper is trapezoidal. | Detecte limites de papel e execute correção de perspectiva | A distorção de perspectiva não é um problema de orientação EXIF |
| You need to save the box returned by OCR | Grave o tamanho da entrada, a transformação da orientação e o tamanho da saída ao mesmo tempo | The adjusted coordinates of the map are different from the original one. |
# look at the display direction separately from the pixel direction
The pixel of the phone photo itself may not be rotated, and the EXIF direction mark in the file tells the viewer how to display it. The viewer adjusts the screen accordingly, and if the OCR engine ignores this tag, it will read the page in the other direction.
The ImageOps.exif_transpose of Pillow adjusts the pixels based on the orientation information and removes the applied orientation markers to avoid further rotation. It deals with the EXIF direction, not arbitrary shooting tilt or perspective deformation. Another set of geometric processing is still needed to straighten the paper with the crooked beat of the phone.
# Test the rotation boundary with synthetic pixels
The following code uses the composite image to check that the direction marker is applied correctly:
from PIL import Image, ImageOps
source = Image.new("RGB", (40, 20), "white")
source.putpixel((0, 0), (255, 0, 0))
source.getexif()[274] = 6
fixed = ImageOps.exif_transpose(source)
assert fixed.size == (20, 40)
assert fixed.getpixel((19, 0)) == (255, 0, 0)
assert fixed.getexif().get(274) is None
assert source.size == (40, 20)
again = ImageOps.exif_transpose(fixed)
assert again.size == fixed.size
assert again.tobytes() == fixed.tobytes()
Here, the red pixels in the corner are used to help check the geometry, and there is no call to the OCR engine, nor does it simulate real text. Therefore, the test can only support the conclusion that "the direction mark is applied correctly", but not "how much the character recognition rate is improved". The example relies on Pillow and should be run in a locked version.
# shunting direction anomalies
When you encounter a direction problem, first look at the phenomenon, and then judge the cause. The following table can help you decide what to do next:
| Observe | Possible reasons | Next step |
|---|---|---|
| Viewer normal, parser tapped | EXIF not applied | Check orientation labels and pixels |
| The whole page tilts slightly | Shooting angle | Independent evaluation of tilt correction |
| The page is trapezoidal | Perspective deformation | Correction after detecting paper boundary |
| The text is upside down but unmarked | Missing metadata | Judge according to the content of the page, do not guess blindly |
# coordinate recording starts after rotation
If OCR returns a text box on the adjusted image, the coordinates of the box correspond to the adjusted image, not the original one. Therefore, three pieces of information need to be retained: input size, direction transformation and output size. When the display is highlighted, the inverse transformation is applied to the coordinates to return to the original position.
Save only one set of boxes without recording which picture it belongs to, and the highlighted position deviates from the text.
# correct the image before translation
When dealing with the scanning material through the OCR and translation entrance of OmniDoc (Product Import), you can first confirm the direction against the original. Orientation, perspective and clarity are three different check items, which need to be confirmed separately.
After the input conditions are sorted out, the bilingual results are used to judge the translation quality. If a missing word is attributed to the translation model, the real input problem will often be left out.
Referências: Pillow ImageOps.
# Core comparison
| Working with object | Treatment mode | Whether it is completed byexif_transpose |
|---|---|---|
| EXIF direction mark | Adjust pixels by marker | Yes |
| The whole page tilts slightly | Shooting angle correction | No, independent evaluation is required. |
| The page is trapezoidal | Perspective correction | No, paper boundaries need to be detected |
# which method is suitable for you?
if you.
- the viewer is normal, and the OCR result is horizontal : apply the EXIF direction first, and then re-enter the OCR. - overall skew of the photo : take tilt correction as an independent step and do not expect direction processing to solve the problem. - the edge of the paper is trapezoidal : check the edge of the paper first, and then make perspective correction. - need to highlight the location of the original image : save the input size and transformation information, and then reverse transform when displaying.
# frequently asked questions (FAQ)
1. Can exif_transpose correct the crooked paper?
No, I can't. It only adjusts pixels according to EXIF orientation markers and does not deal with shooting angles or perspective deformations, which require additional geometric processing.
2. Why does the viewer display normally, but the OCR result is horizontal?
The viewer applies EXIF direction markers, and the pixels themselves may not be rotated. If OCR ignores the tag, it reads to the page in the other direction.
3. Can the sample code show how much the recognition rate has been improved?
No, I can't. The example uses composite pixels to check if the orientation markers are applied correctly, without invoking the OCR engine or simulating real text.
4. Why does the box returned by OCR not match the location of the original image?
If OCR is running on the adjusted diagram, the coordinates of the box correspond to the adjusted diagram. It is necessary to record the input size, transformation and output size, and then do the inverse transformation during the display.
# Summary
The direction of mobile phone photos, first look at the display direction or pixel direction, and then deal with the direction, tilt and perspective respectively. Please keep the original and complete the review before dealing with important documents.
Experience now: upload the document and start translation, keeping the original typesetting


