Skip to main content

3. Compression

Compresses are a way to reduce the size of files. There are two main types of compresses:

  • Image Compression
  • Video Compression

3.1. Image Compression

I ask DeepSeek more about image compression.

What are the image compression techniques, and what image formats are there?

I got the following response:

Alright, I need to figure out how to explain image compression techniques and formats based on the search results provided. Let's start by understanding what image compression is from the sources.

From Wikipedia (source 0), it's about reducing storage or transmission costs using algorithms that consider visual perception and statistical properties. So, it's not just generic data compression but tailored for images.

TechTarget (source 3) mentions lossless formats like BMP and GIF. But I remember seeing elsewhere that while GIF is technically lossless in some aspects, it can lose color information because of the limited palette. That’s a bit confusing, so I should note that.

Looking at sources 1 and 4-5, they discuss encoder processes and techniques. The flowchart involves transforms and encoding, which sounds like something from GeeksforGeeks (source 1). They also mention mathematical transformations, which are key in compression algorithms.

The Medium articles (sources 4 and 5) talk about lossy vs. lossless compression. Lossless doesn't lose data but still reduces size, while lossy does lose some data for smaller sizes. Examples include JPEG for lossy and PNG or TIFF for lossless.

Cloudflare's explanation (source 6) simplifies it to removing bytes or rewriting files, which ties back to the encoder-decoder process mentioned in other sources.

Educative.io (source 7) explains the two parts of compression: encoding (compressing) and decoding (decompressing). This is important because without both processes, you can't get the image back properly. So, encoders create the compressed file, and decoders recreate it from that file.

Hivo's blog (source 8) mentions maintaining visual quality while reducing size, which aligns with lossy compression since it prioritizes quality over exact data preservation. They also mention redundant data removal, which makes sense as images often have repetitive information.

The IRJET paper (source 9) covers the importance of image compression in digital processing for cost reduction and details both lossy and lossless techniques along with their algorithms and standards. This suggests that there's a lot of research and established methods in this field.

Putting it all together, I can structure the answer into sections: what image compression is, its types (lossy and lossless), key formats under each type, compression techniques involved, and why it matters. I should also mention how different industries or use cases might prefer one type over the other based on their needs for quality versus file size.

I need to make sure the explanation is clear and not too technical, but still informative enough to cover the main points from the sources. Including examples of formats under each compression type will help clarify things for someone who's new to this topic.

Image compression techniques are essential methods used to reduce the file size of digital images while maintaining acceptable visual quality. These techniques can be broadly categorized into two types: lossless and lossy compression.

1. Lossless Compression

  • Definition: Lossless compression reduces the file size without losing any information from the original image. The compressed image retains all the data of the original, making it ideal for scenarios where precision is critical.
  • Formats:
    • BMP (Bit Map): A proprietary format introduced by Microsoft, primarily used in Windows-based systems.
    • PNG (Portable Network Graphics): A lossless format that supports transparency and is widely used on the web.
    • TIFF (Tagged Image File Format): Commonly used for storing images with high color depth and quality.

2. Lossy Compression

  • Definition: Lossy compression reduces file size by removing some data, which can lead to a loss in image quality. This trade-off between quality and file size makes it suitable for applications where storage or transmission efficiency is paramount.
  • Formats:
    • JPEG (Joint Photographic Experts Group): The most widely used format for web images due to its high compression ratio and compatibility with nearly all devices.
    • GIF (Graphics Interchange Format): While technically lossless in some aspects, GIFs often lose color information due to their limited palette of 256 colors. They are popular for animations and simple graphics.

Image Compression Techniques

The process of image compression involves several steps and algorithms:

  • Encoding: This is the first part where a raw image is compressed into a smaller file using an encoder.
  • Decoding: The second part, where a compressed image is decompressed back to its original form or a close approximation.

Modern techniques employ sophisticated algorithms, such as mathematical transforms (e.g., Discrete Cosine Transform in JPEG), which play a vital role in achieving high compression ratios while maintaining visual quality. Advanced methods can achieve compression ratios from 10:1 to 100:1, depending on the specific requirements and settings.

Importance of Image Compression

Image compression is crucial for reducing storage costs, improving transmission speeds, and enhancing user experience on web pages and mobile applications. It allows images to load faster while balancing quality and file size effectively.

In summary, understanding the types of image compression (lossless vs. lossy) and their respective formats helps in choosing the right method based on specific needs for quality, compatibility, and efficiency.

Image compression techniques are used to reduce the size of images without losing

So I compressed all the images in the static/images folder

Before:

before

Compression Tool:

I used the following tool to compress the images:

tool

After:

after

The size of the images is reduced by 40%.

3.2. Video Compression [TODO]

Video compression techniques are used to reduce the size of video files without sacrificing quality. They are essential for optimizing video storage and transmission, especially in scenarios where bandwidth is limited.

3.1.1 Install ffmpeg

  • Ubuntu
sudo apt install ffmpeg
  • MacOS
brew install ffmpeg
  • Windows
choco install ffmpeg

In my case, I use the ubuntu. After I installed ffmpeg, I got the following information:

ffmpeg -version
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
configuration: --prefix=/usr --extra-version=0ubuntu0.22.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100

3.1.2 Compress video

ffmpeg -i input.mp4 -c:v libx264 -crf 30 -preset medium -c:a aac -b:a 128k output.mp4

After I compressed the video, The size of the video is reduced by 50%.

after

Resources