-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathimagerec8.py
More file actions
62 lines (53 loc) · 1.88 KB
/
imagerec8.py
File metadata and controls
62 lines (53 loc) · 1.88 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
from PIL import Image
import numpy as np
#import matplotlib
import matplotlib.pyplot as plt
import time
i = Image.open('images/numbers/0.1.png')
iar = np.array(i)
iarl = iar.tolist()
#print iarl
def createExamples():
numberArrayExamples = open('numArEx.txt','a')
numbersWeHave = range(1,10)
for eachNum in numbersWeHave:
#print eachNum
for furtherNum in numbersWeHave:
# you could also literally add it *.1 and have it create
# an actual float, but, since in the end we are going
# to use it as a string, this way will work.
print(str(eachNum)+'.'+str(furtherNum))
#after showing you can print, then do:
imgFilePath = 'images/numbers/'+str(eachNum)+'.'+str(furtherNum)+'.png'
ei = Image.open(imgFilePath)
eiar = np.array(ei)
eiarl = str(eiar.tolist())
print(eiarl)
lineToWrite = str(eachNum)+'::'+eiarl+'\n'
numberArrayExamples.write(lineToWrite)
#time.sleep(55)
def threshold(imageArray):
balanceAr = []
newAr = imageArray
for eachPart in imageArray:
for theParts in eachPart:
#print theParts
avgNum = reduce(lambda x, y: x + y, theParts[:3]) / len(theParts[:3])
balanceAr.append(avgNum)
#time.sleep(3)
balance = reduce(lambda x, y: x + y, balanceAr) / len(balanceAr)
#print balance
for eachRow in newAr:
for eachPix in eachRow:
if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
eachPix[0] = 255
eachPix[1] = 255
eachPix[2] = 255
eachPix[3] = 255
else:
eachPix[0] = 0
eachPix[1] = 0
eachPix[2] = 0
eachPix[3] = 255
return newAr
createExamples()