|
11 | 11 | Script for exporting a series of images into a Blosc2 HDF5 file using the Grok codec. |
12 | 12 | """ |
13 | 13 |
|
| 14 | +import os |
| 15 | + |
| 16 | +import blosc2 |
14 | 17 | import blosc2_grok |
15 | 18 | import h5py |
16 | 19 | import hdf5plugin |
17 | 20 | import numpy as np |
18 | 21 | from PIL import Image |
19 | | -import os |
20 | | - |
21 | | -import blosc2 |
22 | 22 |
|
23 | | -if __name__ == '__main__': |
| 23 | +if __name__ == "__main__": |
24 | 24 | # Define the compression and decompression parameters. Disable the filters and the |
25 | 25 | # splitmode, because these don't work with the codec. |
26 | 26 | cparams = { |
27 | | - 'codec': blosc2.Codec.GROK, |
28 | | - 'nthreads': 1, |
29 | | - 'filters': [], |
30 | | - 'splitmode': blosc2.SplitMode.NEVER_SPLIT, |
| 27 | + "codec": blosc2.Codec.GROK, |
| 28 | + "nthreads": 1, |
| 29 | + "filters": [], |
| 30 | + "splitmode": blosc2.SplitMode.NEVER_SPLIT, |
31 | 31 | } |
32 | 32 | # Set the parameters that will be used by grok |
33 | 33 | kwargs = { |
34 | | - 'cod_format': blosc2_grok.GrkFileFmt.GRK_FMT_JP2, |
35 | | - 'num_threads': 1, # this does not have any effect (grok should work in multithreading mode) |
36 | | - 'quality_mode': "rates", |
| 34 | + "cod_format": blosc2_grok.GrkFileFmt.GRK_FMT_JP2, |
| 35 | + "num_threads": 1, # this does not have any effect (grok should work in multithreading mode) |
| 36 | + "quality_mode": "rates", |
37 | 37 | } |
38 | 38 |
|
39 | 39 | # Compression params for identifying the codec. In the future, one should be able to |
40 | 40 | # specify the grok plugin (and its parameters) here. |
41 | 41 | b2params = hdf5plugin.Blosc2() |
42 | 42 |
|
43 | 43 | # Open the PNG datasets |
44 | | - dir_path = 'numbers' |
| 44 | + dir_path = "numbers" |
45 | 45 | # List of PNG files (in order) |
46 | 46 | png_files = [ |
47 | 47 | "zero.png", |
|
62 | 62 | img_color = Image.open(os.path.join(dir_path, png_file)) |
63 | 63 | images_color.append(np.array(img_color)) |
64 | 64 | # Convert the image to grayscale |
65 | | - img_gray = img_color.convert('L') |
| 65 | + img_gray = img_color.convert("L") |
66 | 66 | images_gray.append(np.array(img_gray)) |
67 | 67 | dset_color = np.array(images_color) |
68 | 68 | dset_gray = np.array(images_gray) |
69 | 69 |
|
70 | 70 | # Open the output file for color and gray images |
71 | | - fname = f'numbers-jpeg2000.h5' |
| 71 | + fname = "numbers-jpeg2000.h5" |
72 | 72 | print(f"output file: {fname}") |
73 | | - fout = h5py.File(fname, 'w') |
| 73 | + fout = h5py.File(fname, "w") |
74 | 74 |
|
75 | 75 | for cratio in (2, 5, 10, 20): |
76 | 76 | print(f"Compressing with cratio={cratio}x ...") |
77 | | - kwargs['quality_layers'] = np.array([cratio], dtype=np.float64) |
| 77 | + kwargs["quality_layers"] = np.array([cratio], dtype=np.float64) |
78 | 78 | blosc2_grok.set_params_defaults(**kwargs) |
79 | | - group = fout.create_group(f'/cratio_{cratio}x') |
| 79 | + group = fout.create_group(f"/cratio_{cratio}x") |
80 | 80 | # Store the color and grey images |
81 | 81 | for dset in [dset_color, dset_gray]: |
82 | 82 | chunks = (1,) + dset.shape[1:] |
83 | | - name = 'numbers_color' if dset is dset_color else 'numbers_gray' |
84 | | - disk_dset = group.create_dataset(name, shape=dset.shape, dtype=dset.dtype, |
85 | | - chunks=chunks, **b2params) |
86 | | - disk_dset.attrs['contenttype'] = 'tomography' |
| 83 | + name = "numbers_color" if dset is dset_color else "numbers_gray" |
| 84 | + disk_dset = group.create_dataset( |
| 85 | + name, shape=dset.shape, dtype=dset.dtype, chunks=chunks, **b2params |
| 86 | + ) |
| 87 | + disk_dset.attrs["contenttype"] = "tomography" |
87 | 88 | for i in range(dset.shape[0]): |
88 | | - im = dset[i:i+1] |
| 89 | + im = dset[i : i + 1] |
89 | 90 | b2im = blosc2.asarray(im, chunks=im.shape, blocks=im.shape, cparams=cparams) |
90 | 91 | # Write to disk. |
91 | 92 | # Due to leading 1 dim, color images have 4 dimensions, gray images have 3 dimensions |
|
0 commit comments