Sub topic 8
Working out raw image size
Raw size is how many bytes an image takes in memory once it is decoded, before any file compression. It tells you whether a frame fits in a GPU buffer, how large a batch of training images will be, and how much a file format saves. This chapter assumes you can read a NumPy array shape and know that a byte is 8 bits. It builds on the earlier chapters on resolution, bit depth, and channels.
The core formula
Every sample is one number for one channel at one pixel. The raw size is the number of samples times the storage per sample:
raw bytes = width × height × channels × bytes per sampleTake a 1920×1080 RGB image at 8 bits per channel:
width, height, channels, bytes_per_sample = 1920, 1080, 3, 1
raw_bytes = width * height * channels * bytes_per_sample
print(raw_bytes) # 6220800
print(raw_bytes / (1024 * 1024)) # about 5.93 MiBNumPy reports the same figure through nbytes. The array shape is (height, width, channels), so height comes first:
import numpy as np
frame = np.zeros((1080, 1920, 3), dtype=np.uint8)
print(frame.shape, frame.nbytes) # (1080, 1920, 3) 6220800The tiny image from the earlier chapter works the same way. A 2×3 RGB image has 2 × 3 × 3 × 1 = 18 bytes.
import numpy as np
img = np.zeros((2, 3, 3), dtype=np.uint8) # (height, width, channels)
print(img.shape, img.dtype, img.nbytes) # (2, 3, 3) uint8 18Each factor and what changes it
- Width and height scale the size linearly each. Doubling both makes the image four times larger.
- Channels is 1 for grayscale, 3 for RGB, and 4 for RGB plus alpha (transparency). Adding alpha to an RGB image costs one third more memory.
- Bytes per sample comes from the data type.
uint8is 1 byte,uint16is 2, andfloat32is 4.
The last factor surprises people most. A model that converts a uint8 image to float32 for normalisation makes it four times larger in memory. The pixel content has not changed.
| Image | Calculation | Bytes |
|---|---|---|
| 640×480 grayscale, uint8 | 640 × 480 × 1 × 1 | 307,200 |
| 1920×1080 RGB, uint8 | 1920 × 1080 × 3 × 1 | 6,220,800 |
| 1920×1080 RGBA, uint8 | 1920 × 1080 × 4 × 1 | 8,294,400 |
| 3840×2160 RGB, uint8 | 3840 × 2160 × 3 × 1 | 24,883,200 |
| 3840×2160 RGB, uint16 | 3840 × 2160 × 3 × 2 | 49,766,400 |
| 224×224 RGB, float32 | 224 × 224 × 3 × 4 | 602,112 |
Bytes, bits, and units
The formula gives bytes. Network speeds and some format specifications use bits, so divide or multiply by 8 when you convert.
Units are the second trap. A kilobyte (KB) is 1,000 bytes, and a kibibyte (KiB) is 1,024 bytes. Likewise a megabyte (MB) is 1,000,000 bytes and a mebibyte (MiB) is 1,048,576 bytes. Operating systems and tools mix the two. The 1080p frame above is 6.22 MB or 5.93 MiB, and both are correct. State the unit you use.
Bit depths that are not whole bytes
Not every bit depth fits in whole bytes, and two cases come up often.
Packed low bit depths. A binary image has 1 bit per pixel. If the file format packs eight pixels into each byte, a row of width pixels takes ceil(width / 8) bytes. A 1000×1000 binary image packs into 125 bytes per row, or 125,000 bytes in total. A NumPy boolean array of the same shape uses 1 byte per element, so it takes 1,000,000 bytes. Use np.packbits if you need the packed form.
Padded high bit depths. A 10-bit or 12-bit sensor value rarely gets exactly 10 or 12 bits in memory. Most software stores it in a 16-bit container. The size is then calculated as if the depth were 16 bits, and the top bits stay unused. The numbers still range only from 0 to 1023 (10-bit) or 0 to 4095 (12-bit).
Row padding and stride
Some formats and libraries align each row to a boundary, often 4 bytes, so the CPU or GPU can read rows efficiently. The distance in bytes from the start of one row to the start of the next is the stride. It can be larger than width × channels × bytes per sample. The bitmap (BMP) format is a well-known example. Its rows are padded to a multiple of 4 bytes.
def padded_row_bytes(width, channels, bytes_per_sample, align=4):
row = width * channels * bytes_per_sample
return (row + align - 1) // align * align
width, height = 5, 3
row = padded_row_bytes(width, channels=3, bytes_per_sample=1)
print(row) # 16 (15 bytes of pixels + 1 byte of padding)
print(row * height) # 48, not 5 * 3 * 3 = 45A buffer that you assume is width × height × channels bytes can then be too small. Read the stride from the library instead of computing it. In NumPy, array.strides gives it for each axis, as in the earlier memory-layout chapter. Many image APIs, such as camera and video frame buffers, expose a separate stride value.
Subsampled formats
Chroma subsampling stores colour at lower resolution than brightness, so the "channels × depth" rule no longer applies per pixel. In the YUV 4:2:0 layout with 8 bits per sample, the Y (brightness) plane is full size. The two colour planes have half the width and half the height each.
width, height = 1920, 1080
y_plane = width * height
chroma_plane = (width // 2) * (height // 2)
total = y_plane + 2 * chroma_plane
print(total) # 3110400
print(total / (width * height)) # 1.5 bytes per pixelThat is half the size of the 6,220,800-byte RGB frame. The chroma subsampling chapter explains why the eye tolerates this loss.
Where raw size matters in practice
- Video and cameras: a stream at 30 frames per second of 1080p RGB needs 30 × 6,220,800 ≈ 187 MB per second uncompressed. This is why video is always compressed.
- Batches: a training batch of 32 images at 224×224×3 float32 takes 32 × 602,112 = 19,267,584 bytes (about 18.4 MiB), before any model weights.
- Intermediate copies: each resize, colour conversion, or dtype change typically allocates a new array. Peak memory can be several times the size of one image.
- Compression ratio: compare a file's size with the raw size to see what a format saves. A 6,220,800-byte frame stored as a 600,000-byte JPEG has a ratio of about 10:1.
Common errors
- Mixing up the order of height and width. A 1920×1080 image has shape
(1080, 1920, 3). - Forgetting alpha. A PNG may load as 4 channels, not 3.
- Counting a 16-bit or float32 array as 1 byte per sample.
- Ignoring row padding when sizing a raw buffer.
- Mixing MB and MiB when comparing numbers from different tools.
Exercise
A phone photo is 4032×3024 pixels, RGB, 8 bits per channel. What is its raw size in bytes and in MiB? How large is it if you convert it to float32 for a model?
Show answer
4032 × 3024 × 3 × 1 = 36,578,304 bytes, which is about 34.9 MiB (36,578,304 / 1,048,576). As float32 each sample takes 4 bytes, so the size is 36,578,304 × 4 = 146,313,216 bytes, about 139.5 MiB.
Exercise
A 1-bit mask of 1000×1000 pixels is stored with 8 pixels packed per byte. How large is it? How large is the same mask as a NumPy bool array?
Show answer
Each row needs ceil(1000 / 8) = 125 bytes, so the packed mask takes 125 × 1000 = 125,000 bytes. A bool array uses 1 byte per element, so it takes 1,000,000 bytes, which is eight times more.