Skip to content

Commit

Permalink
Split Python example in compress and decompress
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavid committed Aug 23, 2023
1 parent 6e35202 commit e92c9b7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
19 changes: 6 additions & 13 deletions .github/workflows/cibuildwheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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/[email protected]
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"

Expand Down
38 changes: 10 additions & 28 deletions examples/test.py → examples/compress.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
from pathlib import Path

import blosc2
import blosc2_openhtj2k
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
44 changes: 44 additions & 0 deletions examples/decompress.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit e92c9b7

Please sign in to comment.