Unified image to tensor utility with streaming TFRecord support
Project description
img2tensor
A unified, high-performance utility to convert images into training-ready tensors for NumPy, PyTorch, and TensorFlow, or stream them directly into TFRecords.
img2tensor handles the standard deep learning data ingestion "papercuts": BGR/RGB swaps, memory layouts (NHWC vs NCHW), and dtype scaling in a single, lightweight function.
🚀 Installation
pip install img2tensor
📖 Usage
1. Single Image (In-Memory)
Returns a 3D tensor ($C, H, W$ for PyTorch).
import img2tensor
Returns: torch.Tensor of shape (3, 224, 224)
tensor = img2tensor.get_tensor("cat.jpg", tensor_type="pytorch")
2. Batch Loading (In-Memory)
Returns a 4D tensor ($N, H, W, C$ for NumPy/TF).
Returns: np.ndarray of shape (32, 224, 224, 3)
batch = img2tensor.get_tensor(list_of_paths, n_jobs=8)
3. Production Pipeline (TFRecord)
Writes to disk using a chunked streaming approach to save RAM.
img2tensor.get_tensor( img_paths=large_list_of_paths, output_format="tfrecord", tfrecord_path="dataset.tfrecord", n_jobs=12 )
🛠 API Reference: get_tensor()
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
img_paths |
`str | Path | list` |
tensor_type |
str |
"numpy" |
Target framework: "numpy", "pytorch", or "tensorflow". |
dtype |
str |
"float32" |
Target type: "float32", "float16", "uint8". Floats are auto-scaled (1/255). |
image_layer |
str |
"PIL" |
Backend decoder: "PIL" or "CV2". |
n_jobs |
int |
4 |
Number of threads for parallel decoding. |
output_format |
str |
"tensor" |
"tensor" (returns object) or "tfrecord" (writes to disk). |
tfrecord_path |
`str | Path` | None |
Outputs
- Single Path Input: Returns a 3D Tensor ($H, W, C$ for NumPy/TF; $C, H, W$ for PyTorch).
- List Input: Returns a 4D Tensor ($N, H, W, C$ for NumPy/TF; $N, C, H, W$ for PyTorch).
- TFRecord Mode: Returns a
dictwith file metadata (path, sample count, dtype).
🧠 Design Philosophy
NCHW vs NHWC
One of the most frequent bugs in Computer Vision pipelines is passing the wrong channel layout. img2tensor detects your framework and adjusts automatically:
- PyTorch: Returns $N \times C \times H \times W$ (and ensures memory is
.contiguous()). - NumPy/TF: Returns $N \times H \times W \times C$.
What it Does NOT Do (By Design)
- No Resizing: We believe silent resizing is dangerous as it introduces artifacts. If your images are inconsistent sizes,
get_tensorwill throw aValueErroridentifying the offending file. - No Augmentation: This is a pure loader. Use specialized libraries like
albumentationsortorchvisionfor data manipulation. - No Heavy Dependencies: Using lazy imports, the library won't crash if you don't have TensorFlow installed but only use NumPy or Torch.
📄 License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file img2tensor-0.1.3.tar.gz.
File metadata
- Download URL: img2tensor-0.1.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
675b9e90020a6e28481b8dabd785d95acff3955a06c5e0e255d262cb75cb18ed
|
|
| MD5 |
7174021d328a0e4632b535e5ea553feb
|
|
| BLAKE2b-256 |
f006489fc5acf8463d99284c8789439f912bdb51dd9c51674c567dbbc9caf352
|
Provenance
The following attestation bundles were made for img2tensor-0.1.3.tar.gz:
Publisher:
publish.yml on sourabhyadav999/img2tensor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
img2tensor-0.1.3.tar.gz -
Subject digest:
675b9e90020a6e28481b8dabd785d95acff3955a06c5e0e255d262cb75cb18ed - Sigstore transparency entry: 798838671
- Sigstore integration time:
-
Permalink:
sourabhyadav999/img2tensor@0253ee9e18e0983375ad7427bf0b5aeac16f8499 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/sourabhyadav999
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0253ee9e18e0983375ad7427bf0b5aeac16f8499 -
Trigger Event:
release
-
Statement type:
File details
Details for the file img2tensor-0.1.3-py3-none-any.whl.
File metadata
- Download URL: img2tensor-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b5e3f2bafd0894308ac73ef67bc66044397fc3c1d307a9d3cf4908cfa212956
|
|
| MD5 |
f22ee465892c86aec2f815da34574e35
|
|
| BLAKE2b-256 |
72c2822663daab97c0faf565c31c1a55f5c99f864f6c15365acc401ef0b60ff9
|
Provenance
The following attestation bundles were made for img2tensor-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on sourabhyadav999/img2tensor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
img2tensor-0.1.3-py3-none-any.whl -
Subject digest:
8b5e3f2bafd0894308ac73ef67bc66044397fc3c1d307a9d3cf4908cfa212956 - Sigstore transparency entry: 798838673
- Sigstore integration time:
-
Permalink:
sourabhyadav999/img2tensor@0253ee9e18e0983375ad7427bf0b5aeac16f8499 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/sourabhyadav999
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0253ee9e18e0983375ad7427bf0b5aeac16f8499 -
Trigger Event:
release
-
Statement type: