Skip to content

Commit 4609d23

Browse files
committed
add imagecodec example
1 parent 8566a8f commit 4609d23

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

examples/image_codec.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "zarr @ git+https://github.com/zarr-developers/zarr-python.git@main",
5+
# "imagecodecs==2025.3.30",
6+
# "pytest==8.4.2"
7+
# ]
8+
# ///
9+
10+
from typing import Literal
11+
12+
import numcodecs
13+
import numpy as np
14+
import pytest
15+
from imagecodecs.numcodecs import Jpeg
16+
17+
import zarr
18+
from zarr.core.common import ZarrFormat
19+
20+
numcodecs.register_codec(Jpeg)
21+
jpg_codec = Jpeg()
22+
23+
24+
@pytest.mark.parametrize("zarr_format", ZarrFormat)
25+
def test(zarr_format: Literal[2, 3]) -> None:
26+
store = {}
27+
if zarr_format == 2:
28+
z_w = zarr.create_array(
29+
store=store,
30+
data=np.zeros((100, 100, 3), dtype=np.uint8),
31+
compressors=jpg_codec,
32+
zarr_format=zarr_format,
33+
)
34+
else:
35+
z_w = zarr.create_array(
36+
store=store,
37+
data=np.zeros((100, 100, 3), dtype=np.uint8),
38+
serializer=jpg_codec,
39+
zarr_format=zarr_format,
40+
)
41+
z_w[:] = 2
42+
z_r = zarr.open_array(store=store, zarr_format=zarr_format)
43+
assert np.all(z_r[:] == 2)
44+
if zarr_format == 2:
45+
print(z_r.metadata.to_dict()["compressor"])
46+
else:
47+
print(z_r.metadata.to_dict()["codecs"])
48+
49+
50+
if __name__ == "__main__":
51+
pytest.main([__file__, f"-c {__file__}", "-s"])

0 commit comments

Comments
 (0)