Skip to content

Fix image resolution when the SVG file is given as a relative file name - #458

Open
entity-0-0-2 wants to merge 1 commit into
Kozea:mainfrom
entity-0-0-2:fix-relative-image-base
Open

entity-0-0-2 wants to merge 1 commit into
Kozea:mainfrom
entity-0-0-2:fix-relative-image-base

Conversation

@entity-0-0-2

@entity-0-0-2 entity-0-0-2 commented Oct 10, 2026 •

Copy link
Copy Markdown

Fixes #420.

When the SVG file is passed as a bare relative file name, any relative image href used to fail with a file-not-found error pointing at the root of the filesystem, so users had to pass absolute paths to get images to load.

The image drawing code built the base URL as os.path.dirname(node.url) + '/' (cairosvg/image.py:24). With node.url equal to bug.svg, dirname returns an empty string and the base becomes /, which parse_url then treats as an existing directory and joins the href onto, giving /bug_photo.jpg. This came in with 0a73c39 (Add URLFetcher, 2016-11-02); before that the code used os.path.join(os.path.dirname(node.url), url), where an empty dirname is harmless. Passing a directory as input hid the problem, which is why it kept resurfacing only for plain file names.

The fix passes node.url itself as the base and lets parse_url do its documented job of taking the folder part of the base. Absolute paths, file URLs, nested folders and remote URLs resolve exactly as before.

A test renders an SVG referencing a sibling image from a bare relative file name; it failed with a URLError on /image.png before the change and passes after it.

os.path.dirname turned a bare file name into an empty string, and the
appended slash made the base URL the root directory, so relative image
hrefs were looked up at / instead of next to the SVG file. Pass the
document URL as base and let parse_url resolve against it.
@liZe

liZe commented Oct 10, 2026

Copy link
Copy Markdown
Member

Pull requests are not advertisements.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fail of image path resolution in a defs/use context

2 participants