-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
32 lines (28 loc) · 967 Bytes
/
util.py
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
from PIL import Image
import cv2
import os
import numpy as np
from pathlib import Path
def resizeHeight(wsize, image):
ratio = min(wsize[0]/image.size[0], wsize[1]/image.size[1])
return image.resize((int(image.size[0]*ratio), int(image.size[1]*ratio)))
def fixString(text):
text = text.replace('〈', 'く')
text = text.replace(' ', '')
text = text.replace(':', '.')
return text
def dilateImage(img):
img = np.array(img)
closek = np.ones((2,2),np.uint8)
dilation = cv2.dilate(img,closek,iterations = 1)
kernel = np.ones((27,27),np.uint8)
erosion = cv2.erode(dilation,kernel,iterations = 1)
img = Image.fromarray(erosion)
return img
def findLangs():
if 'TESSDATA_PREFIX' in os.environ:
data = Path(os.environ['TESSDATA_PREFIX'])
langs = list(data.glob('*.traineddata'))
return [lang.name.replace(lang.suffix, '') for lang in langs]
else:
return ('eng', 'jpn', 'jpn_vert')