Skip to content

Instantly share code, notes, and snippets.

@scionoftech
Created September 10, 2019 13:12
Show Gist options
  • Save scionoftech/43b4dbe9e822411787e08d59d30f5368 to your computer and use it in GitHub Desktop.
Save scionoftech/43b4dbe9e822411787e08d59d30f5368 to your computer and use it in GitHub Desktop.
This python script rotates upside down text image to proper image
from PIL import Image as pillowimg
import numpy as np
import cv2
import pytesseract
# PIL image
def check_orientation(img):
newdata = pytesseract.image_to_osd(np.array(img))
orgi_orientation = re.search('(?<=Rotate: )\d+', newdata).group(0)
# check image orientation
if orgi_orientation != 0:
return rotate(img)
else:
return img
# pip install opevcv-python==4.1.0.25 (this functionality works only in v4.1.0.25 )
def rotate(image, center=None, scale=1.0):
angle = 360 - int(re.search('(?<=Rotate: )\d+', pytesseract.image_to_osd(image)).group(0))
(h, w) = image.shape[:2]
if center is None:
center = (w / 2, h / 2)
# Perform the rotation
mmm = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, mmm, (w, h))
return rotated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment