Python wrapper for decoding DCT coefficients from JPEG-compressed images.
Note: This is research code and has not been designed for use in production. Tested with Python 3.5 and libjpeg 9a.
- Header files for building Python extensions
apt-get install python3-dev
- You need to have libjpeg installed. If you don't have a global installation, you can download libjpeg from here and install it by following the build instructions. Please update the include and lib paths in
setup.py
such that they point to your installation.
libjpeg_include_dir = "path/to/libjpeg/build/include"
libjpeg_lib_dir = "path/to/libjpeg/build/lib"
- Python dependencies
pip install Cython numpy
To compile the extension for use in the project's root directory, run the following:
python setup.py build_ext --inplace
This will place a shared object in the current working directory.
For use from another directory, make sure to include this module in your Python path.
This can be done by either including the project's root directory in your PYTHONPATH
environment variable, or by installing the module with python setup.py install
.
from decoder import PyCoefficientDecoder
filename = "image.jpg"
# Load and decode image
d = PyCoefficientDecoder(filename)
# Access image width
d.image_width
# Read (quantized) DCT coefficients of luminance channel
d.get_dct_coefficients(0)
# Read quantization table for luminance channel
d.get_quantization_table(0)
- Attempting to read uncompressed image after reading DCT coefficients results in
Improper call to JPEG library in state 210
. Workaround: Initialize another instance ofPyCoefficientDecoder
.
- Another way to quickly read an image's quantization tables:
djpeg -verbose -verbose image.jpg > /dev/null
- A visual explanation of the Discrete Cosine Transform (DCT) is given in this notebook.