Image compression is a vital aspect of modern computing, particularly in the world of digital art and web development. JPEG (Joint Photographic Experts Group) is a popular format due to its ability to provide high-quality images in a lesser file size. The key technology behind JPEG image compression is Discrete Cosine Transform (DCT). Let’s delve into how we can implement JPEG Image Compression using DCT in Python.
DCT divides an image into parts of varying frequencies. It is a highly efficient way of converting spatial domain pictures into frequency domain counterpart. DCT retains the most valuable parts of the image in terms of human perception while discarding information deemed less important.
To achieve this, we will require a few Python libraries: Numpy, OpenCV and Matplotlib. You can install them using pip:
pip install opencv-python matplotlib numpy
Starting off, we will load the image using OpenCV and convert it to YCrCb color space:
import cv2 import numpy as np image = cv2.imread('image.jpg') ycrcb_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
Here we'll implement DCT on the image:
def dct(img): img_dct = cv2.dct(np.float32(img)) return np.uint8(img_dct) dct_image = dct(ycrcb_image)
Finally, we can save the compressed form of the image using OpenCV’s imwrite method:
cv2.imwrite('compressed_image.jpg', dct_image)
We can compare the sizes of the original and compressed image to confirm the compression.
import os original_size = os.path.getsize('image.jpg') compressed_size = os.path.getsize('compressed_image.jpg') print(f'Original Image Size: {original_size} bytes') print(f'Compressed Image Size: {compressed_size} bytes')
That's it! We've just implemented a fundamental aspect of JPEG compression. It's crucial to remember that actual JPEG compression involves several more sophisticated steps, but understanding DCT is a step in the right direction.