[1.3.2] Calc file size: image/sound

Calculating File Sizes for Images and Sound

Understanding how to calculate file sizes is essential in computer science, particularly when working with digital media such as images and sound files. Every digital file occupies storage space, and being able to predict file sizes helps us make informed decisions about storage capacity, bandwidth requirements, and system performance. Whether you are designing a website, developing a mobile app, or managing a media library, knowing how to calculate file sizes enables you to plan resources effectively and optimise performance.

File size calculations are based on fundamental principles of data representation. Digital images are composed of pixels, each storing colour information, whilst sound files store samples of audio captured at regular intervals. By understanding the factors that determine file size - such as image resolution, colour depth, sample rate, and duration - we can accurately predict storage requirements and make informed technical decisions about quality versus file size trade-offs.

Calculating Image File Sizes

Understanding Image File Components

Digital images are composed of pixels arranged in a rectangular grid. Each pixel stores colour information, and the total number of pixels combined with how much data is needed to represent each pixel determines the overall file size. To calculate an image file size, we need to know three key pieces of information: the image width (in pixels), the image height (in pixels), and the colour depth (measured in bits per pixel).

Colour depth determines how many different colours each pixel can represent. Common colour depths include 1-bit (black and white), 8-bit (256 colours), 24-bit (16.7 million colours - "true colour"), and 32-bit (24-bit colour plus transparency). Higher colour depths allow more realistic images but result in larger file sizes because more data is needed to store each pixel.

The Image File Size Formula

The formula for calculating the uncompressed size of an image file is:

File size (in bits) = Width × Height × Colour depth

Since file sizes are typically measured in bytes rather than bits, and there are 8 bits in a byte, we need to divide by 8:

File size (in bytes) = (Width × Height × Colour depth) ÷ 8

For larger files, we may want to convert bytes to kilobytes (KiB), megabytes (MiB), or other units. Remember that in computing, 1 KiB = 1,024 bytes and 1 MiB = 1,024 KiB = 1,048,576 bytes.

Worked Example: Image File Size

Problem: Calculate the file size of an uncompressed image with a resolution of 1920 × 1080 pixels and a colour depth of 24 bits per pixel.

Solution:

  1. Identify the values:
    • Width = 1920 pixels
    • Height = 1080 pixels
    • Colour depth = 24 bits per pixel
  2. Calculate the file size in bits:
    • File size = 1920 × 1080 × 24
    • File size = 49,766,400 bits
  3. Convert to bytes by dividing by 8:
    • File size = 49,766,400 ÷ 8
    • File size = 6,220,800 bytes
  4. Convert to more convenient units:
    • File size in KiB = 6,220,800 ÷ 1,024 = 6,075 KiB
    • File size in MiB = 6,075 ÷ 1,024 ≈ 5.93 MiB

Answer: The uncompressed file size is approximately 5.93 MiB (or 6.22 MB in decimal notation).

Understanding Colour Depth

Different colour depths are used for different purposes:

Colour Depth Bits per Pixel Number of Colours Typical Use
1-bit 1 2 (black and white) Simple graphics, text documents
8-bit 8 256 GIF images, icons, simple graphics
24-bit 24 16,777,216 Photographs, high-quality images (RGB colour)
32-bit 32 16,777,216 + transparency Images requiring transparency (RGBA)

Calculating Sound File Sizes

Understanding Sound File Components

Digital sound files store audio as a series of samples taken at regular intervals. When sound is recorded digitally, the analogue sound wave is measured thousands of times per second, and each measurement (sample) is stored as a number. The quality and file size of digital audio depend on four key factors: the sample rate (how many samples per second), the sample resolution or bit depth (how precisely each sample is measured), the duration (length of the recording), and the number of channels (mono or stereo).

The sample rate is measured in Hertz (Hz) and typically ranges from 8,000 Hz (telephone quality) to 48,000 Hz or higher (professional audio). The sample resolution is measured in bits per sample, with common values being 8-bit, 16-bit (CD quality), or 24-bit (professional audio). Stereo sound uses two channels (left and right), doubling the file size compared to mono.

The Sound File Size Formula

The formula for calculating the uncompressed size of a sound file is:

File size (in bits) = Sample rate × Sample resolution × Duration × Number of channels

Again, to convert to bytes, divide by 8:

File size (in bytes) = (Sample rate × Sample resolution × Duration × Number of channels) ÷ 8

The duration must be in seconds to match the sample rate units. If given in minutes, convert to seconds by multiplying by 60.

Worked Example: Sound File Size

Problem: Calculate the file size of an uncompressed sound file with the following properties: sample rate of 44,100 Hz, sample resolution of 16 bits, duration of 3 minutes, stereo (2 channels).

Solution:

  1. Identify the values:
    • Sample rate = 44,100 Hz (samples per second)
    • Sample resolution = 16 bits per sample
    • Duration = 3 minutes = 180 seconds
    • Number of channels = 2 (stereo)
  2. Calculate the file size in bits:
    • File size = 44,100 × 16 × 180 × 2
    • File size = 254,016,000 bits
  3. Convert to bytes by dividing by 8:
    • File size = 254,016,000 ÷ 8
    • File size = 31,752,000 bytes
  4. Convert to more convenient units:
    • File size in KiB = 31,752,000 ÷ 1,024 ≈ 31,008 KiB
    • File size in MiB = 31,008 ÷ 1,024 ≈ 30.28 MiB

Answer: The uncompressed file size is approximately 30.28 MiB (or about 31.75 MB in decimal notation).

Common Sample Rates and Resolutions

Different audio quality levels use different combinations of sample rates and resolutions:

Quality Level Sample Rate Sample Resolution Typical Use
Telephone 8,000 Hz 8 bits Voice calls, basic speech
AM Radio 22,050 Hz 16 bits Low-quality music, podcasts
CD Quality 44,100 Hz 16 bits Music, standard audio
Professional 48,000 Hz or 96,000 Hz 24 bits Studio recording, video production

Step-by-Step Calculation Process

Systematic Approach for Image Files

Follow these steps when calculating image file sizes:

  1. Identify all given values: Write down the width, height, and colour depth
  2. Ensure consistent units: Width and height should be in pixels, colour depth in bits per pixel
  3. Calculate total bits: Multiply width × height × colour depth
  4. Convert to bytes: Divide the result by 8
  5. Convert to appropriate units: If the number is large, convert to KiB or MiB for readability
  6. Check your answer: Does it seem reasonable? A high-resolution photo should be several megabytes

Systematic Approach for Sound Files

Follow these steps when calculating sound file sizes:

  1. Identify all given values: Write down sample rate, sample resolution, duration, and number of channels
  2. Ensure consistent units: Convert duration to seconds if given in minutes
  3. Calculate total bits: Multiply sample rate × sample resolution × duration × channels
  4. Convert to bytes: Divide the result by 8
  5. Convert to appropriate units: Convert to KiB or MiB as appropriate
  6. Check your answer: A 3-minute CD-quality stereo song should be approximately 30 MiB uncompressed

Practical Applications

Planning Storage Capacity

Understanding file size calculations helps you plan storage requirements. If you are designing a digital photo album app that stores images at 2048 × 1536 resolution with 24-bit colour, you can calculate that each uncompressed image requires approximately 9 MiB. If the device has 32 GiB of storage and 8 GiB is used by the operating system and apps, you know that approximately 2,700 photos could be stored (assuming no compression).

Bandwidth Calculations

File size calculations also help determine bandwidth requirements for streaming or downloading. If a music streaming service delivers CD-quality audio (44,100 Hz, 16-bit, stereo), it needs to stream approximately 10 MiB per minute of audio. On a connection that supports 5 Mbit/s (about 0.6 MiB/s), the service would need to use compression or buffer data in advance to avoid interruptions.

Quality vs Storage Trade-offs

By calculating file sizes at different quality levels, you can make informed decisions about quality versus storage trade-offs. For example, reducing image resolution from 4000 × 3000 to 2000 × 1500 quarters the file size whilst still maintaining good quality for most uses. Similarly, using a sample rate of 22,050 Hz instead of 44,100 Hz halves audio file sizes, which might be acceptable for voice recordings where high fidelity is less critical.

Common Pitfalls and Tips

Remember to Divide by 8

One of the most common errors when calculating file sizes is forgetting to divide by 8 to convert from bits to bytes. The formulas naturally give results in bits because colour depth and sample resolution are measured in bits, but file sizes are conventionally expressed in bytes. Always include this conversion step unless specifically asked for an answer in bits.

Watch Your Units

Be careful with units throughout your calculation. Duration must be in seconds (not minutes), and your final answer should use appropriate units (bytes, KiB, MiB) depending on the magnitude. A file size of 31,752,000 bytes is more clearly expressed as 30.28 MiB. Remember that 1 KiB = 1,024 bytes (not 1,000), and 1 MiB = 1,024 KiB.

Don't Forget Channels for Audio

When calculating sound file sizes, remember to multiply by the number of channels. Stereo audio has two channels and therefore twice the file size of mono audio with the same sample rate, resolution, and duration. If the question does not specify, mono (1 channel) is typically assumed.

Show Your Working

In examinations and coursework, always show your working clearly. Write down the formula you are using, substitute the values, and show each calculation step. This allows you to earn partial marks even if you make an arithmetic error, and it helps you identify mistakes when checking your work.

 Key Takeaways

  • Image file size (bytes) = (Width × Height × Colour depth) ÷ 8, where dimensions are in pixels and colour depth is in bits per pixel
  • Sound file size (bytes) = (Sample rate × Sample resolution × Duration × Channels) ÷ 8, where sample rate is in Hz, sample resolution is in bits, duration is in seconds, and channels is typically 1 (mono) or 2 (stereo)
  • Always divide by 8 to convert from bits to bytes, as colour depth and sample resolution are measured in bits but file sizes are conventionally expressed in bytes
  • Common image colour depths include 1-bit (black/white), 8-bit (256 colours), 24-bit (16.7 million colours), and 32-bit (24-bit plus transparency)
  • Common audio quality levels include telephone (8,000 Hz, 8-bit), AM radio (22,050 Hz, 16-bit), and CD quality (44,100 Hz, 16-bit), with stereo doubling the file size compared to mono
  • These formulas calculate uncompressed file sizes; actual files using compression (JPEG, PNG, MP3, etc.) are typically much smaller but understanding uncompressed calculations is essential for planning storage and bandwidth requirements