-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut_word.py
More file actions
executable file
·75 lines (65 loc) · 2.62 KB
/
Copy pathcut_word.py
File metadata and controls
executable file
·75 lines (65 loc) · 2.62 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
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
import config
import pytesseract
from PIL import Image, ImageDraw
from Character import ChineseCharacter
import pretreat
def cut_word_with_tesseract(img: Image.Image, font: str) -> list:
# img = img.resize((1000, 1000))
draw = ImageDraw.Draw(img)
print(img.size)
boxes = pytesseract.image_to_boxes(img, lang="chi_sim").split("\n")
print(pytesseract.image_to_string(img, lang="chi_sim"))
chars = []
for b in boxes:
if b == "":
continue
char = ChineseCharacter(b, img, font)
draw.rectangle([(char.x1, char.y1), (char.x2, char.y2)], outline="red")
img.show()
return chars
def cut_word_with_size(img: Image.Image, font: str) -> list:
num_row, num_column = config.DOC_DIM
w, h = img.size
w_cut = w // num_column
h_cut = h // num_row
img_char_list = []
for i_y in range(num_row):
for i_x in range(num_column):
x1 = i_x * w_cut
y1 = i_y * h_cut
x2 = x1 + w_cut
y2 = y1 + h_cut
image = img.crop((x1, y1, x2, y2))
value = pytesseract.image_to_string(image, lang="chi_sim", config="-psm 10")
char = ChineseCharacter(config.FONT_NAME_Sun, value, image)
char.save()
img_char_list.append(char)
return img_char_list
def cut_word_with_size_and_border(image: Image.Image, font: str) -> list:
ret = []
for row in range(1, config.DOC_DIM[1]-1):
for col in range(1, config.DOC_DIM[0]-1):
left = config.BOARDER_SIZE + col*(config.FONT_SIZE+2*config.MARGIN_SIZE)
upper = config.BOARDER_SIZE + row*(config.FONT_SIZE+2*config.MARGIN_SIZE)
tmp = image.crop((left,
upper,
left + config.FONT_SIZE + 2*config.MARGIN_SIZE,
upper + config.FONT_SIZE + 2*config.MARGIN_SIZE))
'''
tmp = ChineseCharacter(font=font,
value=pytesseract.image_to_string(tmp, lang="chi_sim", config="-psm 10"),
image=tmp)
'''
# tmp.show()
# tmp.save()
cut_word_with_size_and_border.count += 1
tmp.save("result/{}/{}.png".format(font, cut_word_with_size_and_border.count))
ret.append(tmp)
return ret
if __name__ == "__main__":
img = Image.open("data/HuaWenSun/IMG_20190514_020134.jpg")
# img = pretreat(img)
# img = pretreat.crop_image(img)
img.show()
cut_word_with_size_and_border.count = 0
img_list = cut_word_with_size_and_border(img, config.FONT_NAME_HuaWen)