forked from heye/scan_cam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
92 lines (66 loc) · 2.44 KB
/
Copy pathconvert.py
File metadata and controls
92 lines (66 loc) · 2.44 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
from PIL import Image
def main():
image_height = 0 # will be determined by the datta
image_width = 3000
#image_width = 384
#print file size
file = open("values.bin", "rb")
if not file:
print("cannot open file")
file_buf = file.read()
print("file size: " + str(len(file_buf)))
file.close()
#partially read file into buffer
image_buf = bytearray()
with open('values.bin', 'rb') as file:
pixel_data = file.read(2)
pixel_data_trash = file.read(2)
line_number = 0
pixel_number = 0
line_pixel_number = 0
dark_value = 11000
light_value = 4500
skip_pixels = 3825/image_width
while pixel_data:
#pixel = int.from_bytes(pixel_data, "little", signed=True)
pixel_number += 1
if pixel_number >= 3825:
print("pixel in line " + str(line_number) + ": " + str(line_pixel_number))
pixel_number = 0
line_number += 1
line_pixel_number = 0
if pixel_number > skip_pixels*line_pixel_number:
line_pixel_number += 1
#print(pixel_data_h)
pixel = int.from_bytes(pixel_data, byteorder='little')
#print(pixel)
if pixel < light_value:
pixel = light_value
if pixel > dark_value:
pixel = dark_value
pixel -= light_value
pixel = (dark_value-light_value) - pixel
#range/256
pixel = int(pixel/((dark_value-light_value)/255))
if line_pixel_number <= image_width:
image_buf.append(pixel)
pixel_data = file.read(2)
pixel_data_trash = file.read(2)
image_height = line_number
file.close()
print("HEIGHT: " + str(image_height))
print("WIDTH: " + str(image_width))
#TODO: open binary file, read into buffer
buf = bytearray()
for y in range(0,256):
for x in range(0,256):
buf.append(y)
#write buffer into file object
image = Image.frombuffer('L', (image_width,image_height), image_buf)
image = image.transpose(Image.FLIP_LEFT_RIGHT)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
#image = Image.frombuffer('L', (256,256), buf)
file = open("hello.bmp", "wb")
image.save(file, 'BMP')
if __name__ == "__main__":
main()