Advertisement
zdenop

Fix image rotation with tesseract

Aug 5th, 2015
5,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. """ Fix image rotation with tesseract and dpi information
  2. """
  3.  
  4. import os
  5. import subprocess
  6. import PIL.Image as Image
  7.  
  8. from glob import glob
  9.  
  10. command = 'c:\\Share\\tesseract.exe'
  11. image = '337.jpg'
  12. DPI = 300
  13. arguments = ' %s - -psm 0'
  14.  
  15.  
  16. def get_rotation_info(filename):
  17.     stdoutdata = subprocess.getoutput(command + arguments % filename)
  18.     degrees = None
  19.     for line in stdoutdata.splitlines():
  20.         info = 'Orientation in degrees: '
  21.         if info in line:
  22.             degrees = -float(line.replace(info, '').strip())
  23.             #print("Found rotation: %.2f" % degrees)
  24.     return degrees
  25.  
  26. def fix_dpi_and_rotation(filename, degrees, dpi_info):
  27.     im1 = Image.open(filename)
  28.     print('Fixing rotation %.2f in %s...' % (degrees, filename))
  29.     im1.rotate(degrees).save('../%s' % filename,
  30.                              'JPEG', quality=97, dpi = (dpi_info, dpi_info))
  31.  
  32. filenames = sorted(glob('*.jpg'))
  33. for filename in filenames:
  34.     print('Checking %s...' % filename)
  35.     degrees = get_rotation_info(filename)
  36.     if degrees:
  37.         fix_dpi_and_rotation(filename, degrees, DPI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement