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.1.1.tar.gz (7.6 kB view hashes)

Uploaded Source

Built Distributions

pyfastmask-1.1.1-cp312-cp312-win_amd64.whl (86.2 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

pyfastmask-1.1.1-cp312-cp312-win32.whl (77.9 kB view hashes)

Uploaded CPython 3.12 Windows x86

pyfastmask-1.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

pyfastmask-1.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (131.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pyfastmask-1.1.1-cp312-cp312-macosx_11_0_arm64.whl (92.8 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pyfastmask-1.1.1-cp312-cp312-macosx_10_9_x86_64.whl (98.4 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

pyfastmask-1.1.1-cp311-cp311-win_amd64.whl (85.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pyfastmask-1.1.1-cp311-cp311-win32.whl (77.7 kB view hashes)

Uploaded CPython 3.11 Windows x86

pyfastmask-1.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

pyfastmask-1.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (132.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pyfastmask-1.1.1-cp311-cp311-macosx_11_0_arm64.whl (93.9 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pyfastmask-1.1.1-cp311-cp311-macosx_10_9_x86_64.whl (99.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pyfastmask-1.1.1-cp310-cp310-win_amd64.whl (85.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pyfastmask-1.1.1-cp310-cp310-win32.whl (77.1 kB view hashes)

Uploaded CPython 3.10 Windows x86

pyfastmask-1.1.1-cp310-cp310-musllinux_1_2_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

pyfastmask-1.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (131.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyfastmask-1.1.1-cp310-cp310-macosx_11_0_arm64.whl (92.6 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pyfastmask-1.1.1-cp310-cp310-macosx_10_9_x86_64.whl (97.9 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pyfastmask-1.1.1-cp39-cp39-win_amd64.whl (84.6 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pyfastmask-1.1.1-cp39-cp39-win32.whl (77.2 kB view hashes)

Uploaded CPython 3.9 Windows x86

pyfastmask-1.1.1-cp39-cp39-musllinux_1_2_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

pyfastmask-1.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (131.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyfastmask-1.1.1-cp39-cp39-macosx_11_0_arm64.whl (92.7 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pyfastmask-1.1.1-cp39-cp39-macosx_10_9_x86_64.whl (98.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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