NOVANews
๐Ÿ–ผ๏ธ Computing

How do computers store pictures?

A computer has no paint and no film. It stores every picture as a long list of numbers. Zoom in on pixels, see how three numbers make any colour, and find out how a photo gets squeezed down to a tenth of its size.

A famous painting of a girl in a blue headscarf, turned into big blocky squares of colour against a dark background, so that her face and scarf are only about a dozen squares wide
Vermeer's painting Girl with a Pearl Earring, shown as big blocky pixels. Each square holds just one colour, yet you can still tell what the picture shows. Photo: acagastya (pixelated version of a painting by Johannes Vermeer), CC0, via Wikimedia Commons

A picture made of squares

Look closely at the picture above. It is made of big, flat squares of colour. Each square is a pixel, short for 'picture element'. Every picture on a screen is a grid of pixels, only much smaller than these. A computer does not keep a picture the way you hang one on a wall. It keeps a list that says what colour each pixel should be.

The size of a picture is its width in pixels times its height in pixels. An image 800 pixels wide and 600 tall has 480,000 pixels. A phone camera might take a 12-megapixel photo. A megapixel is one million pixels, so that photo has about 12 million of them. The more pixels there are, the finer the detail you can see.

Pixels can stand for real places, too. The Bureau of Meteorology's satellite pictures of Australia's weather come from Japan's Himawari-9 satellite, which hovers about 35,800 kilometres above the equator. In its infrared pictures, each pixel covers a square about 2 kilometres across, and a new picture arrives every 10 minutes. When forecasters zoom in on a storm, they are looking at a grid of squares, just like the one above.

Black, white and binary

Computers only use two symbols, 0 and 1. Each one is called a bit, the smallest building block of storage. Think about a black-and-white picture. Every pixel is either black or white, so one bit per pixel is enough. Let 1 mean a black pixel and 0 a white one. A letter T on a grid five pixels wide and five tall becomes five rows of numbers: 11111, then 00100 four times. The top row is the bar of the T and the other rows are its stem.

Computers bundle bits into groups of eight. A group of eight bits is a byte. Eight bits can make 256 different patterns, so one byte can hold any whole number from 0 to 255. Remember that number, because it is about to matter.

Colour is three numbers

Colour needs more than one bit. Screens make colour from three lights: red, green and blue. This is called RGB. A pixel's colour is stored as three numbers, one for each light, each from 0 (light off) to 255 (full brightness). Each number fits in exactly one byte.

Here are some examples, written as red, green, blue. Pure red is 255, 0, 0. Yellow is 255, 255, 0, because red and green light mix to make yellow. White is 255, 255, 255, with all three lights on full, and black is 0, 0, 0, with all three off. A number in between, such as 100, gives a dimmer light.

With 256 choices for each of the three lights, one pixel can be any of 256 ร— 256 ร— 256 = 16,777,216 colours. That is about 16.7 million. A camera works the other way round. Its sensor measures how bright the red, green and blue light is at each spot and stores each measurement as a number. Your screen then shows the same three numbers as three tiny lights, and your eyes blend them. Space cameras do something similar, taking pictures through red, green and blue filters and combining them back on Earth.

How big is a picture file?

Now you can work out how much space a picture needs. Each pixel needs three numbers, so three bytes. Take a small photo of flowers that is 457 pixels wide and 360 tall. It is a test picture from Stanford University's computer science course. That is 164,520 pixels. Multiply by 3 bytes and you get 493,560 bytes, or about 493 kilobytes. A kilobyte is about a thousand bytes.

Now try a 12-megapixel phone photo. Twelve million pixels times 3 bytes is 36 million bytes, or 36 megabytes, for a single photo. A gigabyte is about a billion bytes, so a phone with 128 gigabytes could hold only about 3,500 photos that big. Yet phones hold many more. Something must be shrinking them.

Squeezing pictures smaller

That something is compression. One simple trick is run-length coding, which fax machines used to send black-and-white pages down phone lines. Take a row that starts with 4 white pixels followed by 11 black ones. Instead of listing 15 pixels one by one, the computer stores just two numbers: 4 and 11. It works best when pictures have long runs of one colour. In a photo, neighbouring pixels are rarely exactly the same, so photos need cleverer tricks.

There are two main kinds. Lossless compression, which PNG pictures use, packs the data more tightly, and when you open the file every pixel comes back exactly as it was. Lossy compression, which JPEG pictures use, throws away tiny details your eyes are unlikely to notice. JPEG works on blocks of 8 by 8 pixels, and its files are often 10 to 20 times smaller. The lost detail never comes back.

In the Stanford test, the 493-kilobyte flower photo shrank to about 48 kilobytes at a typical camera setting, roughly ten times smaller. At a very low quality setting it shrank to just 14 kilobytes, but its 8-by-8 blocks showed and speckles crept in around sharp edges. Compression is why pictures load quickly on a website and why your phone can keep thousands of photos.

๐Ÿ“š Words to know

Pixel
One tiny square of colour. Every picture on a screen is a grid of pixels, short for 'picture elements'.
Bit
The smallest piece of computer storage. A bit is either 0 or 1.
Byte
A group of 8 bits. It can hold any whole number from 0 to 255.
RGB
Red, green and blue. A screen mixes these three lights, each from 0 to 255, to make every colour.
Compression
Storing a file in less space. Lossless compression keeps every detail, and lossy compression throws some away.

๐Ÿงช Try this: send a secret pixel picture

  1. You will need grid paper, a pencil and a friend or family member. Draw an 8 by 8 square on the paper, so there are 64 little boxes.
  2. Colour in some of the boxes black to make a simple picture, such as a heart, a smiley face or the first letter of your name. Leave the other boxes white.
  3. Now turn the picture into numbers. On a separate piece of paper, write each row as eight digits: 1 for a black box and 0 for a white one. Do not let your friend see the grid. You should end up with eight rows of eight digits.
  4. Give the rows of numbers to your friend. Can they colour a fresh grid and rebuild your picture?
  5. Challenge: squeeze it. Rewrite each row as the lengths of its runs, starting with white. A row that is 3 white, 2 black and 3 white becomes 3, 2, 3. If a row starts with black, write 0 first, because the first run of white has no pixels. How many numbers did you need this time, compared with the 64 digits before? Which pictures squeeze best, and which hardly squeeze at all?

๐Ÿง  Quick check

4 questions
1 In a black-and-white picture stored as 0s and 1s, how much does each pixel need?
2 In RGB colour, what do the three numbers stored for each pixel stand for?
3 A picture is 100 pixels wide and 50 pixels tall. Stored in full with 3 bytes for each pixel, about how many bytes is that?
4 What does lossy compression do to a picture?

Where this comes from

We check our facts against trusted sources. Want to find out more? Try these:

Keep exploring