Created
May 26, 2019 06:36
-
-
Save keithrozario/7c66a4edce34abd601397b70547624c6 to your computer and use it in GitHub Desktop.
Example of pytesseract in lambda
This file contains hidden or 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
| """ | |
| remember to set the following two layers | |
| arn:aws:lambda:ap-southeast-1:113088814899:layer:Klayers--tesseract:1 | |
| arn:aws:lambda:ap-southeast-1:113088814899:layer:Klayers-python37-pytesseract:1 | |
| """ | |
| import json | |
| import logging | |
| import pytesseract | |
| from PIL import Image, ImageDraw, ImageFont | |
| logger = logging.getLogger() | |
| logger.setLevel(logging.INFO) | |
| def draw(domain): | |
| """ | |
| draw the text with a given font and size, | |
| returns image as a Image object | |
| """ | |
| logger.info(f"Drawing image for {domain}") | |
| image = Image.new("RGBA", (3000, 50), (255, 255, 255)) | |
| draw = ImageDraw.Draw(image) | |
| draw.text((10, 0), domain, (0, 0, 0)) | |
| return image | |
| def ocr(image): | |
| """ | |
| ocr's the first line on a given image | |
| """ | |
| data = pytesseract.image_to_string(image, lang="eng") | |
| return data | |
| def draw_and_ocr(domain): | |
| image = draw(domain) | |
| data = ocr(image) | |
| result = {"domain": domain, | |
| "ocr": data} | |
| return result | |
| def lambda_handler(event, context): | |
| return draw_and_ocr('keithRozario.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment