-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathby_tone.py
54 lines (43 loc) · 1.35 KB
/
by_tone.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
import numpy as np
from PIL import Image
import sys, os
from numba import njit
from config import *
path_to_img = sys.argv[1] if len(sys.argv) != 1 else input('Path to image: ')
width = int(input('Width: '))
print('Processing...')
img = Image.open(path_to_img)
# Rescale image for symbols patterns
img = img.resize((width*PW, int(img.size[1]/img.size[0] * width / symbol_aspect) * PH))
pix = np.asarray(img.convert('L'))
H, W = pix.shape[0], pix.shape[1]
# Getting tone of every symbol by average brightness
symb_dir = 'symbols_img/'
tones = []
for i in range(95):
p = np.asarray(Image.open(symb_dir + str(i) + '.png').convert('L'))
tones.append(p.sum() / PW / PH)
# Normalising symbols tones
tones -= min(tones)
tones *= 255/max(tones)
tones = np.array(tones)
@njit
def check_pattern(pat):
# Search for the nearest symbol by its tone
ps = pat.sum() / len(pat) / len(pat[0])
it = 0; min_d = 256
for i in range(len(tones)):
d = abs(tones[i] - ps)
if d < min_d:
min_d = d; it = i
return it
f = open('res.txt', 'w')
# Traversing the image by sectors the size of the symbol image and writing result
for y in range(PH, H, PH):
for x in range(PW, W, PW):
part = pix[y-PH:y, x-PW:x]
r = check_pattern(part)
f.write(gradient[r])
f.write('\n')
print(round(y / H * 100, 1))
f.close()