DEV Community

PerfectVector
PerfectVector

Posted on Originally published at perfectvector.com

Is That .svg Real? A 20-Line Check for Agent-Made SVGs

This post was drafted with AI assistance and reviewed by the PerfectVector team.

A file ending in .svg is not necessarily vector art. Some tools wrap a bitmap in an <svg> element: the file opens in a browser, scales without errors, and you can't edit a single shape, because there are none. If an AI agent hands you an SVG, check what's inside before you send it to a cutter, a print shop or a design tool.

Here's a check that needs only Python's standard library. It counts shape elements and path commands, and flags <image> elements whose reference is a data:image/ URI or a common raster filename (.png, .jpg, .webp, .gif). It's a heuristic, not proof that a file is vector-only:

import re, sys
import xml.etree.ElementTree as ET

def audit(path):
    root = ET.parse(path).getroot()
    tags = [e.tag.split('}')[-1] for e in root.iter()]
    shapes = sum(tags.count(t) for t in ('path', 'rect', 'circle', 'ellipse', 'polygon', 'polyline', 'line'))
    raster = 0
    for e in root.iter():
        if e.tag.split('}')[-1] == 'image':
            href = e.get('href') or e.get('{http://www.w3.org/1999/xlink}href') or ''
            if href.startswith('data:image/') or re.search(r'\.(png|jpe?g|webp|gif)(\?|$)', href, re.I):
                raster += 1
    cmds = sum(len(re.findall(r'[MmLlHhVvCcSsQqTtAaZz]', e.get('d', '')))
               for e in root.iter() if e.tag.split('}')[-1] == 'path')
    verdict = 'FAKE (bitmap inside)' if raster and not shapes else ('MIXED' if raster else 'vector')
    return {'shapes': shapes, 'path_commands': cmds, 'raster_images': raster, 'verdict': verdict}

for p in sys.argv[1:]:
    print(p, audit(p))
Enter fullscreen mode Exit fullscreen mode

We ran it on three files we built for this test. fake.svg is a 256×256 PNG (1,682 bytes) base64-embedded in an <image> tag, 2,449 bytes in total. handcoded.svg is a <rect> and a <circle>. The third is the fixed sample SVG that a PerfectVector API test key returns, which we used as a ready-made file with real paths.

File shapes path commands raster images verdict
fake.svg 0 0 1 FAKE (bitmap inside)
handcoded.svg 2 0 0 vector
API test sample 5 4 0 vector

As a second opinion, svgpathtools' svg2paths (which turns basic shapes into paths) found 0, 2 and 5 paths in the same files, matching our shape counts.

What this doesn't catch: <image> references without a raster extension, shapes pulled in with <use> from another file, bitmaps referenced from CSS (background-image), or a real vector that is a bad trace (thousands of tiny paths). It can also flag a vector data:image/svg+xml reference as a bitmap, and an empty SVG gets the "vector" label, because the script doesn't check visible artwork or editability. A real SVG can still be useless for cutting. Count nodes, too. So "vector" in the verdict only means "no bitmap found".

Where does a real vector come from? For simple geometry, an agent can write the shapes itself, and the audit passes. For a photo or a detailed logo, it needs a tracer. If you give your agent a tracing tool, for example through MCP, run this check on whatever comes back, whoever made the tool. We made the same point about a model writing SVG code versus tracing an image in an earlier post: Claude Can Write SVG. That Doesn't Mean It Vectorized Your PNG.

The longer, no-code version of this check (editor selection, embedded vs linked images, what to do next): How to Tell If an SVG Contains a Raster Image.

We build PerfectVector, an image-to-vector tool.

Top comments (0)