Utilities for JPEG data access and manipulation in pytorch
Project description
TorchJPEG
This C++ extension for pytorch interfaces with libjpeg to allow for manipulation of low-level JPEG data using pytorch. By using libjpeg, quantization results are guaranteed to be consistent with other applications, like image viewers or MATLAB, which use libjpeg to compress and decompress images. This is useful because JPEG images can be effected by round-off errors or slight differences in the decompression procedure. Besides this, this library can be used to read and write DCT coefficients, functionality which is not available from other python interfaces.
To quantize spatial domain pixels, the data can be provided in the pytorch native format e.g. CHW float tensors with
values in [0, 1]. For reading data, however, the results are largely untampered with and it is up to the user to
manipulate them as needed. Quantization matrices are represented as C x 8 x 8 short (int16) tensors and DCT coefficients are
given as C x H//8 x W//8 x 8 x 8 short tensors (e.g. blocks) of quantized coefficients, the same format used internally
by libjpeg. The DCT coefficients for the Y channel and CrCb channels are returned separately and will usually be
different sizes. It is up to the user of the library to dequantize, reshape, and otherwise convert them into pixels
if desired. See test_decompress.py
for an example of how to do this.
LIBJPEG
Currently builds against libjpeg-9d
Exported Functions
read_coefficients(path)
- Reads DCT coefficients from the JPEG file at the given path. Return values are
dimensions
- (C x 2 int) the downsampled height x width of the channels. This will often be different from the width/height of the returned DCT coefficients if padding was added to the blocksquantization
- (C x 8 x 8 short) The quantization matrix that was used for each channelY_coefficients
- (1 x H//8 x W//8 x 8 x 8 short) The Y channel coefficients organized row-major blockwiseCrCb_coefficients
- (2 x H//8 x W//8 x 8 x 8 short) The CrCb channel coefficients organized row-major blockwise if the image is color, otherwise empty
write_coefficients (path, dimensions, quantization, Y_coefficients, CrCb_coefficients
- Writes the given DCT coefficients to a JPEG file, arguments must be
in the same format as the return value from read_coefficients
quantize_at_quality(pixels, quality, baseline)
- Computes the quantized DCT coefficients for the given input pixels at the given quality.
Baseline is true be default and limits the quantization matrix entries to the [0, 255] range as mandated by the JPEG standard (see libjpeg documentation
for more details). Quality is an integer from [0, 100] where 0 is the worst quality and 100 is the best quality (note that 100 is not lossless). Pixels
is a tensor in the standard pytorch image format, e.g. CHW float in [0, 1]. Return values are the same as read_coefficients
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file torchjpeg-0.9.1-cp38-cp38-manylinux2014_x86_64.whl
.
File metadata
- Download URL: torchjpeg-0.9.1-cp38-cp38-manylinux2014_x86_64.whl
- Upload date:
- Size: 2.8 MB
- Tags: CPython 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfd5b7b35f845b6ec80bdd4f16bab0ea0cc4735ecaed5c4ac0dca0325797b941 |
|
MD5 | 346f1a54a9f9c3462a42904887d10a08 |
|
BLAKE2b-256 | e35401e3451fffc07bb58feec2d3e773620c2198117371c3dc5b3e441e9e234e |
File details
Details for the file torchjpeg-0.9.1-cp37-cp37m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: torchjpeg-0.9.1-cp37-cp37m-manylinux2014_x86_64.whl
- Upload date:
- Size: 2.8 MB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa72cd6aca4229a2ed9394ed6ebc8523c77fcf72e84a42a52e5763a8bd076712 |
|
MD5 | d0c0446d5c295110b310b99abdd6cb5d |
|
BLAKE2b-256 | 2887c8248e114654341abec3d399361c017fb40964535576f6c4dd411ff34d92 |
File details
Details for the file torchjpeg-0.9.1-cp36-cp36m-manylinux2014_x86_64.whl
.
File metadata
- Download URL: torchjpeg-0.9.1-cp36-cp36m-manylinux2014_x86_64.whl
- Upload date:
- Size: 2.8 MB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 205f359e0977d4d3f619e40065fb0521d65b25530eb04e9eb0b55a7244862950 |
|
MD5 | 5d8beb545c53f61f0da108abf5711870 |
|
BLAKE2b-256 | 32eae4219fd620c5f4bfad1136fe39f1c3da90cd917fd44cc0e54ca6dd4a8dbc |