-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processing.py
113 lines (89 loc) · 2.8 KB
/
image_processing.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
'''image processing to make it conform to mnist format'''
#%%
import tkinter as tk
from tkinter import filedialog
import numpy as np
from PIL import Image
from mnist_loader import LabeledImage
YES = {'Y','y','YES','yes','Yes'}
NO = {'N','n','NO','no','No'}
def process(filename, label=None):
'''Image processing procedure'''
# open image
img = Image.open(filename)
img = img.convert('L')
width, height = img.size
while True: # rotate
new_img = img.copy()
new_mat = new_img.load()
new_img.show()
if input('rotate? (y/n) ') in YES:
deg = _int_input('how many turns? (clockwise) ')
new_img = img.rotate(-deg*90)
if _is_fine(new_img):
break
else:
break
img = new_img
while True: # to BW
new_img = img.copy()
new_mat = new_img.load()
thresh = _int_input('Choose BW sensitivity (0-100): ')
thresh = thresh/100 * 255
for _y in range(height):
for _x in range(width):
new_mat[_x,_y] = 0 if new_mat[_x,_y] > thresh else 255
if _is_fine(new_img):
break
new_img = crop(new_img)
# resize
new_img = new_img.resize((28,28), 1)
# to labled image
new_mat = new_img.load()
img_array = np.array([[new_mat[x,y] for x in range(28)]for y in range(28)])
result = LabeledImage(img_array, label)
return result
def crop(img, pad = 0.2):
''' Takes a BW image of a digit on a white background and crops it to a square '''
mat = img.load()
_w,_h = img.size
top = left = np.inf
bot = right = 0
# find digit bounds
for _x in range(_w):
for _y in range (_h):
if mat[_x,_y] == 255:
left = min(_x,left)
right = max(_x,right)
top = min(_y,top)
bot = max(_y,bot)
length = max(right - left, bot - top) # square crop length
pad = pad * length
cropped = img.crop((
left - pad,
top - pad,
left + length + pad,
top + length + pad))
return cropped
def _int_input(prompt):
while True:
try:
res = int(input(prompt))
break
except ValueError:
print('input is not a number')
continue
return res
def _is_fine(img):
img.show()
check = input('is the processing fine? (y/n) ')
return check in YES
def load_image():
'''load image using a dialogue box'''
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
img = process(file_path)
if img:
return img
return load_image()