Compression Simulator
Compare lossless dictionary compression of text with lossy compression of an image, and see the size saved and the data lost.
Choose a compression type
Dictionary compression
Each different word is stored once in a dictionary. The text is replaced by the index of each word.
Compressed text (indexes)
Decompressed
the cat sat on the mat and the cat saw the dog on the mat so the dog sat on the cat
Dictionary
| Index | Word | Uses |
|---|---|---|
| 0 | the | 7 |
| 1 | cat | 3 |
| 2 | sat | 2 |
| 3 | on | 3 |
| 4 | mat | 2 |
| 5 | and | 1 |
| 6 | saw | 1 |
| 7 | dog | 2 |
| 8 | so | 1 |
About This Simulation
This compression simulator compares the two kinds of compression. Lossless compression is shown with dictionary coding of text, where every word can be rebuilt exactly. Lossy compression is shown on a small greyscale image, where reducing resolution and shades saves space but permanently removes detail.
Features
- Dictionary compression of your own text with the index list and dictionary table
- Original and compressed sizes in bits with the percentage saved
- Check that decompressed text is identical (lossless)
- Lossy image compression by block averaging and fewer grey levels
- Side-by-side images with the number of changed pixels and average error
Topics covered
- Lossless Compression
- Lossy Compression
- Dictionary Coding
- Compression Ratio
- File Formats
Explanation
Compression reduces file size so that files use less storage and transfer faster. Lossless compression removes redundancy and can always be reversed exactly. Lossy compression discards data that people are unlikely to notice, giving much smaller files, but the original cannot be recovered.
How it works
- Dictionary (lossless): each different word is stored once in a dictionary, and the text is replaced by the index of each word. Repeated words cost only a few bits each.
- Decompressing looks each index up in the dictionary, giving back the exact text.
- Lossy (resolution): blocks of pixels are replaced by their average colour, so there are fewer pixels to store.
- Lossy (colour depth): each pixel is rounded to fewer shades, so each pixel needs fewer bits.
- Compression ratio = original size ÷ compressed size. Higher means better compression.
Why it matters
Exams ask you to choose between lossless and lossy compression and justify it. Text, program code and databases need lossless compression because every bit matters; photos, music and video use lossy compression because a small loss of quality is acceptable for a much smaller file.
Frequently Asked Questions
What is the difference between lossy and lossless compression?
Lossless compression lets the original data be rebuilt exactly. Lossy compression permanently removes some data to make the file smaller, so the original cannot be recovered.
Give examples of lossless and lossy formats.
Lossless: ZIP, PNG, FLAC, GIF. Lossy: JPEG, MP3, AAC, most video formats such as MP4 (H.264).
Why can't text use lossy compression?
Losing even one character can change the meaning of text or break a program, so text must be compressed losslessly.
Can compression make a file bigger?
Yes. If data has little repetition, the extra information needed (such as a dictionary) can outweigh the savings.
Related Simulations
Data Representation
Run-Length Encoding Simulator
Compress text or a black-and-white image with run-length encoding, step by step, and compare the sizes before and after.
Data Representation
Huffman Coding Simulator
Build a Huffman tree from character frequencies one merge at a time, read off the prefix codes and compare the bits with ASCII.
Data Representation
Bitmap File Size Simulator
Calculate image file size from resolution and colour depth, with a live preview of how colour depth changes the image.
Data Representation
Sound File Size Simulator
Calculate audio file size from sample rate, bit depth, channels and duration, and see sampling, quantisation and aliasing on a wave.

