-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_image.py
More file actions
128 lines (97 loc) · 2.68 KB
/
Copy pathread_image.py
File metadata and controls
128 lines (97 loc) · 2.68 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import math
filename = "24bit.bmp"
f = open(filename, "rb")
f.read(2)
size = int.from_bytes(f.read(4),byteorder='little')
print('size', size)
f.read(4)
starting_address = f.read(4)
offset = int.from_bytes(starting_address, byteorder='little')
f.read(4)
width = int.from_bytes(f.read(4), byteorder='little')
height = int.from_bytes(f.read(4), byteorder='little')
print('width', width)
print('height', height)
pixelByteSize = 3 #only treats 24-bit depth images
f.seek(0)
allData = f.read()
print('total data', len(allData))
print('offset', offset)
print('image data', len(allData) - offset)
f.seek(0)
header = f.read(offset)#+b''.join(b'\x00' for i in range(size-(height*width*pixelByteSize+offset)))
print(offset + (height)*width*pixelByteSize)#(width)*pixelByteSize +
f.close()
def getPixel(imgData, x, y):
pos = offset + x*pixelByteSize + y*width*pixelByteSize
rgbPixel = []
for i in range(0, pixelByteSize):
rgbPixel.append(imgData[pos:pos+3][2-i])
return rgbPixel
pixelArray = []
for j in range(height):
pixelArray.append([])
#i = 0
for i in range(width):
pixelArray[-1].append(getPixel(allData, i, j))
def getBrightness(rgbPixel):
return (rgbPixel[0]+rgbPixel[1]+rgbPixel[2])/3 #average conversion
brightnessArray = []
for j in range(height):
brightnessArray.append([])
i = 0
for i in range(width):
val = getBrightness(pixelArray[j][i])
brightnessArray[-1].append(val)
def extendArray(array):
array.insert(0, [])
array.append([])
array[0] = array[1][:]
array[-1] = array[-2][:]
for line in array:
line.insert(0, line[0])
line.append(line[-1])
extendArray(brightnessArray)
postArray = []
def transformation(array, x, y):
lines = array[y-1:y+2]
a = 0
b = 0
xTotal = 0
yTotal = 0
lines = map(lambda l: l[x-1:x+2], lines)
for line in lines:
a = 0
for n in line:
xTotal += ((a-1)*((b%2)+1))*n
yTotal += ((b-1)*((a%2)+1))*n
a += 1
b += 1
return math.sqrt(xTotal**2+yTotal**2)
def tranformArray(pre, post, height, width):
for y in range(1, height + 1):
post.append([])
x = 0
for x in range(1, width + 1):
if(transformation(pre, x, y) > 100):
post[-1].append(1)
else:
post[-1].append(0)
tranformArray(brightnessArray, postArray, height, width)
final = []
def binaryToRGB(pre, final):
for y in range(0, height):
final.append([])
x = 0
for x in range(0, width):
if(pre[y][x] == 1):
final[-1].append(b'\xff\xff\xff')
else:
final[-1].append(b'\x00\x00\x00')
binaryToRGB(postArray, final)
final = list(map(lambda x: b''.join(x), final))
final = b''.join(final)+b''.join(b'\x00' for i in range(size-(height*width*pixelByteSize+offset)))
final = header+final
output = open("edges_"+filename, 'wb')
output.write(final)
output.close()