Skip to main content

Zero-copy conversion between Qt QImage (in `Format_RGB32`/`Format_ARGB32`) and NumPy ndarray (in HWC BGRX 8888 format).

Project description

qimage-hwc-bgrx-8888-ndarray-interop

Zero-copy conversion between Qt QImage (in Format_RGB32/Format_ARGB32) and NumPy ndarray (in HWC BGRX 8888 format).

Features

  • Zero-copy: Memory is shared between objects - no pointless data copying!
  • Cross-binding: Supports PyQt6, PyQt5, PyQt4, PySide6, PySide2, or PySide.
  • Safety: Explicit checks for packing and format - no surprises.

Installation

Install your preferred Qt binding, then run:

pip install qimage-hwc-bgrx-8888-ndarray-interop

Usage

# coding=utf-8
from qimage_hwc_bgrx_8888_ndarray_interop import (
    qimage_to_hwc_bgrx_8888_ndarray_view,
    qimage_to_hwc_bgrx_8888_ndarray,
    hwc_bgrx_8888_ndarray_to_qimage_view,
    hwc_bgrx_8888_ndarray_to_qimage,
)

# Convert a QImage to numpy.ndarray (zero-copy view):
arr_view = qimage_to_hwc_bgrx_8888_ndarray_view(qimage)

# Convert a QImage to a copy of image data:
arr = qimage_to_hwc_bgrx_8888_ndarray(qimage)

# Convert a contiguous (H, W, 4) uint8 ndarray back to a QImage (zero-copy view):
qimg_view = hwc_bgrx_8888_ndarray_to_qimage_view(arr)

# Convert ndarray to a QImage (copies data):
qimg_copy = hwc_bgrx_8888_ndarray_to_qimage(arr)

Safety & Caveats

  • Endianness: Only little-endian systems are supported (typical modern computers).
    • On unsupported (big-endian) systems, RuntimeError is raised at import time.
  • Tightly packed images only: QImage must have bytesPerLine == width * 4 for zero-copy view.
  • Memory lifetime matters:
    • Do not let the QImage or ndarray be garbage collected while the other exists and references its buffer.
    • If in doubt, use the copy() variants.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file qimage_hwc_bgrx_8888_ndarray_interop-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for qimage_hwc_bgrx_8888_ndarray_interop-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 78b6f66993264a16e1cb779967ce7fa2d788899db17b94cf8838837b9a84afa4
MD5 2c4aa9dd2abd9f60ee852acae1e297fd
BLAKE2b-256 8f4ec77e27654bb55289a7909ee4d608240743d95a4dbe281e5d7b6be5f47ca6

See more details on using hashes here.

File details

Details for the file qimage_hwc_bgrx_8888_ndarray_interop-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for qimage_hwc_bgrx_8888_ndarray_interop-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2e865ffde38f82f99ee005d8a4d15c648ae31f31eabb9d8d609c6dd2a96980ba
MD5 71584c4c49aedcd9931ee2204d3672a3
BLAKE2b-256 01a5122291b85f0e736b905de63e70fb0b8099330dd63d859b3fd30298da2ac5

See more details on using hashes here.

Supported by

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