Sub topic 5
What happens when you scale an image
This chapter assumes you know what a pixel is, can tell a raster file from a vector file, and have seen the idea of resolution. Scaling is where the two models behave most differently, and it explains many practical choices later in the tutorial.
Scaling a raster image
A raster image stores a fixed grid of pixels. Its pixel dimensions, such as 400 × 300, are part of the data. The file holds no information about what lies between those pixels.
When you change the size, the software has to build a new grid. It does this by resampling: for each new pixel it estimates a color from nearby original pixels. Common methods are:
- Nearest neighbor copies the closest original pixel. It is fast and gives hard, blocky edges. It suits pixel art.
- Bilinear averages the four nearest pixels. The result is smooth but soft.
- Bicubic uses a 4 × 4 neighborhood of 16 pixels. It is sharper than bilinear and is the usual default for photos.
Making an image smaller discards data. This is called downsampling, and it usually looks fine because you are throwing away detail you cannot see at the smaller size.
Making an image larger is upsampling. The software has to invent pixels, so no real detail is added. Enlarged images look blurry or blocky. Repeated resizing makes it worse, because each pass adds more estimation error.
Scaling a vector image
A vector image stores shapes as points, curves and styles. It has no fixed pixel grid. The renderer draws the shapes at whatever size you request, so a curve is recalculated and drawn cleanly at each size. This is called resolution independence.
The SVG below stores the same drawing at 300 × 300. Change width and height to 30 or 3000 and the line stays sharp, because the viewBox maps the 100 × 100 drawing space onto the requested size.
<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>The path starts at (10, 80) with M. The C command draws a cubic Bezier curve using two control points, (40, 10) and (65, 10), and ends at (95, 80). Scaling multiplies every coordinate, and the curve is drawn again from the new numbers.
Comparing the two
| Aspect | Raster | Vector |
|---|---|---|
| Stored data | Pixel grid | Paths, points, styles |
| Enlarging | Loses sharpness | Stays sharp |
| Shrinking | Discards pixels | Recalculates shapes |
| File size when scaled | Changes with pixel count | Stays the same |
Common errors
- Assuming vector is always better. A vector file scales well, but a photo converted to vector does not gain detail. Scaling only helps shapes that were defined as shapes.
- Raising the DPI to enlarge. Changing the DPI label without resampling does not add pixels. It changes only the intended print size.
- Scaling with CSS and expecting sharp output. In the HTML below, the 120-pixel width is applied to whatever file you give it. A small raster logo displayed larger than its pixel size looks soft, while the SVG does not.
- Distorting proportions. Setting width and height independently stretches the image. Keep the aspect ratio, or set only one dimension.
- Thin strokes at small sizes. A vector stroke scales with the drawing, so a fine line can become too thin to see when the icon is small. Check small sizes by eye.
<img src="logo.svg" alt="Company logo" width="120">
<img src="photo.jpg" alt="Mountain lake at sunrise" width="600" height="400">Exercise
Exercise
A 200 × 100 pixel PNG logo is displayed on a web page at 800 × 400 pixels and looks blurry. A designer says the fix is to set the file's DPI to 300. Why will this not work, and what would fix it?
Show answer
DPI is only a label for intended print size. It adds no pixels, so the browser still has to upsample 200 × 100 pixels to 800 × 400 and estimate the missing ones. Two fixes work: supply a raster file with at least 800 × 400 pixels, or use an SVG version of the logo, which is redrawn sharply at any size.