Sub topic 1
How computers represent images
This chapter gives the working model behind every digital image. It frames the two storage approaches, raster and vector, that the rest of the tutorial examines in detail.
Prerequisites
This chapter assumes you:
- Have used image files on a computer, such as opening, saving, and resizing them.
- Know that a screen pixel is one tiny dot of light on a display.
- Recognize common file extensions such as
.jpg,.png, and.svg.
No programming experience is required. The code samples are short, and each one is explained.
From picture to numbers
A computer stores only numbers. To keep an image, it must turn what you see into numbers that can be saved, copied, and redrawn later. Every image format is an answer to one question: what numbers do we store so the picture can be rebuilt?
Two answers dominate:
- Raster storage records the color of every small cell in a grid. The picture is the grid.
- Vector storage records instructions for drawing shapes. The picture is the recipe.
The two answers differ in what they keep. A raster file keeps the result of drawing. A vector file keeps the method of drawing. Most of the differences you will meet later follow from this: how files scale, how big they get, how you edit them, and what each is good for.
Notice the last step. Your screen is a pixel grid, so vector images also end up as pixels. The difference is when that happens. A raster file already contains the pixels. A vector file is turned into pixels each time it is displayed, at whatever size is needed. This is called rendering, or rasterizing.
The raster idea in brief
A raster image is a rectangle divided into rows and columns of pixels. Each pixel holds one color value. A common choice is three numbers, for red, green, and blue, each from 0 to 255. The file records these values in order, row by row.
This makes two facts true:
- The size is fixed by the grid. A 1920 by 1080 image has exactly 2,073,600 pixels. There is no detail finer than one pixel.
- Storage grows with the pixel count. More pixels means more numbers to store.
You can compute the raw size directly:
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 uncompressedThe formula is width times height times bytes per pixel. A single uncompressed Full HD frame is almost 6 MiB. Photo files are much smaller than this because of compression, covered in a later chapter. Compression changes how the numbers are packed. It does not change the grid model.
Rasters suit images where color varies continuously and in complicated ways, such as photographs. A camera sensor measures light at a grid of points, so a raster is a natural fit. The cost is that the grid is the whole truth about the image. If you enlarge it, the software has to invent new pixels, and it can only guess.
The vector idea in brief
A vector image stores a list of shapes. Each shape is described by points, the lines or curves that join them, and style properties such as fill color, stroke color, and stroke width. The file contains no pixels at all.
SVG, a text-based vector format, makes this easy to see:
<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>Read it as instructions:
viewBox="0 0 200 100"defines a drawing space 200 units wide and 100 units tall, starting at the origin.<rect>says: draw a rectangle whose top-left corner is at (10, 10), 80 units wide and 80 tall, filled with blue.<circle>says: draw a circle centered at (150, 50) with radius 40, filled with red.
The file has no resolution. The numbers describe geometry, so a renderer can draw the same circle with 20 pixels or 20,000 pixels across and get a clean edge both times.
Curves and paths
Straight lines and basic shapes are not enough for logos and illustrations. Vector formats also describe curves using Bezier curves. A cubic Bezier curve is defined by a start point, an end point, and two control points that pull the curve toward them without the curve usually touching them.
<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>In the d attribute, M10 80 moves to the start point (10, 80). C 40 10, 65 10, 95 80 draws a cubic curve using control points (40, 10) and (65, 10), ending at (95, 80). The viewBox is 100 by 100, but the image is displayed at 300 by 300. The renderer scales the coordinates by three. Changing width and height to any other value keeps the line crisp, because the renderer recomputes the curve for the new size.
Comparing what is stored
The table below summarizes the core difference. Later chapters expand on each row.
| Question | Raster | Vector |
|---|---|---|
| What is stored? | A color for each pixel | Shapes, points, curves, and styles |
| Where does detail come from? | The pixel grid | The geometry and styles |
| Does it have a fixed size? | Yes, its pixel dimensions | No, it can be drawn at any size |
| File size depends on | Pixel count and compression | Number and complexity of shapes |
| Typical content | Photographs, scans, textures | Logos, icons, diagrams, illustration |
| Typical formats | JPEG, PNG, GIF, WebP | SVG, PDF, EPS, AI |
How an image reaches your screen
Seeing the pipeline helps explain behavior that otherwise looks odd.
For a raster, the software decodes the stored grid. If the display area is a different size from the grid, it resamples: it computes new pixel values by averaging or interpolating neighbors. Resampling cannot add real detail. For a vector, the software renders from the instructions at exactly the target size, so no detail is guessed.
You can see both models in the same web page. Both are placed with the same img element:
<img src="logo.svg" alt="Company logo" width="120">
<img src="photo.jpg" alt="Mountain lake at sunrise" width="600" height="400">The browser treats them identically at the markup level. Underneath, the logo is rendered from shapes and the photo is decoded from pixels. The pairing is also the usual choice: a vector for the logo, a raster for the photograph.
Common misunderstandings
- "Vector images are not made of pixels." On a screen, every image is pixels in the end. The accurate statement is that a vector file does not store pixels.
- "Vector is always better because it scales." Scaling is one property. A photograph cannot be practically described as a short list of clean shapes, so forcing it into vector form gives huge files or a posterized look.
- "File extension tells you the model." Mostly, but not always. A PDF or an AI file can contain vector shapes, embedded raster images, or both. An SVG can also embed a raster image inside it. Check the content, not only the name.
- "Higher resolution fixes a bad raster." Increasing pixel dimensions by resampling spreads existing information over more pixels. It does not recover detail the grid never captured.
Choosing a model: first-pass rules
Later chapters give full decision criteria. As a first pass, ask what the content is:
- Is it a real-world scene captured by a camera or scanner? Use a raster.
- Is it built from clean shapes with flat or simple gradient colors, and might it be used at many sizes? Use a vector.
- Does it mix both, such as a poster with a photograph and a logo? Use both. Layout tools combine them in one document.
Summary of the model
- Computers store images as numbers. The two basic choices are a grid of colors (raster) or a set of drawing instructions (vector).
- Raster detail is limited by the pixel grid, and raw size is width times height times bytes per pixel.
- Vector detail comes from geometry, so a vector image has no built-in resolution and is rendered to pixels at display time.
- Both end as pixels on a screen. They differ in what the file keeps.
Exercise
A 1000 by 500 pixel image uses 3 bytes per pixel and is stored uncompressed. How many bytes does it need? Then, in one sentence, explain why a vector logo drawn with the same visual size would not follow this formula.
Show answer
1000 × 500 × 3 = 1,500,000 bytes, about 1.43 MiB. A vector logo stores shape descriptions (points, curves, styles), not one color per pixel, so its size depends on how many shapes it has and how complex they are, not on the size at which it is displayed.
Exercise
You have a photograph of a street and a company logo made of three flat-colored shapes. For each, which storage model is the natural first choice, and why?
Show answer
The photograph should be a raster, because it has continuous, complex variation in color and detail that a pixel grid captures well and shapes cannot describe compactly. The logo should be a vector, because it is made of a few clean shapes and may need to appear at many sizes without losing sharpness.
Exercise
In the SVG path M10 80 C 40 10, 65 10, 95 80, which points are the control points, and what happens to the curve's sharpness if you display the SVG at 300 by 300 instead of 100 by 100?
Show answer
The control points are (40, 10) and (65, 10). The start is (10, 80) and the end is (95, 80). The curve stays sharp at any display size, because the renderer recomputes the curve from the geometry at the new size instead of stretching stored pixels.