sphinx-autoimage provides two Sphinx directives, autoimage and autofigure, for builder-aware image and figure scaling.
The extension keeps compatibility with the legacy :scale-<builder>: options, such as :scale-html: and :scale-latex:, but the preferred interface is now configuration-driven and open for custom builders.
The extension is based on autoimage for Sphinx by kroger
Add the extension to your Sphinx conf.py:
extensions = [
"sphinx_autoimage",
]autoimage_sources defines where the extension searches for image files. Relative paths are resolved against the Sphinx source directory.
autoimage_sources = [
".",
"_images",
"../shared/images",
]If an image is found in one of these directories, the extension rewrites the image URI so that Sphinx can process it from the current document. If no matching file is found, the original URI is kept unchanged.
autoimage_scales maps Sphinx builder names or builder formats to percentage values.
autoimage_scales = {
"html": 100,
"singlehtml": 100,
"latex": 70,
"simplepdf": 70,
"qthelp": 100,
"epub2": 100,
"mobi": 100,
# Custom builders can be added without changing the extension code.
"my-custom-builder": 85,
# Optional fallback for builders without a more specific value.
"*": 100,
}The following legacy builder keys are part of the built-in base configuration and can be overridden in conf.py:
htmlsinglehtmllatexsimplepdfqthelpepub2mobi
Set a value to None to disable the default scale for a builder.
When no builder-specific entry matches, autoimage_scales["*"] is used as a generic fallback if it is configured. Using this fallback emits one warning per builder/format pair by default, because it usually means that a builder has not been configured explicitly.
Disable the warning when the fallback is intentional:
autoimage_warn_on_scale_fallback = FalseAlternatively, add an explicit entry for the builder or builder format:
autoimage_scales = {
"html": 100,
"latex": 70,
"my-custom-builder": 85,
"*": 100,
}The fallback warning is emitted as a Sphinx warning of type autoimage.scale-fallback.
By default, autoimage and autofigure center images when no explicit :align: option is provided.
autoimage_default_align = "center"Set the value to an empty string to disable automatic alignment:
autoimage_default_align = ""Preferred syntax with open builder mapping:
.. autoimage:: diagrams/architecture.png
:scale-for: html=100, latex=70, my-custom-builder=85
:alt: Architecture overviewConfiguration-only syntax:
.. autoimage:: diagrams/architecture.png
:alt: Architecture overviewLegacy syntax remains supported:
.. autoimage:: diagrams/architecture.png
:scale-html: 100
:scale-latex: 70
:alt: Architecture overview.. autofigure:: diagrams/architecture.png
:scale-for: html=100, latex=70
:alt: Architecture overview
Architecture overviewautofigure supports the standard Sphinx figure options in addition to the autoimage scale options.
For each image node created by autoimage or autofigure, the selected scale is resolved in this order:
:scale-for:for the currentbuilder.name- legacy
:scale-<builder>:for the currentbuilder.name :scale-for:for the currentbuilder.format- standard directive option
:scale: autoimage_scales[builder.name]autoimage_scales[builder.format]autoimage_scales["*"]generic fallback, with a warning unlessautoimage_warn_on_scale_fallback = False
Builder-specific scaling is applied during Sphinx's doctree-resolved event. This keeps the cached Sphinx environment builder-neutral and avoids requiring sphinx-build -E just because a different builder is used.
This project uses uv and the uv_build backend.
uv sync --all-groupsuv run ruff check src/sphinx_autoimage tests
uv run ruff format --check src/sphinx_autoimage tests
uv run pytestuv buildThe generated artifacts are written to dist/:
dist/sphinx_autoimage-<version>.tar.gz
dist/sphinx_autoimage-<version>-py3-none-any.whl
Install the project into the current environment:
uv pip install -e .Or install the generated wheel:
uv pip install dist/sphinx_autoimage-<version>-py3-none-any.whlThe repository includes two workflows:
.github/workflows/ci.yamlruns linting, formatting checks, tests, and package builds..github/workflows/release.yamlbuilds and publishes a GitHub release from a tag.