Sub topic 1
How computers represent images
Prerequisites
This chapter assumes you can do three things:
- Open and save image files on a computer.
- Say what a screen pixel is: one tiny dot of light on a display.
- Recognize common file extensions such as .jpg, .png and .svg.
You do not need any programming experience. The code samples are short and each one is explained.
The core problem
A computer stores only numbers. A picture has to be turned into numbers before it can be saved, sent or shown. There are two basic ways to do this.
- A raster image stores a grid of colored dots called pixels. Each pixel holds a color value.
- A vector image stores instructions for drawing shapes. Each shape is described by points, curves and styles such as fill color and line width.
Every image file you meet uses one of these models, and a few formats can hold both. The rest of this tutorial depends on this split, because it decides how an image scales, how large the file is, and what you can edit.
Storing a grid of pixels
In a raster image, the picture is divided into rows and columns. Each cell in the grid is a pixel, and each pixel stores a color. The common way to store color is three numbers, one each for red, green and blue, from 0 to 255. That takes 8 bits, or 1 byte, per channel, so 3 bytes per pixel.
The storage cost follows directly from this. An uncompressed image needs width × height × bytes per pixel:
width, height = 1920, 1080
bytes_per_pixel = 3 # 8 bits each for red, green and blue
size_bytes = width * height * bytes_per_pixel
print(size_bytes) # 6220800 bytes, about 5.9 MiB uncompressedA single full-HD frame is almost 6 MiB before compression. That is why raster formats rely on compression, which later chapters cover.
The grid has a fixed size. The image holds exactly as many pixels as it was created with. It does not contain any information about detail finer than one pixel.
Storing shapes
A vector image does not record pixels. It records a list of shapes. The SVG file below describes a blue square and a red circle in a few lines of text:
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
<rect x="10" y="10" width="80" height="80" fill="#2a6fdb"/>
<circle cx="150" cy="50" r="40" fill="#e5484d"/>
</svg>The rect element gives a corner position, a width and a height. The circle element gives a center point and a radius. The fill attribute sets the color. The viewBox defines the coordinate system the shapes are drawn in.
Curves work the same way. A path stores a start point and a curve defined by control points:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="300" height="300">
<path d="M10 80 C 40 10, 65 10, 95 80" fill="none" stroke="#222" stroke-width="4"/>
</svg>M10 80 moves to the start point. C 40 10, 65 10, 95 80 draws a cubic Bezier curve. It uses two control points that pull the curve, and ends at the last point. The file stores only these few numbers. When the image is displayed, software calculates which pixels to color. This step is called rasterizing, and it happens every time a vector image appears on a screen.
Because the drawing is computed at display time, changing width and height to 300 does not reduce quality. The viewBox scales the coordinates to fit.
Side-by-side summary
| Aspect | Raster | Vector |
|---|---|---|
| What is stored | Color of every pixel | Shapes, points, curves, styles |
| Size depends on | Pixel dimensions | Number and complexity of shapes |
| Best at | Photos and fine tonal detail | Logos, icons, diagrams |
| Detail limit | Fixed by the grid | None until it is drawn |
Common mistakes
- Treating the file extension as the model. A PDF can contain raster images, vector shapes, or both. Check what is inside.
- Assuming vector means "better". A photograph has millions of subtle color changes. Describing it as shapes would be huge and look unnatural.
- Assuming small pixel dimensions can be fixed later. Once a raster image is saved at a low size, the lost detail is gone.
Check your understanding
Exercise
A 1000 × 500 pixel image is stored as raw RGB with 3 bytes per pixel. How many bytes does it need, and why would a vector file of a simple two-shape logo be much smaller?
Show answer
1000 × 500 × 3 = 1,500,000 bytes, about 1.4 MiB. A raster file stores a value for every pixel, so size grows with pixel count. A vector file stores only a few numbers per shape, such as position, size and color, so a simple logo needs only a few hundred bytes.
Exercise
In the path M10 80 C 40 10, 65 10, 95 80, what do the numbers after C represent?
Show answer
Two control points (40,10 and 65,10) and the end point (95,80) of a cubic Bezier curve that starts at 10,80. The control points shape the curve, but the curve does not pass through them.