You can detect image orientation and script type using different Page Segmentation Modes (PSM).
Using PSM.AUTO_OSD to analyze layout:
from PIL import Image
from tesserocr import PyTessBaseAPI, PSM
with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api:
api.SetImage(Image.open("image.tif"))
api.Recognize()
it = api.AnalyseLayout()
orientation, direction, order, deskew_angle = it.Orientation()
print(f"Orientation: {orientation}")
Using PSM.OSD_ONLY for direct script/orientation detection:
from tesserocr import PyTessBaseAPI, PSM
with PyTessBaseAPI(psm=PSM.OSD_ONLY) as api:
api.SetImageFile("image.tif")
os = api.DetectOS()
print(f"Orientation: {os['orientation']}")
print(f"Script: {os['script']}")
Using LSTM engine for improved detection (Tesseract 4+):
from tesserocr import PyTessBaseAPI, PSM, OEM
with PyTessBaseAPI(psm=PSM.OSD_ONLY, oem=OEM.LSTM_ONLY) as api:
api.SetImageFile("image.tif")
api.Recognize()
os = api.DetectOrientationScript()
print(f"Orientation: {os['orient_deg']}")
print(f"Script: {os['script_name']}")
from PIL import Image
from tesserocr import PyTessBaseAPI, PSM
with PyTessBaseAPI(psm=PSM.AUTO_OSD) as api:
api.SetImage(image)
api.Recognize()
it = api.AnalyseLayout()
orientation, direction, order, deskew_angle = it.Orientation()
print("Orientation: {:d}".format(orientation))
print("WritingDirection: {:d}".format(direction))
print("TextlineOrder: {:d}".format(order))
print("Deskew angle: {:.4f}".format(deskew_angle))