Skip to content

Allow show to write the screenshot to a file-like object - #2136

Open
lucas-santos-f wants to merge 2 commits into
CadQuery:masterfrom
lucas-santos-f:png-to-memory
Open

lucas-santos-f wants to merge 2 commits into
CadQuery:masterfrom
lucas-santos-f:png-to-memory

Conversation

@lucas-santos-f

@lucas-santos-f lucas-santos-f commented Nov 11, 2026 •

Copy link
Copy Markdown

Fixes #1760

show(..., screenshot=...) now also takes a binary file-like object such as io.BytesIO. In that case vtkPNGWriter writes to memory (WriteToMemoryOn) and the PNG bytes are written to the object, so docs or notebooks can get the image without going through a temporary file:

from io import BytesIO

buf = BytesIO()
show(b, screenshot=buf, interact=False)
png = buf.getvalue()

A file name works as before: only a BytesIO goes to memory (isinstance(screenshot, BytesIO)), and anything else, a str or a pathlib.Path, still goes to SetFileName. The example above is added to the Screenshots section of doc/vis.rst.

The new test_screenshot_to_memory uses the patch_vtk fixture, but feeds the real vtkPNGWriter a 10x5 image from vtkImageCanvasSource2D in place of the window, and checks the PNG signature and the size in the IHDR chunk. On master it fails with TypeError: SetFileName argument 1.

Checked locally (Windows 10, Python 3.12, pip install with cadquery-ocp 8.0.1.1, VTK 9.6.2):

  • off-screen render of a box at 320x240: the bytes in the BytesIO are identical to the file written with screenshot="img.png"
  • pytest -n 8: 904 passed, 3 skipped (master: 903 passed, 3 skipped), no new warnings
  • black . --diff --check (cq fork): nothing to reformat
  • mypy cadquery: same errors as master; here they are almost all import-untyped for OCP, since the pip package has no stubs
  • docs build with Sphinx 9.1.0: no new warnings, and the new example shows up under Screenshots

Happy to adjust anything if needed, e.g. if you'd rather have show return the bytes.

AI disclosure: this change was written with the help of Claude Code (Claude Opus 5.5).

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.86%. Comparing base (b3f9afd) to head (2fdd178).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2136      +/-   ##
==========================================
+ Coverage   95.81%   95.86%   +0.05%     
==========================================
  Files          30       30              
  Lines        9612     9657      +45     
  Branches     1446     1461      +15     
==========================================
+ Hits         9210     9258      +48     
+ Misses        247      245       -2     
+ Partials      155      154       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread cadquery/vis.py Resolved
screenshot now also takes a binary file-like object such as io.BytesIO.
The PNG is then written to memory with vtkPNGWriter.WriteToMemoryOn and
copied into it, which avoids a temporary file when the image is only
needed in memory (docs, notebooks).

Fixes CadQuery#1760
@lucas-santos-f

Copy link
Copy Markdown
Author

Done: it checks for BytesIO with isinstance now, and the type hint and docs say BytesIO.

@jmwright jmwright left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lucas-santos-f just one comment.

From the PR description:

"The check is hasattr(screenshot, "write") rather than isinstance(screenshot, str) because VTK also takes a pathlib.Path in SetFileName, and that should keep working."

However, the code seems to be the inverse of that. Is that correct, and if so, is it intentional?

@lucas-santos-f

Copy link
Copy Markdown
Author

Yes, that's intentional, the description was out of date, sorry. The first version checked hasattr(screenshot, "write"); Adam asked for isinstance(screenshot, BytesIO) in review, so now only a BytesIO takes the in-memory path and anything else, a str or a pathlib.Path, still goes to SetFileName (just rechecked a Path with VTK 9.6.2, it writes the file). I've updated the description to match.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write PNG to io.Bytes

3 participants