Skip to main content

Convert an HWC BGR 888 or BGRX 8888 NumPy ndarray to a CHW RGB PyTorch tensor, with pixel values normalized to `[0, 1]` and the alpha/unused channel discarded.

Project description

hwc-bgrx-8888-ndarray-to-chw-rgb-0-1-tensor

Convert an HWC BGR 888 or BGRX 8888 NumPy ndarray to a CHW RGB PyTorch tensor, with pixel values normalized to [0, 1] and the alpha/unused channel discarded.

This function helps bridge interoperability between Qt, OpenCV (which often use HWC BGRX 8888 images), and PyTorch ( which often uses CHW RGB tensors with pixel values normalized to [0, 1]).

Installation

pip install hwc-bgrx-8888-ndarray-to-chw-rgb-0-1-tensor

Usage

import numpy as np
from hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor import hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor

# Example: create a dummy BGRX image
hwc_bgrx = np.random.randint(0, 256, (224, 224, 4), dtype=np.uint8)

tensor = hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor(hwc_bgrx)
print(tensor.shape)  # (3, 224, 224)
print(tensor.min(), tensor.max())  # [0.0, 1.0]

Notes

  • This function makes the array contiguous for compatibility with PyTorch tensors.

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 hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 852d1b73567ff7f7cc8fa34e265cbca4bb56027de81a0188938bb3aea97c82b2
MD5 7880b5b32567596a920f5ecddc7b2685
BLAKE2b-256 5e487978d67e7a22eb52fbde87c8d36c065f83d09213aeff461377f10166f926

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hwc_bgrx_8888_ndarray_to_chw_rgb_0_1_tensor-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 10315171bfcab0c0f5e720dabb472e34e7e43dcadeb562e76d6ce79f428df8c1
MD5 09bb9455d491227808f407b1450db3f1
BLAKE2b-256 7c5e7bcfc36c4fe285d3a56db2bbfaf93d1c559ef8b22e3f04874671a3bbca34

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