## Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Representing Data with Images and Sound: Bringing Data to Life. Join the course to learn more.
2.5

# File size calculation

Millions of images are being shared via social media in the time it takes you to read this sentence: the data in an image can be transferred within an instant. How are we able to send images so quickly? In this section, you are going to discover why the size of an image matters, and how to calculate the file size of bitmap images.

## What is file size, and why is it important?

As mentioned previously, file sizes range from bits, bytes, and kilobytes (the smallest sizes) to (and beyond) gigabytes, terabytes, and petabytes (the bigger ones).

These days, fast internet connections have enabled us to transmit images quickly. If transmission time is important, the image should be as small as possible.

## File size factors

The higher the number of pixels within an image, the more space you need to store it.

More pixels = greater image file size

### Resolution

The number of pixels in an image is known as its resolution. This is often represented as width × height and, in most operating systems, you can look it up in the image file properties

### Classroom Activity: find the resolution

Ask your students to find the resolution of one or many images within a shared folder. In this way, you can smoothly introduce the topic of metadata.

1. Right-click (or ctrl+click if you’re using a Mac) on the emoji above
2. Save the image on your computer
3. Open the folder containing the image, and right-click (ctrl+click for a Mac) on the image
4. Click properties
5. Find the image height and width
6. Calculate the image resolution

Along with the resolution, you may have found some other information about the image. This information is commonly known as metadata.

When you take a photo with your phone, you might be storing more data than you expect. As well as the image itself, your phone may also save information such as the camera settings, the date and time, and possibly even your location!

If you send the picture to a friend and they take a screenshot of it, the image data itself (the picture) won’t change, but some of the image properties (e.g. how the image was taken and when) will. Information on the properties of an image that aren’t part of the image is known as metadata.

Metadata is information about the data, rather than the actual data itself.

Teacher tip: consider asking students to think about what other types of metadata might be captured by various mobile apps.

### Bit depth

The second factor affecting image file size is bit depth. Bit depth is the storage space each pixel needs to represent the range of different colours.

Imagine a black-and-white image. Each pixel can only be either black or white, so we can represent this image with two colours, and therefore with one binary bit: 1 = white, and 0 = black.

A colour image needs more bits.

Binary bits available for colour Number of colours we can represent for a pixel
1 2, black and white
2 4, e.g. black, white, and two greys
3 8
4 16
8 256
16 65536
24 16.7 million

Do you spot the pattern? What effect would this have on the image size?

Images you take with online apps, for example Instagram or Snapchat, are likely to be stored with 16- or 24-bit colour depth. This means each pixel can use one of 65536 or 16.7 million colours, respectively. Wow!

More colours = more bits needed = more file storage space required

So if more colours mean a larger file size, why would we even use 24-bit colour depth?

Imagine you’re working for a hot air balloon company and you want to create a digital advert to show off all your wonderful balloons. The advert will be seen by millions of people from all around the world. Here, using 24 bits (16.7 million colours) to create the most vibrant and accurate image on your advert makes complete sense! Image quality and small file size are two competing goals that must be balanced for every image.

## Size calculations

File size can be expressed as the resolution (the image width multiplied by the image height) multiplied by the bit depth (the number of bits needed to store colours).

Image file size = resolution * bit depth

Files usually also have some metadata, which increases the file size slightly, but we will ignore this for now.

### Activity: How big is your smiley?

Your goal is to find the total size of the smiley image, inclusive of its bit depth. Once again, open up the smiley image you downloaded for this step’s first activity.

1. Find the resolution by multiplying the height by the width
2. Calculate the file size of the smiley image using the following bit depths:

a. 1

b. 2

c. 8

d. 16

e. 24

### Activity: use Python to calculate image file size

This Python script works out the width and height of an image:

from PIL import Image

im = Image.open('myimage.png') #path to image file

width, height = im.size

resolution = width * height

print("The image resolution is: ", width, " x ", height)
print("The resolution is: ", resolution)


Can you extend the code to:

• Calculate the resolution of another file?
• Ask the user to input a fixed bit depth and then update your output calculation to include it?
• Calculate the file sizes of all the images within a given folder?

Research task: can you write/find some code to make the Python script automatically find the bit depth of the image(s)? This forum conversation might help get you going.

Most importantly, get collaborative by sharing your code in the comments, ideally using pastebin to preserve the formatting.