📘 CodingMarble Learn

Image and Sound Encoding: Pictures and Sound as Numbers

A computer keeps only numbers. A picture is a grid of pixels, and each pixel is stored as three numbers (red, green, blue). Colour depth is the number of bits per pixel: more bits, more colours. Sound is a wave measured many times a second. Sampling rate is how many measures per second, and bit depth is how finely each measure is stored. File size = number of values x bits for each.

🎬 Step-by-step story

  1. This picture is a grid of tiny coloured squares. Each square is one pixel.
  2. Zoom on one pixel. The computer does not keep a colour. It keeps numbers for this pixel.
  3. A colour is three numbers: red, green and blue, each from 0 to 255. Move the sliders and watch the pixel change.
  4. Now we cut the bits for each colour. Fewer bits means fewer colours, and the picture turns blocky.
  5. Sound is a wave. The computer measures the wave height again and again. Each measure is a sample. More samples draw the wave better.
  6. Free play: change the number of samples and the bits for each sample. See the file size grow.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Does a pixel really store a colour?

It stores numbers. The screen turns the numbers into light. In the 3D, the pixel colour changes only when you change the numbers.

Why is the top number 255 and not 100?

Each colour uses 8 bits. Eight bits count 256 values, so the scale runs from 0 to 255.

Why does the picture look blocky with fewer bits?

With fewer bits there are fewer colour steps. Nearby colours are forced into the same one, so you see blocks and bands.

Why do we need many samples for sound?

The sound wave moves up and down quickly. Few samples miss the turns. Many samples follow the wave closely.

What does bit depth change in sound?

It changes how many height levels each sample can use. With few bits the bars jump in big steps.

Pixels: a picture made of squares

A computer cannot store a drawing the way paper does. So it cuts the picture into a grid of tiny squares. Each square is a pixel (picture element). Each pixel holds one colour.

The grid size is the resolution. A picture 800 pixels wide and 600 pixels high has 800 x 600 = 480,000 pixels. More pixels give a sharper picture, but also a bigger file.

Pixels are stored row by row, like words in a book. Every pixel is just a number or a few numbers.

RGB: a colour as three numbers

A screen makes every colour by mixing three lights: red, green and blue. This is the RGB model. Each light has a strength from 0 (off) to 255 (full).

Why 255? Each number uses 8 bits, and 8 bits can count from 0 to 255, which is 256 values.

Colour depth: how many bits for each pixel

Colour depth (bit depth of a picture) is the number of bits used for one pixel. With b bits you can make 2b different colours.

Fewer bits means the colours are squeezed into steps, so a smooth sky turns into bands. Size of a picture = pixels x bits per pixel (divide by 8 for bytes).

Sound: sampling rate and bit depth

Sound is a wave of air pressure. A microphone turns it into a wave of electricity. The computer then samples it: it measures the height of the wave many times a second.

Sampling rate is the number of samples every second, in hertz (Hz). A CD uses 44,100 Hz. A phone call uses about 8,000 Hz, so it sounds thin.

Bit depth is how many bits store each sample. 8 bits give 256 height levels. CDs use 16 bits, which give 65,536 levels. More bits give a smoother, quieter background.

A good rule: to keep a sound, sample at least twice its highest pitch. People hear up to about 20,000 Hz, so 44,100 Hz is enough.

Size of sound = sampling rate x bit depth x seconds x channels (stereo has 2 channels).

Try it: measure a wave by hand

In the 3D, go to the last step. Set samples to 6, then to 48. See how the blue bars follow the wave better. Now lower the sound bits to 1 and see the steps. On paper: draw a 8 x 8 grid, colour it as a smiley with two colours, and write 0 or 1 in each square. You just encoded a picture with 1 bit per pixel.

Key formulas and definitions

Worked examples

1. How many colours can 1, 2 and 8 bits per pixel show?

2^1 = 2, 2^2 = 4, 2^8 = 256 colours.

2. A black-and-white picture is 10 x 10 pixels. How many bits and bytes is it?

Black and white needs 1 bit per pixel. 10 x 10 x 1 = 100 bits. 100 / 8 = 12.5 bytes.

3. Write pure yellow and dark grey in RGB (use 64 for dark grey).

Yellow = red + green = (255, 255, 0). Dark grey has equal values: (64, 64, 64).

4. Find the size of a 800 x 600 picture in true colour (24 bits).

Pixels = 800 x 600 = 480,000. Bits = 480,000 x 24 = 11,520,000. Bytes = 11,520,000 / 8 = 1,440,000 bytes, about 1.44 MB.

5. A voice is recorded at 8,000 Hz, 8 bits, one channel, for 10 seconds. How many bytes?

Bits = 8,000 x 8 x 10 x 1 = 640,000. Bytes = 640,000 / 8 = 80,000 bytes.

6. How big is 1 minute of CD sound (44,100 Hz, 16 bits, stereo)?

Bits = 44,100 x 16 x 60 x 2 = 84,672,000. Bytes = 84,672,000 / 8 = 10,584,000 bytes, about 10.6 MB.

Common mistakes

Practice quiz

1. What is a pixel?
2. How many colours can 4 bits per pixel show?
3. RGB stands for:
4. Sampling rate is measured in:
5. What happens if you use more bits for each sample?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

Why does a computer need to turn pictures and sound into numbers?

A computer can only store and work with bits (0 and 1). Numbers can be stored, copied and sent without loss, so everything is changed to numbers first.

Why is 44,100 Hz used for music?

People hear up to about 20,000 Hz. A sampling rate of more than twice that keeps all the sound we can hear, and 44,100 Hz does that.

Does a bigger picture file always mean a better picture?

No. A larger file has more pixels or more bits, which can look better, but a poor photo stays poor. Compression can also make files smaller.

Learn first

Learn next

Related lessons

All Computer Science lessons