Sub topic 13
Converting Between Raster and Vector
This chapter assumes you know how a raster image stores a grid of pixels and how a vector image stores paths, points and styles. It also assumes you know what resolution means and what the common formats (JPEG, PNG, SVG, PDF) hold. Here you will learn how to move an image from one model to the other, what each direction costs, and the errors that show up most often.
The two directions are not symmetric
Converting vector to raster is routine and predictable. Converting raster to vector is an estimate, and the result is never a perfect copy of what was lost.
- Rasterizing turns shape descriptions into pixels. The computer knows exactly what the shapes are, so it only has to decide which pixels each shape covers and how strongly.
- Tracing (also called vectorizing) turns pixels into shape descriptions. The computer has only a grid of colors and must guess where the edges, curves and shapes were.
A useful rule is that rasterizing discards information (the shape descriptions) and tracing invents information (shapes that were never stored). Because of this, you can rasterize as often as you like from the same vector source. A traced result is only as good as the guess.
Vector to raster: rasterizing
Rasterizing happens whenever a vector shape is displayed on a screen or exported to a pixel format. Your browser rasterizes an SVG every time it draws it. When you choose File > Export as PNG or JPEG in a design tool, you are asking for a rasterization at a chosen size.
What the renderer does
The renderer takes a target pixel size and works out which pixels each shape covers. Three decisions affect the result:
- Pixel dimensions. You pick the width and height in pixels. This is the last moment the image is resolution independent, so choose it with the final use in mind.
- Anti-aliasing. Shape edges rarely line up with pixel boundaries. With anti-aliasing on, an edge pixel that is 40% covered gets a color blended 40% toward the shape color. With it off, every pixel is fully in or fully out, and diagonals look like staircases.
- Background and transparency. A vector canvas has no background by default. PNG and WebP can keep that transparency. JPEG cannot, so the exporter fills transparent areas, usually with white.
A worked example in code
The following Python script rasterizes an SVG at three sizes. It uses the CairoSVG library, which you can install with pip install cairosvg.
import cairosvg
svg = """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<rect x="10" y="10" width="80" height="80" fill="#2a6fdb"/>
<circle cx="50" cy="50" r="30" fill="#e5484d"/>
</svg>"""
for size in (64, 256, 1024):
cairosvg.svg2png(
bytestring=svg.encode("utf-8"),
write_to=f"shapes_{size}.png",
output_width=size,
output_height=size,
)
print("wrote", f"shapes_{size}.png")All three PNG files come from the same source and are equally sharp for their size. The viewBox defines a 100 by 100 coordinate space, and the renderer scales it to whatever output_width and output_height you ask for. Only the pixel count changes. The file with 1024 pixels per side stores about 256 times as many pixels as the 64-pixel one, so it is much larger before compression.
Choosing the export size
Export for the largest size at which the image will be used, and then scale down if needed. Scaling a raster down is safe. Scaling it up later is not, as the next section shows.
- Screens with high pixel density. Export at 2x or 3x the layout size. A 120 pixel wide logo shown on a 2x display needs a 240 pixel file.
- Print. Multiply the physical size by the required PPI. A 10 cm wide image at 300 PPI needs about 1181 pixels across (10 / 2.54 × 300).
- Anything that can stay vector. Keep SVG or PDF and let the viewer rasterize at the exact size it needs.
Things that change when you rasterize
- Live text becomes pixels. You can no longer edit the wording, search it, or select it. Keep the vector master.
- Strokes and effects are flattened. A stroke is no longer a line with a width. It is colored pixels. Changing its thickness means redoing the export.
- Layers and structure disappear. Individual shapes can no longer be selected or recolored separately.
- Thin lines can blur. A 1 unit line that lands between two pixel rows renders as two half-strength rows. Aligning shapes to the pixel grid before export gives crisper results for small icons.
Raster to vector: tracing
Tracing starts from pixels and builds paths. It is useful for logos that exist only as a low-resolution file, for scanned drawings, and for turning hand-drawn lettering into editable shapes.
How a tracer works
Most tracers follow the same broad steps.
- Reduce the colors. The image is simplified to a limited palette, or to black and white, so that regions of similar color become one region.
- Find the edges. The boundaries between regions are located as chains of pixels.
- Fit curves. Along each boundary, the tracer places points and fits Bézier curves through them. Fewer points make a smoother, simpler path. More points follow the original more closely but also copy its roughness.
- Fill and stack. Each region becomes a closed path with a fill color, and the paths are layered to rebuild the picture.
Tools that do this include Adobe Illustrator's Image Trace, Inkscape's Trace Bitmap, and the command-line tool Potrace for black-and-white images.
A worked example with Potrace
Potrace works on a one-bit (black and white) bitmap. This example converts a scanned drawing to SVG. It assumes Potrace and ImageMagick are installed.
# 1. Convert the scan to a clean black-and-white bitmap
magick scan.png -colorspace Gray -threshold 55% scan.pbm
# 2. Trace it to SVG, smoothing small corners
potrace scan.pbm --svg --output scan.svg --turdsize 5 --alphamax 1.0-threshold 55%decides which gray levels become black. A wrong threshold is the most common cause of a bad trace, because it decides where the shape edges are before tracing begins.--turdsize 5drops specks smaller than 5 pixels, which removes scanner dust.--alphamax 1.0controls corner smoothing. Lower values keep sharper corners, and higher values round them.
What tracing can and cannot recover
Tracing recovers shapes that were already clean, flat and high contrast: logos, line art, simple icons and silhouettes. It struggles with anything that depends on continuous tone.
| Source image | Trace quality | Why |
|---|---|---|
| Black logo on white, large | Very good | Few colors, sharp edges |
| Line drawing or signature | Good | Edges are clear, needs a good threshold |
| Flat-color icon | Good | Few regions with solid fills |
| Low-resolution logo (for example 100 px) | Fair | Edges are already blurred, so curves are guessed |
| Photograph | Poor | Thousands of tones produce huge, messy files |
| JPEG with heavy compression | Poor | Compression artifacts get traced as real shapes |
Common tracing problems
- Too many points. A noisy source produces thousands of tiny segments. The SVG may be larger than the original PNG, and it will be hard to edit. Increase smoothing or the speck-removal setting.
- Wobbly edges. If the source was blurry, the fitted curves wobble. Clean the source first by enlarging it with good resampling, increasing contrast, or both.
- Lost fine detail. Aggressive smoothing rounds off sharp corners and closes small gaps in letters.
- Merged or missing colors. With a small palette, two similar colors can merge into one region. With a large palette, a gradient becomes dozens of banded shapes.
- JPEG artifacts. Blocky noise around edges from lossy compression becomes extra shapes. Where possible, trace from a lossless source such as PNG or the original scan.
- A white box. Many tracers also trace the white background as a filled shape. Delete it, or set the tracer to ignore white, if you need a transparent result.
Another path: rebuild by hand
When a trace is not clean enough, the professional approach is to redraw the image over the raster. Place the low-resolution logo on a locked layer, then draw each shape with the pen tool and Bézier curves, using as few points as possible. This takes longer than automatic tracing, but it gives cleaner curves, correct geometry (true circles, equal stroke widths) and exact colors you can set from the brand specification. For type, identify the font and retype the text instead of tracing the letters.
Choosing a conversion strategy
Checking the result
After either conversion, check the output before delivering it.
- Rasterized output: open it at 100% zoom and check the edges for jaggedness, halos around transparent areas, and the correct pixel dimensions.
- Traced output: overlay the SVG on the original at high zoom. Check corners, small holes, and thin strokes. Count the points: a simple logo should have tens or a few hundred, not thousands.
- File size: compare. A trace that is larger than the source raster is a warning sign.
- Colors: for brand work, replace sampled colors with exact values from the style guide.
Summary of costs and gains
| Direction | You gain | You lose |
|---|---|---|
| Vector to raster | Universal support, photo-style effects, a fixed and predictable look | Resolution independence, editability of shapes and text |
| Raster to vector | Clean scaling, small files for flat art, editable shapes and colors | Exact fidelity, fine texture and tone, and any detail the tracer smoothed away |
Exercise
A client sends a 150 by 150 pixel PNG of their logo, a flat blue shield with white lettering, and asks for a version that can be printed on a 2 meter banner. The original Illustrator file is lost. Describe your approach, name one problem you expect from automatic tracing, and explain how you would fix it.
Show answer
The PNG cannot be enlarged to 2 meters without visible blur, so convert it to vector. Trace it (for example with Image Trace or Inkscape's Trace Bitmap) using a small palette of two or three colors, or redraw it by hand over the PNG on a locked layer. Because the source is only 150 pixels wide, edges are already blurred, so expect wobbly curves and rounded corners. Fix this by reducing points and adding smoothing, or by redrawing the shield and rectangles with the pen tool and shape tools so lines are straight and curves are true. Retype the lettering in a matching font rather than tracing it, and set the blue to the client's exact brand value. Then overlay the result on the original, deliver SVG or PDF, and keep it as the new master.
Exercise
You export the same SVG as a 400 pixel wide JPEG and a 400 pixel wide PNG. The shapes had a transparent background. What differs in the two files, and why?
Show answer
The PNG keeps the transparent background because PNG supports an alpha channel. The JPEG cannot store transparency, so the exporter fills the empty areas with a solid color, usually white. The JPEG also uses lossy compression, which can add faint artifacts around sharp edges, while the PNG is lossless and reproduces the rasterized pixels exactly.
Exercise
Why is it a mistake to trace a photograph and call it a vector version?
Show answer
A photograph has continuous tones and soft transitions, which a tracer can only approximate with many stacked, flat-colored shapes. The result usually has thousands of points, a large file size, a posterized look, and no real editing benefit. It does not gain the clean scaling that makes vector useful for logos and line art, so the photograph should stay a raster.