-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageToGel.py
105 lines (81 loc) · 3.25 KB
/
imageToGel.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
import PIL.ImageStat
import PIL.Image
import math
import os
def calculateAverage(im, x0, x1, y0, y1):
sumBrightness = 0 #initiate sum
numberofpixels = 0 #initiate number of pixels
rgb_im = im.convert('RGB')
for x in range(x0, x1):
for y in range(y0, y1):
numberofpixels += 1
r,g,b = rgb_im.getpixel((x, y))
sumBrightness+=math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
return sumBrightness/numberofpixels
#find perceived brightnes as a standard
##later could be compared to and decide whether the designated block would be inked or not
def standardBrightness(im):
imgWidth, imgHeight = im.size
return calculateAverage(im, 0, imgWidth, 0, imgHeight)
#find the designated block, check whether its brightness is higher than the standard
## if yes return a true; otherwise false
def processBlock(img, _x, _boxWidth, _y, _boxHeight, _standard):
blockBrightness = calculateAverage( img,
_x * _boxWidth ,
(_x + 1) * _boxWidth -1,
_y * _boxHeight,
(_y + 1) * _boxHeight -1)
# print(_x,",",_y,":",blockBrightness,":",_standard)
#compare with standard
return blockBrightness > _standard
def printImage(image1):
#read image
im = PIL.Image.open(image1)
#initialise things
numOfFalses = 0
total = 20*39
outputArray = [[False] * 20] * 39
#initiate array to prepare storage, in output each block should be true or false
## true means inked; false means not inked
#Parametrisation
imgwidth, imgheight = im.size
boxHeight = imgheight//39
boxWidth = imgwidth//20
#find perceived brightnes as a standard
##later could be compared to and decide whether the designated block would be inked or not
standard = standardBrightness(im)
print("Standard")
print(standard)
# update numOfFalses
for y in range(0, 39):
for x in range(0, 20):
outputArray[y][x] = processBlock(im, x , boxWidth, y, boxHeight, standard) #true or false
if outputArray[y][x] == False:
numOfFalses += 1
numOfTrues = total - numOfFalses
print(numOfFalses)
print(numOfTrues)
#for loop that produces the output array: each block should have its right true or false
#for i,j:
if numOfTrues < numOfFalses:
for y in range(0, 39):
for x in range(0, 20):
outputArray[y][x] = processBlock(im, x , boxWidth, y, boxHeight, standard) #true or false
# print(outputArray[x][y])
if outputArray[y][x] == True:
print("[X]", end = '')
else:
print("[ ]", end = '')
print()
else:
for y in range(0, 39):
for x in range(0, 20):
outputArray[y][x] = processBlock(im, x , boxWidth, y, boxHeight, standard) #true or false
# print(outputArray[x][y])
if outputArray[y][x] == False:
print("[X]", end = '')
else:
print("[ ]", end = '')
print()
# if __name__ == "__main__":
# main(image1)