-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualize.py
128 lines (97 loc) · 3.78 KB
/
visualize.py
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
""" File Visualizer.
Usage:
visualize.py <filename> [--mmap | --np-mmap | --np-memmap | --np-fullcolor]
Options:
--np-fullcolor Use numpy to extract data in 4-byte chunks and draw in full color
--np-memmap Use numpy but run it with .memmap instead of .fromfile
--mmap Use mmap and regular file i/o instead of numpy fromfile.
--np-mmap Use mmap but stick it in a numpy array and use skimage
--version Show version.
-h --help Show this screen.
"""
from PIL import Image
import mmap
import math
import numpy as np
import struct
from docopt import docopt
from skimage import io
def npfullcolor(path):
"""Show a square image that represents the specified file using numpy.fromfile"""
data = np.memmap(path, dtype=np.uint32, mode='r')
side = math.ceil(math.sqrt(len(data)))
img = Image.new('RGB', (side, side), "black")
pixels = img.load()
for i in range(side):
for j in range(side):
index = i * side + j
if index < len(data):
pixels[i, j] = struct.unpack("BBBB", data[index])
img.show()
def npmemmaprepresent(path):
"""Show a square image that represents the specified file using numpy.fromfile"""
data = np.memmap(path, dtype=np.uint8, mode='r')
side = math.ceil(math.sqrt(len(data)))
img = Image.new('RGB', (side, side), "black")
pixels = img.load()
for i in range(side):
for j in range(side):
index = i * side + j
if index < len(data):
pixels[i, j] = (data[index], data[index], 0)
img.show()
def nprepresent(path):
"""Show a square image that represents the specified file using numpy.fromfile"""
data = np.fromfile(path, dtype=np.uint8)
side = math.ceil(math.sqrt(len(data)))
img = Image.new('RGB', (side, side), "black")
pixels = img.load()
for i in range(side):
for j in range(side):
index = i * side + j
if index < len(data):
pixels[i, j] = (data[index], data[index], 0)
img.show()
def skrepresent(path):
"""Show a square image that represents the file specified starting from an np.zeros array"""
data = None
with open(path, 'rb') as infile:
data = bytearray(mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ))
side = math.ceil(math.sqrt(len(data)))
#img = Image.new('RGB', (side, side), "black")
pixels = np.zeros(shape=(side, side, 3), dtype=np.uint8)
for i in range(side):
for j in range(side):
index = i*side+j
if index < len(data):
pixels[i, j, 0] = data[index]
pixels[i, j, 1] = data[index]
pixels[i, j, 2] = 0
io.imshow(pixels)
io.show()
def represent(path):
"""Show a square image that represents the file specified"""
data = None
with open(path, 'rb') as infile:
data = bytearray(mmap.mmap(infile.fileno(), 0, access=mmap.ACCESS_READ))
side = math.ceil(math.sqrt(len(data)))
img = Image.new('RGB', (side, side), "black")
pixels = img.load()
for i in range(side):
for j in range(side):
index = i*side+j
if index < len(data):
pixels[i, j] = (data[index], data[index], 0)
img.show()
if __name__ == '__main__':
ARGS = docopt(__doc__, version="0.1")
if ARGS['--mmap']:
represent(ARGS['<filename>'])
elif ARGS['--np-mmap']:
skrepresent(ARGS['<filename>'])
elif ARGS['--np-memmap']:
npmemmaprepresent(ARGS['<filename>'])
elif ARGS['--np-fullcolor']:
npfullcolor(ARGS['<filename>'])
else:
nprepresent(ARGS['<filename>'])