diff --git a/.github/workflows/cibuildwheels.yml b/.github/workflows/cibuildwheels.yml index bf09c96..803c16f 100644 --- a/.github/workflows/cibuildwheels.yml +++ b/.github/workflows/cibuildwheels.yml @@ -59,29 +59,22 @@ jobs: env: CIBW_BUILD: 'cp38-win_amd64 cp39-win_amd64 cp310-win_amd64 cp311-win_amd64' CIBW_BEFORE_TEST: python -m pip install --upgrade pip && python -m pip install -r requirements-test.txt - CIBW_TEST_COMMAND: cmd /V /C "set "BLOSC_TRACE=1" && python {project}/examples/test.py {project}/examples/teapot.ppm" + CIBW_TEST_COMMAND: > + cmd /V /C "python {project}/examples/compress.py {project}/examples/teapot.ppm /tmp/teapot.b2nd" CIBW_BUILD_VERBOSITY: 1 - - name: Build wheels (Mac OSX arm64) - if: ${{ matrix.os == 'macos-latest' && matrix.arch == 'arm64' }} - uses: pypa/cibuildwheel@v2.14.1 - env: - CIBW_BUILD: 'cp38-* cp39-* cp310-* cp311-*' - CIBW_BEFORE_TEST: python -m pip install --upgrade pip && python -m pip install -r requirements-test.txt - CIBW_TEST_COMMAND: BLOSC_TRACE=1 python {project}/examples/test.py {project}/examples/teapot.ppm - CIBW_BUILD_VERBOSITY: 1 - CIBW_ARCHS_MACOS: "arm64" - - name: Build wheels (Linux / Mac OSX) if: ${{ matrix.os != 'windows-latest' && (matrix.arch == 'x86_64' || matrix.arch == 'aarch64') }} uses: pypa/cibuildwheel@v2.14.1 env: CIBW_BEFORE_BUILD: python -m pip install --upgrade pip && python -m pip install -r requirements-build.txt CIBW_BUILD: 'cp38-* cp39-* cp310-* cp311-*' - CIBW_SKIP: '*-manylinux*_i686 *-musllinux_* ${{ env.CIBW_SKIP}}' + #CIBW_SKIP: '*-manylinux*_i686 *-musllinux_* ${{env.CIBW_SKIP}}' CIBW_ARCHS_LINUX: ${{ matrix.arch }} CIBW_BEFORE_TEST: python -m pip install --upgrade pip && python -m pip install -r requirements-test.txt - CIBW_TEST_COMMAND: BLOSC_TRACE=1 python {project}/examples/test.py {project}/examples/teapot.ppm + CIBW_TEST_COMMAND: > + python {project}/examples/compress.py {project}/examples/teapot.ppm /tmp/teapot.b2nd && + python {project}/examples/decompress.py /tmp/teapot.b2nd /tmp/teapot.ppm CIBW_BUILD_VERBOSITY: 1 CIBW_ARCHS_MACOS: "x86_64" diff --git a/examples/test.py b/examples/compress.py similarity index 68% rename from examples/test.py rename to examples/compress.py index ac96783..ead71ac 100644 --- a/examples/test.py +++ b/examples/compress.py @@ -1,4 +1,5 @@ import argparse +from pathlib import Path import blosc2 import blosc2_openhtj2k @@ -10,7 +11,7 @@ # This is done once. blosc2.register_codec("openhtj2k", 244) -def compress(im): +def compress(im, urlpath=None): """This function gets a PIL image and returns a blosc2 array. """ # Convert the image to a numpy array, it will have 3 dimensions: height, width, color. @@ -24,7 +25,9 @@ def compress(im): np_array = np_array.astype('uint32') # Set the parameters that will be used by the codec - blosc2_openhtj2k.set_params_defaults(transformation=1) + blosc2_openhtj2k.set_params_defaults( + transformation=1, + ) # Multithreading is not supported, so we must set the number of threads to 1 nthreads = 1 @@ -47,6 +50,7 @@ def compress(im): blocks=np_array.shape, cparams=cparams, dparams=dparams, + urlpath=urlpath, ) # Print information about the array, see the compression ratio (cratio) @@ -55,36 +59,14 @@ def compress(im): return bl_array -def decompress(bl_array): - """This function gets a blosc2 array and returns a PIL image. - """ - # Transform the blosc2 array to a numpy array. This is where decompression happens. - np_array = bl_array[:] - - # Get back 1 byte per color (the codec works with uint32) - np_array = np_array.astype('uint8') - - # Get back the original shape: height, width, channel - np_array = np.transpose(np_array, (2, 1, 0)) - - # Transfom the numpy array to a PIL image - im = Image.fromarray(np_array) - - return im - - if __name__ == '__main__': parser = argparse.ArgumentParser( - description='Compress and decompress the given input image', + description='Compress the given input image using Blosc2 and OpenHTJ2K', ) parser.add_argument('inputfile') - parser.add_argument('--show', action='store_true') + parser.add_argument('outputfile') args = parser.parse_args() - # Load the image using PIL im = Image.open(args.inputfile) - bl_array = compress(im) - im = decompress(bl_array) - - if args.show: - im.show() + Path(args.outputfile).unlink(missing_ok=True) + compress(im, args.outputfile) diff --git a/examples/decompress.py b/examples/decompress.py new file mode 100644 index 0000000..a9e2f44 --- /dev/null +++ b/examples/decompress.py @@ -0,0 +1,44 @@ +import argparse + +import blosc2 +import numpy as np +from PIL import Image + + +# Register the codec, with this number we will tell blosc2 to use the openhj2k codec. +# This is done once. +blosc2.register_codec("openhtj2k", 244) + +def decompress(array): + """This function gets a blosc2 array and returns a PIL image. + """ + # Transform the blosc2 array to a numpy array. This is where decompression happens. + np_array = array[:] + + # Get back 1 byte per color (the codec works with uint32) + np_array = np_array.astype('uint8') + + # Get back the original shape: height, width, channel + np_array = np.transpose(np_array, (2, 1, 0)) + + # Transfom the numpy array to a PIL image + im = Image.fromarray(np_array) + + return im + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Decompress the given Blosc2 file, and optionally display the image', + ) + parser.add_argument('inputfile') + parser.add_argument('outputfile') + parser.add_argument('--show', action='store_true') + args = parser.parse_args() + + array = blosc2.open(args.inputfile) + im = decompress(array) + im.save(args.outputfile) + + if args.show: + im.show()