Sub topic 13
Converting between raster and vector
This chapter assumes you know how a raster image stores a pixel grid and how a vector image stores paths and styles. It also assumes you know the common formats (PNG, JPEG, SVG) and why scaling behaves differently in each model. Here you will see how to convert in both directions and what each direction costs.
The two directions are not symmetric
Converting vector to raster is a well-defined calculation. Converting raster to vector is an interpretation, and it can give different results depending on the settings.
| Direction | Name | Nature | Typical result |
|---|---|---|---|
| Vector to raster | Rasterizing | Exact, repeatable | Pixel grid at a size you choose |
| Raster to vector | Tracing (vectorizing) | Approximate, guided by settings | Paths that imitate the pixels |
Vector to raster: rasterizing
Rasterizing means drawing the shapes onto a pixel grid. You choose the pixel dimensions, and the software works out the color of every pixel. Pixels on a shape's edge are partly covered, so the software blends them with the background. This blending is called anti-aliasing, and it keeps diagonal and curved edges from looking jagged.
Rasterizing loses the shape descriptions. After export, the circle is only pixels, so you cannot change its radius without redrawing it. You also cannot enlarge the result without softness. Keep the vector original and export again at the larger size.
Here is a rasterizing step in Python. It assumes the cairosvg package is installed (pip install cairosvg).
import cairosvg
svg = """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="#e5484d"/>
</svg>"""
# The same source can be rasterized at any size you need.
for size in (64, 256, 1024):
cairosvg.svg2png(
bytestring=svg.encode("utf-8"),
write_to=f"circle_{size}.png",
output_width=size,
output_height=size,
)
print(f"wrote circle_{size}.png")All three PNG files are sharp, because each one is drawn from the shapes and not enlarged from another PNG.
A useful workflow is to export several sizes from one vector master, such as app icons at 16, 32, 180 and 512 pixels. The vector file stays the source of truth.
Raster to vector: tracing
Tracing analyzes a pixel image and builds paths that follow its regions. A typical tracer does four things:
- It reduces the image to a limited set of colors, or to black and white.
- It finds the boundaries between regions of different color.
- It fits curves through those boundaries, using as few points as it can within a tolerance.
- It fills each region with its color.
Tools such as Adobe Illustrator's Image Trace, Inkscape's Trace Bitmap, and the command-line tool Potrace all follow this idea. Settings control the result:
- Number of colors: more colors capture more detail but create more paths and larger files.
- Threshold or tolerance: a loose setting gives smooth shapes that drift from the source. A tight setting follows every pixel, including noise.
- Speckle or noise filtering: small regions below a size limit are dropped.
What works and what does not
Tracing gives good results on images that already look like vector art: logos, line drawings, silhouettes, and scanned signatures with strong contrast. It gives poor results on photographs. A photo has smooth gradients and thousands of subtle colors. Tracing it yields either a posterized look with flat color bands, or a huge file with so many paths that it is heavier than the original JPEG.
A low-resolution source also limits the result. If a logo is a blurry 100-pixel PNG, the tracer fits curves to the blur, and the edges come out wobbly. Start from the largest, cleanest source you have. If you can, redraw the logo by hand over the raster as a template. For important brand assets, a manual redraw is often better than automatic tracing.
Checking a conversion
After tracing, inspect the file:
- Zoom to 400% or more and look at curves and corners.
- Count the anchor points. A simple logo with thousands of points was traced too tightly.
- Compare colors with the original, since the reduction step can shift them.
- Check the file size. A traced SVG larger than the source PNG suggests the wrong tool for the image.
Exercise
A client sends only a 300 x 300 pixel PNG of their logo and asks for a version that prints sharply on a banner. Which direction of conversion do you need, what two risks should you check for, and what would be a better alternative to automatic tracing?
Show answer
You need raster to vector, which is tracing. The risks are that a low-resolution source gives wobbly or lumpy curves, and that colors or small details may be lost or changed. Check the result at high zoom and compare colors with the original. A better alternative is to redraw the logo manually as vector paths, using the PNG as a template, or to ask the client for the original vector file.
Exercise
You rasterize an SVG icon to a 64-pixel PNG, and later need a 512-pixel version. Should you enlarge the PNG or export again? Why?
Show answer
Export again from the SVG. The PNG holds only 64 by 64 pixels, so enlarging it invents pixels and looks soft. The SVG still holds the shapes, so rasterizing at 512 pixels produces a sharp image.