Skip to main content

Fast low color mask read/write

Project description

pyfastmask - Fast image segmentation format

Tests

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

  1. Encode Lines:
    • First Line: Standard RLE.
    • Subsequent Lines: Sparse RLE.
  2. Estimate Bit Widths: Calculate the number of bits required to store each value.
  3. Write Header and Symbol Mapping.
  4. Pack Data: Combine all encoded data into a byte stream.

Decoding Process

  1. Read Header and Symbol Mapping.
  2. Decode first line with standard RLE.
  3. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfastmask-1.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distributions

pyfastmask-1.2.0-cp312-cp312-win_amd64.whl (87.5 kB view details)

Uploaded CPython 3.12 Windows x86-64

pyfastmask-1.2.0-cp312-cp312-win32.whl (79.2 kB view details)

Uploaded CPython 3.12 Windows x86

pyfastmask-1.2.0-cp312-cp312-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

pyfastmask-1.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (133.1 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pyfastmask-1.2.0-cp312-cp312-macosx_11_0_arm64.whl (94.5 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pyfastmask-1.2.0-cp312-cp312-macosx_10_9_x86_64.whl (100.3 kB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

pyfastmask-1.2.0-cp311-cp311-win_amd64.whl (87.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

pyfastmask-1.2.0-cp311-cp311-win32.whl (79.0 kB view details)

Uploaded CPython 3.11 Windows x86

pyfastmask-1.2.0-cp311-cp311-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

pyfastmask-1.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (134.1 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pyfastmask-1.2.0-cp311-cp311-macosx_11_0_arm64.whl (95.5 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pyfastmask-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl (100.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pyfastmask-1.2.0-cp310-cp310-win_amd64.whl (86.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

pyfastmask-1.2.0-cp310-cp310-win32.whl (78.3 kB view details)

Uploaded CPython 3.10 Windows x86

pyfastmask-1.2.0-cp310-cp310-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

pyfastmask-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (132.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyfastmask-1.2.0-cp310-cp310-macosx_11_0_arm64.whl (93.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pyfastmask-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl (99.5 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pyfastmask-1.2.0-cp39-cp39-win_amd64.whl (85.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

pyfastmask-1.2.0-cp39-cp39-win32.whl (78.5 kB view details)

Uploaded CPython 3.9 Windows x86

pyfastmask-1.2.0-cp39-cp39-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

pyfastmask-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (133.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyfastmask-1.2.0-cp39-cp39-macosx_11_0_arm64.whl (94.0 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pyfastmask-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl (99.6 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

Details for the file pyfastmask-1.2.0.tar.gz.

File metadata

  • Download URL: pyfastmask-1.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for pyfastmask-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e5a755192b410f5470bcd1758e1d8677daf355ece02922447a15e990f8e32350
MD5 7a11ded149129c8f65acc6fc02d2028a
BLAKE2b-256 299bb29eb5b0e9d47c8de99d73f5d34b08c6675bf7657798b73f2353ffcf3052

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8355df60225160a1fb79dc653cb3177c00b452a283c6ed8d8fc0f75236bbde9b
MD5 1c2402bfeea569e8985f7e528c31146c
BLAKE2b-256 6d4bf9c62cede0aa9d1405f7f6cf1ed6df3ef8237a3ab9fafed0672b64840051

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-win32.whl.

File metadata

  • Download URL: pyfastmask-1.2.0-cp312-cp312-win32.whl
  • Upload date:
  • Size: 79.2 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 ab30c8396d5d1886b8186118e317b094405068f58d5747f009c7fc61dd642e30
MD5 ff94c3f68803aca263d88c44ed3b20db
BLAKE2b-256 f2d8cb804584105e0b85c6fe669e7fb6b10dd1c1b79a47c8519d37600b450939

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a0f94b1fb66deff78ec753ced79358c06ad348939f8103f29d69e5b353495d02
MD5 e42c65d701af8e249981e3634d9eac5e
BLAKE2b-256 cea5f0baaec5c2dcfef9254fa83ab6bf53107506d851161930a4ddfbd5703daa

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 670d06a62e9116288934eacbf0d94482fb0704ee8f6636ba53ef5666af2f64d9
MD5 5d406c747fef11f9cca003ba4fc54793
BLAKE2b-256 8de77949514e147be948b85cbe5be456a8bd182129f9ed8b22864502f328f99f

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 399d0c2acabbb3c30d997278334f6e11604ef995f2651a61bdfc8c3e2c077e82
MD5 395e3c00b0636dd34f74f491b1ae7ddb
BLAKE2b-256 167a159214a8e9ab75e593e40dfab96c608ad117bb409221bafa2395aeb1a15e

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 01224195d629ad5e42eb298efc9d511d4421b016aeb73f6e1aece157cafd29b2
MD5 ee313923abecccce8ec5f71fb55a24aa
BLAKE2b-256 83e1e7c1da3396bedf64f137ca1511beb5abf7a45c4416e23b3248f067b4a855

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 039582e7ccb25446fc73d55e0d28c61b9e4ede2e470870c95b9846accef4b46a
MD5 a53ba8ffb2728dfeedb106c924395afb
BLAKE2b-256 28a0a6a1e4508e9cda91b273ce9af3e7540747f308f351a1f1dce73109d38356

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: pyfastmask-1.2.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 79.0 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 6d02e4ef325bded16d5223612ccb3fc66511e1f95fbbbc130d232b5e3b1a211b
MD5 98f1044241edc1afb1c1a57d00c12fdd
BLAKE2b-256 98c94e14271d37ad9cad1d505fc111d8d6780b6308ad025afcbc52e7b190acc7

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9ce310c3ba0723c87eb2974325d6f0b57b5dfc895e0543022cd23688c52ac3a5
MD5 c763dd823ac7c9af954b3688f127ca6e
BLAKE2b-256 f9195f1afeec7140eb132d53ae089da73e25ceb190cc17f5abc2dfe91d7174b6

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f930e45ee84d79e942095543b3e9c44c31e0ed4bdc46342bf228efdffcdbe101
MD5 62b99b8fe93a2de330fc36e751f24457
BLAKE2b-256 a53473f1ca3d13759b246b8ce5ea1b8c2cfee7703bd549c46a3072b7972c5a73

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78e0a5fb0f7ab45465e34741038d02c55dfde4fdaf7fc87967d441065c95a96f
MD5 be9d7f47780a9787fb045eef0d4b4335
BLAKE2b-256 ee30da9cb325acd671d09c690f5f410f48eaaa838ed3f4fbec04b30e3aba24af

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fd830b6227dbcea444bb8f3dcbbeeb8c0d7175908ab9a6bab25357d9b866da6e
MD5 04bf3a165da43b94af9afd726cfd4e81
BLAKE2b-256 67011f841d1c71e528f312fdab12e7de1a6dbd009d9f92285c677c768af90392

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 42c999cab20888898d060ad9a19b019b234ac9169bd305a59bdf8bb9604aeceb
MD5 0ee873830ac15d7b153294743f77cebb
BLAKE2b-256 cdb3266a3ea0d402239480ccc398ecc397ce6ebad11feb28b79c120c99ef4247

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-win32.whl.

File metadata

  • Download URL: pyfastmask-1.2.0-cp310-cp310-win32.whl
  • Upload date:
  • Size: 78.3 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 4f31fc10752b461ed819eedead2dae9c7ecec2485a6ca144bdf3cc913320c4f0
MD5 e56978418a07bf73775e7255c03c950b
BLAKE2b-256 d119fbc36af5ad501f85b6fa7205c634c96222a89c9bf74de413cba0cd024ff5

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f54a5b23c25ccc0268760ea9126a23795c4bae877ef287e4ff015b36f0652d6c
MD5 212bde5c3536a1a5e06aad66855a852e
BLAKE2b-256 6869f4a7fa9cee54232bd3cde38c255222d0e97e79c0813ef009a6518b06d056

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 922f2012d95c72f0a14f8e87adece588abfa97f0078e8198fca818b393f55fd9
MD5 4c0c8750f697e530be896a2d9b012c8e
BLAKE2b-256 aa37e3592c2611f8cf5033a72ffde73cef7ed4dae633bd7e2b6c93dc66454b97

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2615ff6357c6c1da655aa9f89209c1f4ae6c89a4bd46a839163a2f452d720305
MD5 d25c67d27dd618faa75394bfd23c256c
BLAKE2b-256 6ef7b27f8f55e3080d9890d1c737165076a71028a811a9f57be4a3df0268b3e8

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 932a096839eac3ba6d6b98c3e1ec72ad7c316f9656713120ee62b35d54c261bd
MD5 310f0aa31a2265bdcd7488ebfc2597e6
BLAKE2b-256 09358bf4b5fc6f254e9d94544d939dda6d6b31db5b9ffdf8b0b077084cc7daa9

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3c9fa3db5fb957920354c67eded9a7069cb00d8b5b6b25bfe140e704ad7ebb48
MD5 ed3df45a3624bbf36598817861fabc84
BLAKE2b-256 46b6afde87c5e36a830587db2c192994b1a59e48da5788b285740ad31dbf60c1

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-win32.whl.

File metadata

  • Download URL: pyfastmask-1.2.0-cp39-cp39-win32.whl
  • Upload date:
  • Size: 78.5 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 7bfc4cb43c59c709aaed62294307bfc95f64bc9c867d00aece496b4040625926
MD5 cccc0fe9ee1adb37aceb7232573929ae
BLAKE2b-256 6dafa5a46700310584285058967996ff5534885849e4b09749bd2448c2c5d5cb

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 215213f0d3a499711e49b7aa0e1bde4f9bd90702fdd8352f475b6c34521ecbba
MD5 c806e948842d318a6f0e9c406b6e1f92
BLAKE2b-256 97a8bfb4592ba2a97611eb970e6bbdffe77045911fe2e69a98c56611fd5aeb93

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 412d40407bc928ea876658c593a853e3a8be8d0c5c5ff2c02db076ba8d044e9e
MD5 7dabd707c1ccf180270e02ac2f4e4d79
BLAKE2b-256 a6562c4e01561fd472f7be8d805f046a26f191174215c15599f44c95aa598799

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 69d96132298d9c420bdbdb6ef015bf6541ebf121f7c539486d355faece23dd54
MD5 48b0e66bbd9b741c394164217d4f5b4b
BLAKE2b-256 4aba34d8be231717250b584cf5c94c6778848336a951bd76c1b6c2cf9a07ee22

See more details on using hashes here.

File details

Details for the file pyfastmask-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyfastmask-1.2.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3628652da953091814121d2b4e2339d11fed84de8fd0cb3ed7ad0ac0aed55181
MD5 27f18173a6d1c08b7481d6c81978cf93
BLAKE2b-256 11e4cb8bc10d5781ebfff9a443e09308ffed447742cc34502be4d9b0a5d82e67

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page