-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbgrer.py
More file actions
112 lines (105 loc) · 3.54 KB
/
bgrer.py
File metadata and controls
112 lines (105 loc) · 3.54 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
import sys
import os
def read_args(argv):
output = {}
i = 1
while i < len(argv):
if argv[i] == '-f' and len(argv) > i + 1:
output['filepath'] = argv[i + 1]
i += 2
elif argv[i] == '-i' and len(argv) > i + 1:
output['info'] = argv[i + 1]
i += 2
else:
i += 1
return output
def process_path(path):
if not os.path.exists(path):
return None
else:
binary_data = None
try:
with open(path, 'rb') as f:
binary_data = bytearray(f.read())
except Exception as e:
print("Error reading file: {0}".format(str(e)))
finally:
return binary_data
def read_type_file(data):
if len(data) >= 8 and data[:8] == b'\x89PNG\r\n\x1a\n':
return 'png'
else:
return None
def binary_to_png(data,info):
width = int.from_bytes(data[16:20])
height = int.from_bytes(data[20:24])
color_depht = data[24]
if info == 'general':
color_type = data[25]
compression_methpd = data[26]
filter_method = data[27]
interlace_method = data[28]
palette_red = data[29]
palette_green = data[30]
palette_blue = data[31]
print("WIDTH: ",width)
print("HEIGHT: ",height)
print("COLOR DEPTH: ", color_depht)
print("COLOR TYPE: ", color_type)
print("COMPRESSION METHOD: ",compression_methpd)
print("FILTER METHOD: ",filter_method)
print("INTERLACE METHOD: ", interlace_method)
print("PALETTE RED: ", palette_red)
print("PALETTE GREEN: ", palette_green)
print("PALETTE BLUE: ", palette_blue)
else:
offset = 33
img = bytearray()
while offset < len(data):
length = int.from_bytes(data[offset:offset + 4], 'big')
chunk_type = data[offset + 4: offset + 8]
if chunk_type == b'IDAT':
img.extend(data[offset + 8:offset + 8 + length])
offset += 12 + length
img_matrix = []
offset = 0
for row in range(height):
row_pixel = []
for col in range(width):
if color_depht == 8 and len(img) >= offset + 3:
r = img[offset]
g = img[offset + 1]
b = img[offset + 2]
row_pixel.append((r,g,b))
offset += 3
img_matrix.append(row_pixel)
return img_matrix
def main():
args = read_args(sys.argv)
if 'filepath' not in args:
filepath = None
print("Not filepath found")
sys.exit(1)
else:
#information_type = 'pixels'
if 'info' not in args:
print("Not info type provided")
sys.exit(1)
else:
if args['info'] != 'general' and args['info'] != 'data':
print("Invalid info type (enter 'general' or 'data')")
sys.exit(1)
else:
binary_data = process_path(args['filepath'])
#print("{0}".format(binary_data))
if binary_data is None:
print("Binary data not found")
sys.exit(1)
file_type = read_type_file(binary_data)
if(file_type == 'png'):
#print("Es un png")
image_matrix = binary_to_png(binary_data,args['info'])
if args['info'] == 'data':
print(image_matrix)
if __name__ == '__main__':
main()