Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to Image Compression

Image compression is a technique used to reduce the size of an image file while maintaining acceptable visual quality. This process minimizes the storage space required and optimizes the transfer of images over networks. Image compression techniques can broadly be categorized into lossy and lossless compression:

  1. Lossy Compression:

    • Reduces file size by removing some image data.
    • Results in minor degradation of image quality.
    • Commonly used formats: JPEG, WebP.
  2. Lossless Compression:

    • No reduction in file size (sometimes can increase it).
    • Preservation of same quality of original image.
    • Commonly used formats: PNG.

Purpose:

The primary purpose of image compression is to:

  • Optimize storage requirements.
  • Speed up image transmission over networks.
  • Enable faster processing in systems handling large numbers of images.

Importance:

Image compression is critical in fields like:

  • Web development: Faster page loads with smaller image files.
  • Medical imaging: Efficient storage of high-resolution scans.
  • Surveillance: Long-term storage of high volumes of footage.
  • Satellite imagery: Compression of large datasets for analysis.

Example Application:

Consider a high-resolution photograph shared on social media. Compression reduces its size for quicker upload/download, while still maintaining visual appeal.

Fundamentals of Image Compression

Image compression relies on reducing redundancy and irrelevance in image data to achieve smaller file sizes while maintaining reasonable quality. The process involves analyzing the structure of image data and applying mathematical techniques to minimize storage requirements. Below are the core principles:

1. Types of Redundancy in Images

  • Spatial Redundancy: Caused by repetitive pixel values in adjacent regions of an image. Compression techniques group similar pixels to reduce data size.
  • Spectral Redundancy: Found in color images where adjacent color channels (e.g., RGB) contain similar information.
  • Temporal Redundancy: Present in video frames where successive frames have minimal differences.
  • Psychovisual Redundancy: Exploits the limitations of human vision by discarding information that the eye is less sensitive to (e.g., subtle color changes).

2. Compression Methods

  • Lossless Compression:
    • Preserves all original data.
    • Examples: Run-Length Encoding (RLE), Huffman Coding, Arithmetic Coding, PNG format.
  • Lossy Compression:
    • Eliminates data deemed unnecessary for human perception.
    • Examples: Transform coding (JPEG uses DCT - Discrete Cosine Transform), Vector Quantization, WebP format.

3. Key Techniques

  • Run-Length Encoding (RLE):
    • Represents consecutive repeated pixel values with a single value and a count.
    • Example: AAAAABBBCC becomes 5A3B2C.
  • Transform Coding:
    • Converts image data into frequency domain using transformations like DCT or Discrete Wavelet Transform (DWT).
    • High-frequency details (less important) are compressed more than low-frequency details.
  • Entropy Coding:
    • Compresses data based on frequency of occurrence. Common techniques include Huffman and Arithmetic coding.

4. Image Compression Metrics

  • Compression Ratio: $$ CR = \frac{Original_Size}{Compressed_Size} $$

  • Peak Signal-to-Noise Ratio (PSNR):

    • Measures the visual quality of compressed images. Higher PSNR indicates better quality.
  • Mean Squared Error (MSE):

    • Calculates the difference between original and compressed images. Lower MSE indicates better quality.

5. Process of Image Compression

  1. Transform: Convert image data into a suitable domain (e.g., frequency domain via DCT).
  2. Quantization: Reduce precision of less important data (applied in lossy compression).
  3. Encoding: Apply entropy coding to store data efficiently.

Real-World Relevance:

  • Lossless compression is crucial in medical imaging where data accuracy is essential.
  • Lossy compression is widely used in web and multimedia to achieve significant size reductions.

Real-World Examples of Image Compression

1. Web Applications

  • Purpose: Faster page load times and reduced bandwidth consumption.
  • Application: Images on websites are compressed to optimize performance without sacrificing visual appeal.
  • Example:
    • Before Compression: A 4MB high-resolution photo on a webpage.
    • After Compression: Reduced to 400KB using JPEG compression, enabling quicker loading and smoother user experience.

2. Medical Imaging

  • Purpose: Efficient storage and transmission of large image datasets like CT scans, MRIs, and X-rays.
  • Application:
    • Lossless compression ensures no critical data is lost, which is essential for diagnosis.
  • Example:
    • A high-resolution X-ray image of 20MB is compressed to 10MB while maintaining pixel-perfect accuracy.

3. Satellite Imagery

  • Purpose: Reducing file sizes of high-resolution images for analysis and storage.
  • Application:
    • Satellite images captured for geographical mapping or environmental monitoring are compressed for storage and faster processing.
  • Example:
    • Before Compression: A raw satellite image of 50MB.
    • After Compression: Compressed to 5MB using JPEG2000, allowing quicker transmission to ground stations.

4. Photography

  • Purpose: Storing and sharing large volumes of photographs without occupying excessive space.
  • Application:
    • JPEG compression is commonly applied in cameras and smartphones to save storage while retaining visual quality.
  • Example:
    • A 12MB RAW image from a DSLR is compressed to a 2MB JPEG for easy sharing on social media.

5. Streaming Services

  • Purpose: Optimize image and video quality for streaming while conserving bandwidth.
  • Application:
    • Platforms like Netflix, YouTube, and Spotify compress multimedia content to balance quality and streaming speed.
  • Example:
    • Thumbnails and preview images are compressed to a few KB while retaining sharpness for mobile and desktop users.

6. Surveillance Systems

  • Purpose: Long-term storage of security footage.
  • Application:
    • Video streams from CCTV cameras are compressed to H.264 or H.265 format for efficient storage and playback.
  • Example:
    • A 24-hour surveillance video of 100GB is compressed to 10GB using advanced codecs.

Implementation

Lossless Image Compression

from PIL import Image
import gzip

def compress_image_lossless(input_path, output_path):
    """
    Compress an image to reduce its file size without losing quality.
    
    Parameters:
    input_path (str): Path to the input image file.
    output_path (str): Path to save the compressed image.
    """
    try:
        # Open the image file
        img = Image.open(input_path)

        # Save the image in PNG format (lossless compression)
        img.save(output_path, format='PNG', compress_level=9)
        print(f"Image compressed and saved to {output_path}")
    except Exception as e:
        print(f"Error compressing image: {e}")

# Example usage
if __name__ == "__main__":
    input_image_path = "input.jpg"  # Change this to your image file
    output_image_path = "lossless_output.png"  # Ensure it's a .png or other lossless format

    # Ensure the output directory exists
    # os.makedirs(os.path.dirname(output_image_path), exist_ok=True)

    compress_image_lossless(input_image_path, output_image_path)

    with open("lossless_output.png", "rb") as f_in:
        with gzip.open("compressed_output.gz", "wb") as f_out:
            f_out.writelines(f_in)

Output:

Original Image

Original Image

Compressed Image

Compressed Image

Lossy Image Compression

from PIL import Image
import os

def compress_image(input_path, output_path, quality=50):
    """
    Compress an image to reduce its file size.

    Parameters:
    input_path (str): Path to the input image file.
    output_path (str): Path to save the compressed image.
    quality (int): Quality level for the compressed image (1 to 100, where 100 is the best quality).
    """
    try:
        # Open the image file
        img = Image.open(input_path)

        # Compress and save the image
        img.save(output_path, optimize=True, quality=quality)
        print(f"Image compressed and saved to {output_path}")
    except Exception as e:
        print(f"Error compressing image: {e}")

# Example usage
if __name__ == "__main__":
    input_image_path = "input.jpg"
    output_image_path = "lossy_output.jpg"

    # Ensure the output directory exists
    os.makedirs(os.path.dirname(output_image_path), exist_ok=True)

    compress_image(input_image_path, output_image_path, quality=30)

Output:

Original Image

Original Image

Compressed Image

Compressed Image

Comparison of Outputs

Lossless Compression (PNG):

  • Quality: No loss in image quality.
  • File Size: Increased.
  • Use Case: Ideal for applications requiring exact quality, like medical imaging.
  • Original: 5MB → Compressed: 35MB
  • Visual: No visible change.

Lossy Compression (JPEG):

  • Quality: Some quality loss, visible artifacts at lower quality settings.
  • File Size: Significantly reduced.
  • Use Case: Suitable for web applications, social media.
  • Original: 5MB → Compressed: 2MB
  • Visual: Artifacts and slight blurring may appear.

Visual Comparison:

  • Lossless: No degradation, same as original.
  • Lossy: Loss of sharpness and details at higher compression levels, but acceptable for web use.

Here is the shortened version of your markdown on image compression:

BENEFITS AND LIMITATIONS

Benefits:

Effective Compression: Reduces image file size while maintaining good quality.
Computational Efficiency: Simple and fast, requiring minimal computational resources.
Storage Optimization: Helps save storage space and reduces bandwidth usage.

Limitations:

Quality Loss (Lossy Compression): May introduce visible artifacts and reduce image sharpness.
File Size (Lossless Compression): In some cases, may result in larger files or similar size as the original.
Limited to Certain Formats: Lossless compression is mainly used with PNG, while lossy compression is common in JPEG.

Comparison with Other Techniques

Lossless Compression (PNG): Preserves all image data with no quality loss, but may result in a larger or similar file size.
Lossy Compression (JPEG): Reduces file size significantly but sacrifices some quality, especially at low settings.
WebP: A modern format that offers both lossy and lossless compression with better file size reduction than JPEG and PNG.

Future Prospects

AI-Based Compression: Machine learning techniques could optimize image compression by analyzing the content to balance quality and size more effectively.
Real-Time Compression: Future advancements may enable real-time compression for streaming and video applications with minimal quality loss.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages