Created
December 21, 2017 00:22
-
-
Save olooney/4206151902dbd88c7faed88984e66ffc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# apt-get install tesseract-ocr | |
# pip3 install Pillow, pytesseract | |
from PIL import Image, ImageDraw, ImageFont | |
import os, sys | |
import pytesseract as t | |
def round_trip(text, font_size=32): | |
font_filename = '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf' | |
font = ImageFont.truetype(font_filename, size=font_size) | |
text_size = font.getsize(text) | |
img_size = (text_size[0]+20, text_size[1]+20) | |
img = Image.new("RGB", img_size, (255,255,255)) | |
draw = ImageDraw.Draw(img) | |
draw.text( (10,10), text, fill=(0,0,0), font=font ) | |
return t.image_to_string(img) | |
if __name__ == '__main__': | |
text = " ".join(sys.argv[1:]) or "Hello Tesseract!" | |
print(round_trip(text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment