Sub topic 5
What happens when you scale an image
This chapter assumes you know what a pixel is, you can tell a raster file (such as .jpg or .png) from a vector file (such as .svg), and you have read the earlier chapters on the raster and vector models and on resolution. It covers what happens to each kind of image when its displayed size changes, why the results differ, and which mistakes follow from that.
Scaling means different things in each model
"Scaling" is one word for two different operations.
- A raster image is a fixed grid of pixels. Scaling it means producing a new grid with a different number of pixels. The software must invent or discard color values. This is called resampling.
- A vector image is a list of shapes with coordinates and styles. Scaling it means multiplying the coordinates by a factor and drawing the shapes again. No stored information is invented or discarded.
This difference explains nearly everything else in the chapter. A raster file has a native size, which is its pixel dimensions. A vector file has no native size in pixels. It describes proportions, and pixels appear only when the shapes are drawn on a particular screen or printer.
Scaling raster images
Scaling down (downsampling)
When you shrink a raster image, several source pixels must be combined into one output pixel. Software does this by averaging or by weighting nearby pixels. Detail is lost, but the result usually looks fine because the output has fewer pixels to fill. The loss is permanent if you save over the original. You cannot recover the 4000-pixel-wide version from a 400-pixel-wide copy.
Shrinking too aggressively with a poor method can cause aliasing. Fine patterns such as striped shirts or window grids can turn into jagged edges or moiré (false wavy patterns). Better resampling methods reduce this by looking at more neighboring pixels.
Scaling up (upsampling)
When you enlarge a raster image, the software must create pixels that were never captured. It estimates their colors from neighbors. The common methods are:
| Method | How it works | Typical result |
|---|---|---|
| Nearest neighbor | Copies the closest original pixel | Hard, blocky squares. Edges stay crisp. |
| Bilinear | Averages the 4 nearest pixels | Smooth but soft |
| Bicubic | Weights a 4 by 4 neighborhood (16 pixels) | Smoother and sharper than bilinear, with possible halos at edges |
| Lanczos | Uses a wider windowed filter | Sharp, with slight ringing near strong edges |
| AI upscaling | A trained model predicts plausible detail | Looks detailed, but the detail is guessed |
None of these methods adds real information. Interpolation (estimating new values between known ones) spreads the existing information over more pixels. The image gets bigger but not sharper. The visible effects are blockiness with nearest neighbor and blur with the smoother methods. AI upscalers can produce convincing texture, but it is invented. Do not rely on it where accuracy matters, such as in documents, faces used for identification, or scientific images.
Nearest neighbor is not always a bad choice. For pixel art and screenshots of interface elements, it keeps edges sharp and avoids blur.
The following Python example uses the Pillow library to compare the methods. Install it with pip install pillow.
from PIL import Image
# Create a tiny 4x4 test image with a hard black and white edge.
small = Image.new("RGB", (4, 4), "white")
for x in range(2):
for y in range(4):
small.putpixel((x, y), (0, 0, 0))
methods = {
"nearest": Image.NEAREST,
"bilinear": Image.BILINEAR,
"bicubic": Image.BICUBIC,
"lanczos": Image.LANCZOS,
}
for name, method in methods.items():
big = small.resize((64, 64), resample=method)
big.save(f"scaled_{name}.png")
# Sample a pixel near the edge between black and white.
print(name, big.getpixel((31, 32)), big.getpixel((32, 32)))Open the four output files. The nearest neighbor result has a sharp edge. The others show a gradient across the boundary, because they calculate in-between grays that the original never contained.
Scaling vector images
A vector drawing is scaled by arithmetic on its coordinates. If a circle has a radius of 40 units and you scale by 3, the radius becomes 120 units and the renderer draws a circle with that radius. The edge is calculated fresh at the output resolution, so it is as sharp as the device allows. This property is called resolution independence.
In SVG, the viewBox attribute defines the internal coordinate system. The width and height attributes define the size on the page. The browser maps one onto the other.
<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>Change width and height to 30 or to 3000 and the curve is equally clean. The file size does not change, because the file stores the same four numbers pair for the curve either way.
Things that do not scale the way you expect
Vector scaling has its own edge cases.
- Stroke width scales with the drawing. In the example above, the stroke is 4 units wide. At 3 times the size, it is drawn 12 pixels wide. For a line that must stay a constant thickness, such as a map outline or a diagram border, SVG offers
vector-effect="non-scaling-stroke"on the element. - Embedded raster images stay raster. An SVG or PDF can contain a bitmap. That bitmap still blurs when enlarged, even though the file extension suggests a vector. A "vector" logo exported with an embedded photo is a common source of blurry output.
- Text depends on fonts. If a vector file uses live text and the font is not installed or embedded, a different font appears. Converting text to outlines (shapes) removes the dependency but makes the text uneditable and increases file size slightly.
- Effects may rasterize. Drop shadows, blurs, and some transparency effects can be rendered as bitmaps at a fixed resolution when exported to some formats. They then look soft when scaled.
- Fine detail can become too fine. A logo with thin lines is sharp at any size, but at 16 pixels wide those lines may be thinner than a pixel and render as faint gray smudges. Sharp rendering does not mean legible.
- Very complex vectors are slow. A map with millions of points scales perfectly but may render slowly, because every point is processed on every redraw.
Seeing the difference
The diagram shows what each model stores and what scaling does to it.
Resolution, display size and effective resolution
For raster images, the quality you see depends on how many source pixels cover each unit of output. This is effective resolution, measured in pixels per inch (PPI) at the final size.
effective PPI = pixel width of the image ÷ printed width in inches
A 1200-pixel-wide image printed 4 inches wide has 300 PPI, which is good for print. The same image printed 12 inches wide has 100 PPI, and the softness is visible at normal reading distance. Changing the PPI number in a file's metadata does not change the pixels. It only changes the size at which the software assumes you want to print.
Screens add one more factor. A high-density display uses a device pixel ratio (the number of physical pixels per CSS pixel) of 2 or 3. An image shown at 300 CSS pixels wide needs 600 or 900 source pixels to look sharp on those screens. Web developers supply multiple sizes with the srcset attribute, so each device downloads a suitable one.
<img src="photo-600.jpg"
srcset="photo-600.jpg 600w, photo-1200.jpg 1200w, photo-1800.jpg 1800w"
sizes="(max-width: 600px) 100vw, 600px"
alt="Mountain lake at sunrise"
width="600" height="400">A vector image needs no srcset, since one file serves every density.
Common errors and how to avoid them
- Enlarging a small raster file for print. A 200-pixel logo scaled to a banner cannot be repaired. Find the original vector file or a larger raster, or redraw the logo.
- Repeatedly resizing and saving. Each resize and each lossy save (such as JPEG) compounds the damage. Keep an untouched master and produce every size from it.
- Assuming an
.eps,.pdfor.svgfile is vector. Open it and check. If zooming in to 800 percent shows squares, the content is a bitmap inside a vector container. - Stretching with unlocked proportions. Scaling width and height by different factors distorts the image. Lock the aspect ratio unless distortion is the goal. In SVG,
preserveAspectRatiocontrols how the drawing fits a box of a different shape. - Trusting the DPI field. A file labeled 300 DPI with 600 pixels of width is still only 2 inches wide at 300 PPI.
- Ignoring small-size legibility. Test icons and logos at their smallest real size, not only at a zoomed preview.
Summary of behavior
| Situation | Raster | Vector |
|---|---|---|
| Enlarged 4 times | Interpolated, soft or blocky | Redrawn, sharp |
| Reduced to 25 percent | Detail lost, smaller file | Redrawn, same file size |
| Stored size | Grows with pixel count | Grows with number of shapes and points |
| Needs several sizes for different screens | Yes | No |
| Best used for | Photos, textures, pixel art | Logos, icons, diagrams, signage |
Exercise
A designer has a 900-pixel-wide PNG of a company logo and wants to print it 6 inches wide. (a) What is the effective PPI? (b) The printer asks for 300 PPI. What is the largest width in inches at which the file meets that? (c) What would you recommend instead of enlarging the PNG?
Show answer
(a) 900 pixels ÷ 6 inches = 150 PPI, which is too low for crisp print. (b) 900 ÷ 300 = 3 inches. (c) Ask for the original vector version of the logo (SVG, PDF, EPS or AI), or redraw or trace the logo as vector shapes. A vector file can be printed at 6 inches or at any larger size without losing sharpness. Check that it contains no embedded bitmap.
Exercise
You export an SVG that contains a photograph embedded as an image element, and a vector shape drawn over it. You scale the SVG to ten times its original size. Which parts stay sharp, and which become blurry?
Show answer
The vector shape stays sharp, because its coordinates are recalculated at the new size. The embedded photograph becomes blurry or blocky, because it is a raster image with a fixed number of pixels and must be interpolated when enlarged. The file is a vector container, but it is not entirely vector content.