repomatic.images moduleΒΆ

Image optimization using external CLI tools.

Tools used per format:

  • PNG: oxipng (lossless, multithreaded Rust optimizer).

  • JPEG/JPG: jpegoptim (lossless Huffman optimization + metadata stripping).

Note

Both tools are strictly lossless: oxipng finds optimal PNG encoding parameters without altering pixel data, and jpegoptim (without -m) rewrites Huffman tables only. This means optimization is idempotent: a second run produces no further changes, so the workflow never creates noisy PRs for negligible savings.

Warning

WebP and AVIF are intentionally not optimized. The only available tools (cwebp, avifenc) work by lossy re-encoding: decode, then re-compress at a target quality. This is not idempotent: each pass re-compresses the previous output, producing progressively smaller (and worse) files. A lossy optimizer needs several workflow runs to settle below the savings threshold, and each run opens a pull request with less to gain and more quality lost. Lossless WebP/AVIF modes exist but typically increase file size when applied to already lossy-encoded images, making them counterproductive. Since WebP and AVIF are modern formats chosen specifically for their compression efficiency, files in these formats are almost always already well-optimized at creation time.

repomatic.images.DEFAULT_MIN_SAVINGS_BYTES = 1024ΒΆ

Minimum absolute byte savings required to keep the optimized file.

repomatic.images.DEFAULT_MIN_SAVINGS_PCT = 5ΒΆ

Minimum percentage savings required to keep the optimized file.

repomatic.images.JPEGOPTIM_FLAGS = ('--strip-all', '--all-progressive')ΒΆ

Flags of every jpegoptim run: drop the metadata, write progressive JPEGs.

repomatic.images.OXIPNG_OPT_LEVEL = '4'ΒΆ

Optimization level oxipng runs at.

class repomatic.images.OptimizationResult(path, before_bytes, after_bytes)[source]ΒΆ

Bases: object

Result of optimizing a single image file.

path: PathΒΆ
before_bytes: intΒΆ
after_bytes: intΒΆ
property saved_bytes: intΒΆ

Bytes saved by optimization.

property saved_pct: floatΒΆ

Percentage saved, as a float 0–100.

repomatic.images.optimize_image(path, min_savings_pct, min_savings_bytes=1024)[source]ΒΆ

Optimize a single image file in-place.

Parameters:
  • path (Path) – Path to the image file.

  • min_savings_pct (float) – Minimum percentage savings to keep the result. If savings are below this threshold, the original file is restored.

  • min_savings_bytes (int) – Minimum absolute byte savings to keep the result. Prevents noisy diffs for tiny files where even a high percentage represents negligible absolute savings.

Return type:

OptimizationResult | None

Returns:

An OptimizationResult if the file was optimized, or None if the format is unsupported, the required tool is missing, or savings were below the threshold.

repomatic.images.optimize_images(image_files, min_savings_pct=5, min_savings_bytes=1024)[source]ΒΆ

Optimize a list of image files.

Parameters:
  • image_files (Sequence[Path]) – Paths to image files.

  • min_savings_pct (float) – Minimum percentage savings to keep an optimization.

  • min_savings_bytes (int) – Minimum absolute byte savings to keep an optimization.

Return type:

list[OptimizationResult]

Returns:

List of results for files that were successfully optimized.

repomatic.images.generate_markdown_summary(results)[source]ΒΆ

Generate a markdown summary table of optimization results.

Shows the size before and after, and the percentage saved, for each optimized file.

Return type:

str