pyfastmask - Fast image segmentation format
This is a simple format for storing single channel images with low-frequency data (e.g. semantic segmentation masks).
It has a size similar to PNG, but is much faster (up to 20x) to read.
Installation
From PyPI:
The easiest way to install the latest version is by using pip:
pip install pyfastmask
From source:
git clone git@github.com:JIy3AHKO/pyfastmask.git
cd pyfastmask
pip install -e .
Usage
For image reading and writing, use the read and write functions:
import numpy as np
import pyfastmask as pf
img = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
pf.write('mask.pfm', img)
img2 = pf.read('mask.pfm')
np.testing.assert_array_equal(img, img2)
Benchmark
See BENCHMARK.md for more detailed information.
| Image | pyfastmask | opencv png | cv2_bmp | qoi |
|---|---|---|---|---|
| Median Read Time | 0.09 ms | 1.71 ms | 0.35 ms | 0.81 ms |
| Average Size | 217.35 KiB | 149.36 KiB | 1146.64 KiB | 498.24 KiB |
Format Description
The pyfastmask efficiently compresses and stores image segmentation masks using Run-Length Encoding (RLE) and line-differential encoding. All values are stored with different bit widths, depending on the number of unique symbols and the mask size - it helps to reduce the size of encoded data.
Storage Structure
The format organizes data into three main sections: header, symbol mapping, and line-by-line encoded data.
1. Header
- Magic Byte: Format identifier.
- Version Byte: Format version.
- Symbol Bit Width: Bits for each symbol.
- Count Bit Width: Bits for run lengths.
- Line Count Bit Width: Bits for the number of runs per line.
- Unique Symbols Count: Number of unique symbols.
- Mask Height: Mask height in pixels.
- Mask Width: Mask width in pixels.
2. Symbol Mapping
Lists unique symbols in the mask, each encoded with 8 bits.
Semantic segmentation masks usually have a small number of unique symbols, so we can use a small number of bits to encode each symbol.
3. Line-by-Line Encoding
Encodes mask data line by line:
- First Line: Encoded with standard RLE.
- Subsequent Lines: Encoded with sparse RLE on the difference between the current and previous lines.
First line is represented as: (Number of runs), (Symbol, Run Length), (Symbol, Run Length), ...
Subsequent lines are represented as: (Number of runs), (Offset, Symbol, Run Length), (Offset, Symbol, Run Length), ...
Where:
- Number of runs: Number of runs in the line.
- Symbol: Symbol index from the symbol mapping.
- Run Length: Number of pixels with the same symbol.
- Offset: Number of pixels to skip from the previous line.
Encoding Process
- Encode Lines:
- First Line: Standard RLE.
- Subsequent Lines: Sparse RLE.
- Estimate Bit Widths: Calculate the number of bits required to store each value.
- Write Header and Symbol Mapping.
- Pack Data: Combine all encoded data into a byte stream.
Decoding Process
- Read Header and Symbol Mapping.
- Decode first line with standard RLE.
- On subsequent lines:
- copy the previous line
- apply sparse RLE to the symbols which are differ from the previous line
Testing
To run tests, use the following command:
python -m unittest discover tests/
Contributing
Contributions are welcome! If you want to contribute, please create an issue or a pull request.
Metadata
Release files for pyfastmask 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyfastmask-1.2.0.tar.gz | 8.2 kB | Details |
Built distributions (wheels)
Total release size: 6.4 MB
Release files / pyfastmask-1.2.0.tar.gz
| Download URL | pyfastmask-1.2.0.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e5a755192b410f5470bcd1758e1d8677daf355ece02922447a15e990f8e32350
|
|
BLAKE2b-256 checksum How to use checksums |
299bb29eb5b0e9d47c8de99d73f5d34b08c6675bf7657798b73f2353ffcf3052
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp312-cp312-win_amd64.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 87.5 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
8355df60225160a1fb79dc653cb3177c00b452a283c6ed8d8fc0f75236bbde9b
|
|
BLAKE2b-256 checksum How to use checksums |
6d4bf9c62cede0aa9d1405f7f6cf1ed6df3ef8237a3ab9fafed0672b64840051
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp312-cp312-win32.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-win32.whl |
|---|---|
| Size | 79.2 kB |
| Tags | CPython 3.12 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
ab30c8396d5d1886b8186118e317b094405068f58d5747f009c7fc61dd642e30
|
|
BLAKE2b-256 checksum How to use checksums |
f2d8cb804584105e0b85c6fe669e7fb6b10dd1c1b79a47c8519d37600b450939
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.12 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
a0f94b1fb66deff78ec753ced79358c06ad348939f8103f29d69e5b353495d02
|
|
BLAKE2b-256 checksum How to use checksums |
cea5f0baaec5c2dcfef9254fa83ab6bf53107506d851161930a4ddfbd5703daa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 133.1 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
670d06a62e9116288934eacbf0d94482fb0704ee8f6636ba53ef5666af2f64d9
|
|
BLAKE2b-256 checksum How to use checksums |
8de77949514e147be948b85cbe5be456a8bd182129f9ed8b22864502f328f99f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 94.5 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
399d0c2acabbb3c30d997278334f6e11604ef995f2651a61bdfc8c3e2c077e82
|
|
BLAKE2b-256 checksum How to use checksums |
167a159214a8e9ab75e593e40dfab96c608ad117bb409221bafa2395aeb1a15e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp312-cp312-macosx_10_9_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp312-cp312-macosx_10_9_x86_64.whl |
|---|---|
| Size | 100.3 kB |
| Tags | CPython 3.12 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
01224195d629ad5e42eb298efc9d511d4421b016aeb73f6e1aece157cafd29b2
|
|
BLAKE2b-256 checksum How to use checksums |
83e1e7c1da3396bedf64f137ca1511beb5abf7a45c4416e23b3248f067b4a855
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp311-cp311-win_amd64.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 87.0 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
039582e7ccb25446fc73d55e0d28c61b9e4ede2e470870c95b9846accef4b46a
|
|
BLAKE2b-256 checksum How to use checksums |
28a0a6a1e4508e9cda91b273ce9af3e7540747f308f351a1f1dce73109d38356
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp311-cp311-win32.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-win32.whl |
|---|---|
| Size | 79.0 kB |
| Tags | CPython 3.11 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
6d02e4ef325bded16d5223612ccb3fc66511e1f95fbbbc130d232b5e3b1a211b
|
|
BLAKE2b-256 checksum How to use checksums |
98c94e14271d37ad9cad1d505fc111d8d6780b6308ad025afcbc52e7b190acc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp311-cp311-musllinux_1_2_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.11 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
9ce310c3ba0723c87eb2974325d6f0b57b5dfc895e0543022cd23688c52ac3a5
|
|
BLAKE2b-256 checksum How to use checksums |
f9195f1afeec7140eb132d53ae089da73e25ceb190cc17f5abc2dfe91d7174b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 134.1 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
f930e45ee84d79e942095543b3e9c44c31e0ed4bdc46342bf228efdffcdbe101
|
|
BLAKE2b-256 checksum How to use checksums |
a53473f1ca3d13759b246b8ce5ea1b8c2cfee7703bd549c46a3072b7972c5a73
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp311-cp311-macosx_11_0_arm64.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-macosx_11_0_arm64.whl |
|---|---|
| Size | 95.5 kB |
| Tags | CPython 3.11 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
78e0a5fb0f7ab45465e34741038d02c55dfde4fdaf7fc87967d441065c95a96f
|
|
BLAKE2b-256 checksum How to use checksums |
ee30da9cb325acd671d09c690f5f410f48eaaa838ed3f4fbec04b30e3aba24af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl |
|---|---|
| Size | 100.8 kB |
| Tags | CPython 3.11 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
fd830b6227dbcea444bb8f3dcbbeeb8c0d7175908ab9a6bab25357d9b866da6e
|
|
BLAKE2b-256 checksum How to use checksums |
67011f841d1c71e528f312fdab12e7de1a6dbd009d9f92285c677c768af90392
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp310-cp310-win_amd64.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 86.3 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
42c999cab20888898d060ad9a19b019b234ac9169bd305a59bdf8bb9604aeceb
|
|
BLAKE2b-256 checksum How to use checksums |
cdb3266a3ea0d402239480ccc398ecc397ce6ebad11feb28b79c120c99ef4247
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp310-cp310-win32.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-win32.whl |
|---|---|
| Size | 78.3 kB |
| Tags | CPython 3.10 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
4f31fc10752b461ed819eedead2dae9c7ecec2485a6ca144bdf3cc913320c4f0
|
|
BLAKE2b-256 checksum How to use checksums |
d119fbc36af5ad501f85b6fa7205c634c96222a89c9bf74de413cba0cd024ff5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp310-cp310-musllinux_1_2_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.10 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
f54a5b23c25ccc0268760ea9126a23795c4bae877ef287e4ff015b36f0652d6c
|
|
BLAKE2b-256 checksum How to use checksums |
6869f4a7fa9cee54232bd3cde38c255222d0e97e79c0813ef009a6518b06d056
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 132.9 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
922f2012d95c72f0a14f8e87adece588abfa97f0078e8198fca818b393f55fd9
|
|
BLAKE2b-256 checksum How to use checksums |
aa37e3592c2611f8cf5033a72ffde73cef7ed4dae633bd7e2b6c93dc66454b97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp310-cp310-macosx_11_0_arm64.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-macosx_11_0_arm64.whl |
|---|---|
| Size | 93.8 kB |
| Tags | CPython 3.10 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
2615ff6357c6c1da655aa9f89209c1f4ae6c89a4bd46a839163a2f452d720305
|
|
BLAKE2b-256 checksum How to use checksums |
6ef7b27f8f55e3080d9890d1c737165076a71028a811a9f57be4a3df0268b3e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl |
|---|---|
| Size | 99.5 kB |
| Tags | CPython 3.10 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
932a096839eac3ba6d6b98c3e1ec72ad7c316f9656713120ee62b35d54c261bd
|
|
BLAKE2b-256 checksum How to use checksums |
09358bf4b5fc6f254e9d94544d939dda6d6b31db5b9ffdf8b0b077084cc7daa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp39-cp39-win_amd64.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-win_amd64.whl |
|---|---|
| Size | 85.8 kB |
| Tags | CPython 3.9 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
3c9fa3db5fb957920354c67eded9a7069cb00d8b5b6b25bfe140e704ad7ebb48
|
|
BLAKE2b-256 checksum How to use checksums |
46b6afde87c5e36a830587db2c192994b1a59e48da5788b285740ad31dbf60c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp39-cp39-win32.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-win32.whl |
|---|---|
| Size | 78.5 kB |
| Tags | CPython 3.9 Windows x86-32 |
|
SHA-256 checksum How to use checksums |
7bfc4cb43c59c709aaed62294307bfc95f64bc9c867d00aece496b4040625926
|
|
BLAKE2b-256 checksum How to use checksums |
6dafa5a46700310584285058967996ff5534885849e4b09749bd2448c2c5d5cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp39-cp39-musllinux_1_2_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.9 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
215213f0d3a499711e49b7aa0e1bde4f9bd90702fdd8352f475b6c34521ecbba
|
|
BLAKE2b-256 checksum How to use checksums |
97a8bfb4592ba2a97611eb970e6bbdffe77045911fe2e69a98c56611fd5aeb93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 133.0 kB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
412d40407bc928ea876658c593a853e3a8be8d0c5c5ff2c02db076ba8d044e9e
|
|
BLAKE2b-256 checksum How to use checksums |
a6562c4e01561fd472f7be8d805f046a26f191174215c15599f44c95aa598799
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / pyfastmask-1.2.0-cp39-cp39-macosx_11_0_arm64.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-macosx_11_0_arm64.whl |
|---|---|
| Size | 94.0 kB |
| Tags | CPython 3.9 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
69d96132298d9c420bdbdb6ef015bf6541ebf121f7c539486d355faece23dd54
|
|
BLAKE2b-256 checksum How to use checksums |
4aba34d8be231717250b584cf5c94c6778848336a951bd76c1b6c2cf9a07ee22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.13
|
Release files / pyfastmask-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl
| Download URL | pyfastmask-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl |
|---|---|
| Size | 99.6 kB |
| Tags | CPython 3.9 macOS 10.9+ x86-64 |
|
SHA-256 checksum How to use checksums |
3628652da953091814121d2b4e2339d11fed84de8fd0cb3ed7ad0ac0aed55181
|
|
BLAKE2b-256 checksum How to use checksums |
11e4cb8bc10d5781ebfff9a443e09308ffed447742cc34502be4d9b0a5d82e67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|