-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageutil2.py
190 lines (139 loc) · 5.72 KB
/
imageutil2.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy as np
from PIL import Image, ImageChops, ImageDraw, ImageFont
from fontTools.ttLib import TTFont
import matplotlib.pyplot as plt
import cv2
def trim(img,tol=255*0.95):
# img is image data
# tol is tolerance
mask = img<tol
return img[np.ix_(mask.any(1),mask.any(0))]
def readimage(img_path):
# read image as black and white
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
# fall back option for Windows since
# cv2 doesn't work with Unicode path
if img is None:
img = Image.open(img_path).convert('L')
img = np.array(img)
return img
def binarise(img):
# binarisation
_, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
return img
def invertimage(img):
return 255 - img
def padtosquare(img, padding_ratio=0):
# get image size and find the longer dimension
# we will pad in the other dimension to make
# a perfect square (since img is a matrix, it shape is row x col)
h,w = img.shape
max_dim = max(w,h)
# add padding border
max_dim = int(max_dim*(1+padding_ratio))
x_space = max_dim - w
y_space = max_dim - h
# since int() convertion always round down, we
# add 1 to left or top if the needed number of
# pixels is in odd number
x_is_even = (x_space % 2 == 0)
y_is_even = (y_space % 2 == 0)
left = int(x_space/2) + (0 if x_is_even else 1)
right = int(x_space/2)
top = int(y_space/2) + (0 if y_is_even else 1)
bottom = int(y_space/2)
img = cv2.copyMakeBorder(img, top, bottom, left, right, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
return img
def resize(img, size):
return cv2.resize(img, size)
def morph_open(img, kernel_size):
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, kernel_size)
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
return img
def reshape_for_keras(img):
""" reshape the numpy array to the same format as our model """
return img.reshape(1, 1, img.shape[0], img.shape[1]).astype('float32')
def char_img(char, size=(70,70)):
w = size[0]
h = size[1]
# create a white width*height image
img = Image.new(mode='L', size=size, color=255)
d = ImageDraw.Draw(img)
# we want to find the size in pt which fit exactly in our canvas
# we start with an estimate (the number here came from trial and error)
size_in_pt = int(min(w,h)*1.7)
fnt = ImageFont.truetype("Norasi.ttf", size_in_pt)
text_width, text_height = fnt.getsize(char)
fnt_offset_x, fnt_offset_y = fnt.getoffset(char)
# caculate where to place image so it is centered
img_x = (w - text_width - fnt_offset_x)/2
img_y = (h - text_height - fnt_offset_y)/2
# draw text on the image, starting at (img_x, img_y) top-left
d.text((img_x, img_y), char, font=fnt, fill=0)
return img
def chars_from_font(chars, font_path, size):
w = size[0]
h = size[1]
char_imgs = []
try:
for char in chars:
# create a white width*height image
img = Image.new(mode='L', size=size, color=255)
d = ImageDraw.Draw(img)
# we want to find the size in pt which fit exactly in our canvas
# we start with an estimate (the number here came from trial and error)
size_in_pt = int(min(w,h)*1.2)
while True:
# we scan up until it overflow
_fnt = ImageFont.truetype(font_path, size=size_in_pt)
text_width, text_height = _fnt.getsize(char)
fnt_offset_x, fnt_offset_y = _fnt.getoffset(char)
if text_width-fnt_offset_x > w or text_height-fnt_offset_y > h:
break
# the rate here can be quite fast as we will do
# a precious scan down later
size_in_pt += min(int(max(w,h)*0.5), 100)
while True:
# now keep reducing the size slowly until we reach
# a size that do not overflow
_fnt = ImageFont.truetype(font_path, size=size_in_pt)
text_width, text_height = _fnt.getsize(char)
fnt_offset_x, fnt_offset_y = _fnt.getoffset(char)
if text_width-fnt_offset_x <= w and text_height-fnt_offset_y <= h:
break
size_in_pt -= 1
fnt = ImageFont.truetype(font_path, size=size_in_pt)
text_width, text_height = fnt.getsize(char)
fnt_offset_x, fnt_offset_y = fnt.getoffset(char)
# caculate where to place image so it is centered
img_x = (w - text_width - fnt_offset_x)/2
img_y = (h - text_height - fnt_offset_y)/2
# draw text on the image, starting at (img_x, img_y) top-left
d.text((img_x, img_y), char, font=fnt, fill=0)
char_imgs.append(np.array(img))
except OSError as e:
print("problem openning {}".format(font_path))
return None
# convert to a numpy array then return
return np.array(char_imgs)
def showimage(img):
plt.imshow(img, cmap=plt.get_cmap('gray'))
plt.show()
plt.close()
if __name__ == '__main__':
# for i in range(ord('ก'), ord('ฮ')):
# plt.subplot(5,10,i+1-ord('ก'))
# img = char_from_font(chr(i), 'fonts/4711_AtNoon_Traditional.ttf', size=(56, 56))
# # img = binarise(img)
# plt.imshow(img, cmap=plt.get_cmap('gray'))
for i in range(1,9):
plt.subplot(2,4,i)
img = readimage('th_samples/ก/im12_3.jpg')
# img = binarise(img)
img = trim(img)
img = padtosquare(img)
img = resize(img, (56, 56))
img = morph_open(img, (1<<2, 1<<2))
plt.imshow(img, cmap=plt.get_cmap('gray'))
plt.show()
plt.close()