Skip to content

Fix image extraction failing when --image-dir contains spaces - #2

Merged
mrgeoffrich merged 3 commits into
mainfrom
fix/image-dir-spaces
Mar 22, 2026
Merged

Fix image extraction failing when --image-dir contains spaces#2
mrgeoffrich merged 3 commits into
mainfrom
fix/image-dir-spaces

Conversation

@mrgeoffrich

@mrgeoffrich mrgeoffrich commented Mar 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Root cause: pymupdf4llm's utils.md_path() replaces spaces with underscores in the image save path, but creates the directory with the original name — so pix.save() fails with ENOENT on the sanitized path
  • Fix: When image_path contains spaces, write images to a temp directory, then relocate them to the user's requested path and rewrite markdown references
  • Added with_image test fixture (PDF with embedded image)

Fixes #1

Test plan

  • pytest -v — all 91 tests pass (3 new regression tests for this fix)
  • ruff check . and ruff format --check . clean
  • Manual test with a real PDF and an --image-dir path containing spaces

🤖 Generated with Claude Code

mrgeoffrich and others added 3 commits March 22, 2026 10:54
pymupdf4llm's utils.md_path() replaces spaces with underscores in the
file save path, but creates the directory with the original name. This
causes pix.save() to fail with ENOENT because the sanitized path
doesn't exist.

Work around this by writing images to a temp directory when the image
path contains spaces, then relocating them to the user's requested
directory and rewriting markdown references.

Fixes #1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Always use absolute paths for markdown image reference rewriting (pymupdf4llm
emits absolute paths, not relative). Also ensure temp directory is cleaned up
if _relocate_images raises an exception.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mrgeoffrich
mrgeoffrich merged commit d9f83b0 into main Mar 22, 2026
13 checks passed
@tinker-industries-housekeeping
tinker-industries-housekeeping Bot deleted the fix/image-dir-spaces branch August 27, 2026 23:40
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.

Spaces in image directory path are replaced with underscores, causing file write failure

1 participant