-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzarr_jpeg2k.py
51 lines (42 loc) · 1.5 KB
/
zarr_jpeg2k.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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 Glencoe Software, Inc. All rights reserved.
#
# This software is distributed under the terms described by the LICENSE.txt
# file you can find at the root of the distribution bundle. If the file is
# missing please request a copy by contacting [email protected]
from numcodecs.abc import Codec
from numcodecs.compat import \
ensure_bytes, \
ensure_contiguous_ndarray, \
ndarray_copy
from numcodecs.registry import register_codec
import imagecodecs
import numpy as np
class jpeg2k(Codec):
"""Codec providing jpeg2k compression via imagecodecs.
Parameters
----------
level : int
Compression level defined by imagecodecs. Relates to peak
signal-to-noise ratio (PSNR). May need to be the reverse of PSNR
(eg. 40 PSNR = compression level 60 | 90 PSNR = compression level 10).
"""
codec_id = "jpeg2k"
def __init__(self, level=50):
self.level = level
assert (self.level > 0 and self.level <= 100
and isinstance(self.level, int))
super().__init__()
def encode(self, buf):
return imagecodecs.jpeg2k_encode(np.squeeze(buf), level=self.level)
def decode(self, buf, out=None):
buf = ensure_bytes(buf)
decoded = imagecodecs.jpeg2k_decode(buf)
if out is not None:
out_view = ensure_contiguous_ndarray(out)
ndarray_copy(decoded, out_view)
else:
out = decoded
return out
register_codec(jpeg2k)