This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.py
90 lines (80 loc) · 2.66 KB
/
example.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
import logging
import numpy as np
from prototype_jpeg import compress, extract
from prototype_jpeg.utils import show_raw_images, psnr
logging.basicConfig(level=logging.INFO)
def example():
specs = ({
'fn': 'tests/images/rgb/Baboon.raw',
'size': (512, 512),
'grey_level': False,
'quality': 50,
'subsampling_mode': 1
}, {
'fn': 'tests/images/rgb/Lena.raw',
'size': (512, 512),
'grey_level': False,
'quality': 50,
'subsampling_mode': 1
}, {
'fn': 'tests/images/rgb/Baboon.raw',
'size': (512, 512),
'grey_level': False,
'quality': 5,
'subsampling_mode': 1
}, {
'fn': 'tests/images/rgb/Lena.raw',
'size': (512, 512),
'grey_level': False,
'quality': 5,
'subsampling_mode': 1
}, {
'fn': 'tests/images/grey_level/Baboon.raw',
'size': (512, 512),
'grey_level': True,
'quality': 50,
'subsampling_mode': 1
}, {
'fn': 'tests/images/grey_level/Lena.raw',
'size': (512, 512),
'grey_level': True,
'quality': 50,
'subsampling_mode': 1
})
for spec in specs:
with open(spec['fn'], 'rb') as raw_file:
original = np.fromfile(raw_file, dtype=np.uint8)
raw_file.seek(0)
compressed = compress(
raw_file,
size=spec['size'],
grey_level=spec['grey_level'],
quality=spec['quality'],
subsampling_mode=spec['subsampling_mode']
)
with open('compressed.protojpg', 'wb') as compressed_file:
compressed['data'].tofile(compressed_file)
header = compressed['header'] # get compressed header (metadata)
with open('compressed.protojpg', 'rb') as compressed_file:
extracted = extract(
compressed_file,
header={
'size': header['size'],
'grey_level': header['grey_level'],
'quality': header['quality'],
'subsampling_mode': header['subsampling_mode'],
'remaining_bits_length': header['remaining_bits_length'],
'data_slice_lengths': header['data_slice_lengths']
}
)
logging.getLogger(__name__).info(
'PSNR: %.4f', psnr(original, extracted)
)
show_raw_images(
(original, extracted),
(spec['size'], spec['size']),
(spec['fn'], 'Compressed and Extracted'),
grey_level=spec['grey_level']
)
if __name__ == '__main__':
example()