[Bug]: optimize>=2 crashes with pikepdf 10.2-10.8: AttributeError: get_images (Page.get_images() added only in pikepdf 10.9, floor still >=10.2)
Description
ocrmypdf/optimize.py::_transcode_png() (optimize ≥ 2) calls pikepdf.Page.get_images(), but that method was only added in pikepdf 10.9.0. The declared dependency floor is pikepdf>=10.2 (comment: "NamePath, Object.as_int()/as_bool(), thread-local explicit_conversion()"), so any environment that resolves pikepdf 10.2–10.8 (lockfiles, distro packages) crashes during the optimize phase with AttributeError: get_images — while pip check passes, because no constraint is violated.
The call was introduced by commit 62b42095 ("Replace deprecated pikepdf Page.images with Page.get_images()"), first released in v17.11.0 (v17.10.0 and earlier used the deprecated Page.images). Since it looked like a mere deprecation replacement, the dependency floor was never raised.
Reference: pikepdf release notes, v10.9.0 — "Added pikepdf.Page.get_images(), which by default recurses into nested form XObjects to find images." (https://pikepdf.readthedocs.io/en/latest/releasenotes/version10.html)
Environment
Reproduced standalone in a throwaway ghcr.io/paperless-ngx/paperless-ngx:latest container (Python 3.14, Debian trixie):
- ocrmypdf 17.12.1, pikepdf 10.2.0 (the paperless-ngx uv.lock pin), pngquant 2.18.0
- Upstream context: paperless-ngx v3.2.0 already locked
ocrmypdf 17.11.0 + pikepdf 10.2.0— i.e. the first release that shipped the broken pair
Steps to reproduce
Make a minimal PDF whose single image is PNG-class (FlateDecode):
from PIL import Image
Image.new("RGB", (300, 200), (120, 140, 160)).save("in.png")
import img2pdf
with open("in.pdf", "wb") as f:
img2pdf.convert("in.png", outputstream=f)$ pip install "pikepdf==10.2.0" # satisfies declared floor pikepdf>=10.2
$ ocrmypdf --optimize 2 in.pdf out.pdf→ fails with the traceback below. Then:
$ pip install "pikepdf==10.13.0.post1"
$ ocrmypdf --optimize 2 in.pdf out_ok.pdf # → succeedsTraceback (key frames)
File "ocrmypdf/optimize.py", line 739, in _transcode_png
foreign_image = next(iter(pdf_image.pages[0].get_images().values()))
File "pikepdf/_methods.py", line 755, in __getattr__
return getattr(self.obj, name)
AttributeError: get_imagesSuggested fix
Raise the floor to pikepdf>=10.9 (the release that added Page.get_images()), or version-guard the call. A release-note line for 17.11.x/17.12.x documenting the effective minimum pikepdf would also help packagers who froze 10.2–10.8.
Why this is underreported
The crash needs optimize ≥ 2 (at optimize ≤ 1 the modified set stays empty and _transcode_png is never called) and a PNG-class image, and pip-installed ocrmypdf usually pulls the latest pikepdf (≥ 10.9) anyway — so lockfile-frozen environments are the ones that break.
Real-world impact
Found via paperless-ngx v3.2.0/v3.2.1: their uv.lock pins pikepdf==10.2.0, so consuming PDFs fails whenever the user's OCR user_args contain optimize >= 2. Cross-referencing an issue there.
Source: ocrmypdf/OCRmyPDF