-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_parser.py
More file actions
83 lines (69 loc) · 2.32 KB
/
Copy pathimage_parser.py
File metadata and controls
83 lines (69 loc) · 2.32 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
76
77
78
79
80
81
82
83
#-*- coding: utf-8 -*-
from PIL import Image
import numpy as np
import scipy.misc
# image read
original_image = Image.open('original6.png')
# pixeling, array
original_array = np.array(original_image)
original_pixels = original_image.load()
# divide image to top and bottom row -> abc.. & ABC..
firstRow = False
noBlack = True
middle = -1;
# background color with white, alphabet with red
for j in range(original_array.shape[0]):
for i in range(original_array.shape[1]):
if original_pixels[i, j][0] + original_pixels[i, j][1] + original_pixels[i, j][2] < 360:
original_pixels[i, j] = (0, 0, 0)
firstRow = True
noBlack = False
else:
original_pixels[i, j] = (255, 255, 255)
if (middle < 0 and firstRow and noBlack):
middle = j+1
noBlack = True
print (original_array.shape[0])
# modified image with black, white background
scipy.misc.imsave("modified.png", original_image)
modified_image = Image.open("modified.png")
modified_array = np.array(modified_image)
modified_pixels = modified_image.load()
# alphabet parsing
print (modified_array.shape[0])
print (modified_array.shape[1])
print (middle)
isDetected = False
height = [0,middle,modified_array.shape[0]]
width = modified_array.shape[1]
capital_or_not = ['0', 'c']
for t in range(2):
num_of_white = 0
ver_min = 10000
ver_max = 0
horizontal_min = 10000
horizontal_max = 0
index = 0
for i in range(width):
for j in range(height[t],height[t+1]):
if modified_pixels[i, j][1] == 0:
isDetected = True
ver_min = min(ver_min, j)
ver_max = max(ver_max, j)
horizontal_min = min(horizontal_min, i)
horizontal_max = max(horizontal_max, i)
else:
num_of_white = num_of_white + 1
if (num_of_white == height[t+1]-height[t]) and isDetected:
img = modified_array[ver_min:ver_max, horizontal_min:horizontal_max]
dir = chr(97+index)+capital_or_not[t]+'.png'
dir = dir.lower()
scipy.misc.imsave(dir, img)
index = index + 1
isDetected = False
num_of_white = 0
ver_min = 10000
ver_max = 0
horizontal_min = 10000
horizontal_max = 0
num_of_white = 0