-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFingerprintBitmapHeader.py
More file actions
61 lines (43 loc) · 1.91 KB
/
FingerprintBitmapHeader.py
File metadata and controls
61 lines (43 loc) · 1.91 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
# Taken from Brian Ejike (2022)
import requests
from flask import *
import serial, time, argparse, struct
IMAGE_WIDTH = 256
IMAGE_HEIGHT = 288
IMAGE_DEPTH = 8
IMAGE_START_SIGNATURE = b'\xAA'
# Assemble BMP header for a grayscale image
def assembleBMPHeader(width, height, depth, includePalette=False):
# Define the formats of the header and palette entries
# See https://gibberlings3.github.io/iesdp/file_formats/ie_formats/bmp.htm for details.
bmpHeader = struct.Struct("<2s3L LLl2H6L")
bmpPaletteEntry = struct.Struct("4B")
# width of the image raster in bytes, including any padding
byteWidth = ((depth * width + 31) // 32) * 4
numColours = 2 ** depth
bmpPaletteSize = bmpPaletteEntry.size * numColours
imageSize = byteWidth * height
if includePalette:
fileSize = bmpHeader.size + bmpPaletteSize + imageSize
rasterOffset = bmpHeader.size + bmpPaletteSize
else:
fileSize = bmpHeader.size + imageSize
rasterOffset = bmpHeader.size
BMP_INFOHEADER_SZ = 40
TYPICAL_PIXELS_PER_METER = 2835 # 72 DPI, boiler-plate
# Pack the BMP header
# Height is negative because the image is received top to bottom and will be similarly stored
bmpHeaderBytes = bmpHeader.pack(b"BM", fileSize, 0, rasterOffset,
BMP_INFOHEADER_SZ, width, -height, 1, depth,
0, imageSize, TYPICAL_PIXELS_PER_METER, TYPICAL_PIXELS_PER_METER, 0, 0)
# Pack the palette, if needed
if includePalette:
bmpPaletteBytes = []
# Equal measures of each colour component yields a scale
# of grays, from black to white
for index in range(numColours):
R = G = B = A = index
bmpPaletteBytes.append(bmpPaletteEntry.pack(R, G, B, A))
bmpPaletteBytes = b''.join(bmpPaletteBytes)
return bmpHeaderBytes + bmpPaletteBytes
return bmpHeaderBytes