-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadpixel.py
More file actions
45 lines (40 loc) · 1.95 KB
/
readpixel.py
File metadata and controls
45 lines (40 loc) · 1.95 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
def bytes_to_i(mbytes, byteorder='little'):
return int.from_bytes(mbytes, byteorder)
def i_to_bytes(number, length, byteorder='little'):
return number.to_bytes(length, byteorder)
def readpixelfunction(image):
file = open(image, "rb")
#BmpFileHeader
bfType = bytes_to_i(file.read(2))
bfSize = bytes_to_i(file.read(4))
bfReserved1 = bytes_to_i(file.read(2))
bfReserved2 = bytes_to_i(file.read(2))
bfOffBits = bytes_to_i(file.read(4))
# BmpStructHeader
biSize = bytes_to_i(file.read(4))
biWidth = bytes_to_i(file.read(4)) #picture width
biHeight = bytes_to_i(file.read(4)) #picture height
biPlanes = bytes_to_i(file.read(2)) #default 1
biBitCount = bytes_to_i(file.read(2)) #one pixel occupy how many bits 24
biCompression = bytes_to_i(file.read(4)) #whether compression
biSizeImage = bytes_to_i(file.read(4)) #picture size
biXPelsPerMeter = bytes_to_i(file.read(4)) #x fenbianlv
biYPelsPerMeter = bytes_to_i(file.read(4)) #y fenbianlv
biClrUsed = bytes_to_i(file.read(4))
biClrImportant = bytes_to_i(file.read(4))
bit_count = biBitCount // 8
__bitSize = biWidth * biHeight
headermessages = {'bfType':bfType, 'bfSize':bfSize, 'bfReserved1':bfReserved1, 'bfReserved2':bfReserved2, 'bfOffBits':bfOffBits, 'biSize':biSize, 'biWidth':biWidth, 'biHeight':biHeight, 'biPlanes':biPlanes, 'biBitCount':biBitCount, 'biCompression':biCompression, 'biSizeImage':biSizeImage, 'biXPelsPerMeter':biXPelsPerMeter, 'biYPelsPerMeter':biYPelsPerMeter, 'biClrUsed':biClrUsed, 'biClrImportant':biClrImportant}
# pixels lists
count = 0
bits = []
while count < __bitSize:
bit_count1 = 0
key = 0
while bit_count1 < bit_count:
key = bytes_to_i(file.read(1))
bits.append(key)
bit_count1 += 1
count += 1
file.close()
return headermessages, bits