A JPEG encoder implemented in Python
The script does not generate usable .jpg
files in truth,
but it will perform the transforms needed to convert e.g. a PNG
image to JPEG at a requested quality level, showing the difference
in each luma and chroma channel from the original source image
and the JPEG-ified output.
- Convert image RGB pixel values to the YCbCr (luma+chroma) color space.
- Compute the discrete cosine transform to find coefficients for high and low frequency content in 8x8 pixel blocks.
- Compute a quantization table using a specified quality value, then divide each coefficient by the corresponding entry in the table. Divisors increase in magnitude for higher frequency image content.
- Round resulting quotients to the nearest integer. This typically rounds high frequency coefficients to zero. Multiply by the original divisor to get the quantized coefficients, many of which will now be zero.
- Perform an inverse discrete cosine transform to obtain quantized YCbCr pixel values.
- Convert YCbCr color data to RGB to display the image.
The strength of the JPEG encoding process lies in the quantization and removal of high frequency content. High frequency content is less noticeable than low frequency content, so it can be removed with little noticeable impact on many images (notably photographs). By employing run-length encoding and traversing DCT coefficients from lowest frequency to highest, a long string of zeroes in the high frequency section may be represented compactly. Applying a further layer of Huffman coding can further compress the data.