forked from jakub-maly/FoodFriend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.py
More file actions
33 lines (25 loc) · 1.02 KB
/
ocr.py
File metadata and controls
33 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pytesseract as ocr
from PIL import Image, ImageEnhance
def image_to_text(filepath: str) -> str:
""" Converts image to a string using optical character recognition (OCR).
:param filepath: filepath of the image
:return: string contents of the image
"""
return ocr.image_to_string(filepath)
def enhance_image(filepath: str, contrast: float = 2.0, sharpness: float = 2.0) -> str:
""" Enhances the image to be easier to OCR.
:param filepath: filepath of the image
:param contrast: contrast ratio (default 2.0)
:param sharpness: sharpness ratio (default 2.0)
:return: image filepath
"""
image = Image.open(filepath)
# convert to greyscale
image = image.convert('L')
# enhance image and contrast
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(contrast)
enhancer = ImageEnhance.Sharpness(image)
image = enhancer.enhance(sharpness)
image.save('enhanced_image.png', "PNG")
return filepath