Orientasi dokumen yang diambil dengan ponsel salah. Proses EXIF terlebih dahulu lalu kirimkan ke OCR.
Foto kontrak di ponsel saya terlihat vertikal, namun setelah diunggah berubah menjadi horizontal. Alasannya mungkin karena pikselnya sendiri tidak diputar, pemirsa hanya menyesuaikan tampilan berdasarkan penanda orientasi EXIF. Jika input OCR mengabaikan tanda ini, maka halaman akan dibaca ke arah lain.
Foto kontrak yang diambil dengan ponsel berbentuk vertikal di album, tetapi menjadi horizontal setelah diunggah. Banyak orang akan mengira fotonya bengkok atau filenya salah. Artikel ini ditulis untuk orang-orang yang perlu mengirim foto ponsel ke dalam OCR atau proses penerjemahan untuk membantu Anda membedakan antara "arah tampilan" dan "arah piksel".
Setelah membaca ini, Anda akan mengetahui apa itu bendera orientasi EXIF, fungsi apa yang digunakan Pillow untuk menanganinya, dan masalah apa yang tidak dapat diselesaikan. Contohnya menggunakan sampel sintetis dan kodenya telah diperiksa di lingkungan yang terisolasi, namun hal ini tidak menjamin semua file atau performa produk sebenarnya. Sebelum memproses dokumen penting, harap simpan dokumen asli dan tinjau sesuai dengan batasan dan item penerimaan dalam teks.
Apa yang harus saya lakukan jika dokumen yang diambil dengan ponsel saya salah arah? Kesimpulan satu kalimat
Pertama gunakan ImageOps.exif_transpose untuk menerapkan tanda arah EXIF, lalu kirim gambar yang disesuaikan ke OCR; masalah kemiringan dan perspektif perlu ditangani secara terpisah.
| situasi Anda | Praktik yang direkomendasikan | alasan |
|---|---|---|
| Tampilan viewer normal, namun hasil parsingnya sideways | Pertama gunakan ImageOps.exif_transpose untuk menerapkan arah EXIF | Piksel tidak diputar, hanya lapisan tampilan yang disesuaikan |
| Seluruh foto agak miring | Evaluasi koreksi kemiringan satu per satu | exif_transpose tidak menangani sudut pengambilan gambar |
| Kertasnya berbentuk trapesium | Deteksi batas kertas dan lakukan koreksi perspektif | Distorsi perspektif bukanlah masalah orientasi EXIF |
| Perlu menyimpan kotak yang dikembalikan oleh OCR | Catat ukuran masukan, transformasi orientasi, dan ukuran keluaran secara bersamaan | Koordinat gambar yang disesuaikan berbeda dengan gambar aslinya |
Arah tampilan dan arah piksel harus dilihat secara terpisah.
Piksel foto seluler mungkin tidak diputar sendiri, dan tag orientasi EXIF dalam file memberi tahu pemirsa cara menampilkannya. Pemirsa akan menyesuaikan gambarnya, dan jika mesin OCR mengabaikan tanda ini, ia akan membaca halaman ke arah lain.
ImageOps.exif_transpose Pillow menyesuaikan piksel berdasarkan informasi orientasi dan menghapus penanda orientasi yang diterapkan untuk menghindari rotasi berikutnya. Ini berkaitan dengan orientasi EXIF, bukan kemiringan bidikan sembarangan atau distorsi perspektif. Meluruskan kertas yang diambil miring dengan ponsel masih memerlukan serangkaian proses geometris lainnya.
Uji batas rotasi dengan piksel yang disintesis
Kode berikut menggunakan gambar komposit untuk memeriksa apakah penanda orientasi diterapkan dengan benar:
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()
Piksel merah di sudut digunakan di sini untuk membantu memeriksa hubungan geometris, tanpa memanggil mesin OCR atau menyimulasikan teks sebenarnya. Oleh karena itu, pengujian ini hanya dapat mendukung kesimpulan bahwa "penanda arah diterapkan dengan benar", tetapi tidak dapat mendukung "seberapa besar tingkat pengenalan teks telah ditingkatkan". Contohnya bergantung pada Pillow dan harus dijalankan dalam versi terkunci.
Alihkan anomali arah
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.
Referensi: 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


