From e21a3c260e4d053ba69a3c09f3c327efc36cb80c Mon Sep 17 00:00:00 2001 From: lucas-santos-f <162367088+lucas-santos-f@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:42:48 -0300 Subject: [PATCH 1/2] Allow show to write the screenshot to a file-like object 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 #1760 --- cadquery/vis.py | 13 ++++++++++--- doc/vis.rst | 10 ++++++++++ tests/test_vis.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/cadquery/vis.py b/cadquery/vis.py index d79278990..8bbf656d3 100644 --- a/cadquery/vis.py +++ b/cadquery/vis.py @@ -14,6 +14,7 @@ from .occ_impl.nurbs import Curve, Surface from typing import Union, List, Tuple, Iterable, cast, Optional +from io import BytesIO from OCP.TopoDS import TopoDS_Shape from OCP.Geom import Geom_BSplineSurface @@ -402,7 +403,7 @@ def show( edges: bool = False, specular: bool = True, title: str = "CQ viewer", - screenshot: Optional[str] = None, + screenshot: Optional[Union[str, BytesIO]] = None, interact: bool = True, zoom: float = 1.0, roll: float = -35, @@ -572,9 +573,15 @@ def show( win2image.Update() writer = vtkPNGWriter() - writer.SetFileName(screenshot) writer.SetInputConnection(win2image.GetOutputPort()) - writer.Write() + + if isinstance(screenshot, BytesIO): + writer.WriteToMemoryOn() + writer.Write() + screenshot.write(memoryview(writer.GetResult())) + else: + writer.SetFileName(screenshot) + writer.Write() # start interaction if interact: diff --git a/doc/vis.rst b/doc/vis.rst index 44ffa7047..772ef937d 100644 --- a/doc/vis.rst +++ b/doc/vis.rst @@ -92,6 +92,16 @@ camera position and windows size. show(b, width=800, height=800, screenshot='img.png', zoom=2, roll=-20, elevation=-30, interact=False) +``screenshot`` also accepts a ``BytesIO`` object, for example to keep the image in memory. + +.. code-block:: python + + from io import BytesIO + + buf = BytesIO() + show(b, screenshot=buf, interact=False) + png = buf.getvalue() + .. warning:: Intermittent issues were observed with this functionality, please submit detailed bug reports in case diff --git a/tests/test_vis.py b/tests/test_vis.py index b59367d73..b9908c4dd 100644 --- a/tests/test_vis.py +++ b/tests/test_vis.py @@ -17,9 +17,11 @@ ) from vtkmodules.vtkRenderingAnnotation import vtkAnnotatedCubeActor from vtkmodules.vtkIOImage import vtkPNGWriter +from vtkmodules.vtkImagingSources import vtkImageCanvasSource2D from pytest import fixture, raises, mark import os +from io import BytesIO from pathlib import Path from typing import List @@ -158,6 +160,39 @@ def test_screenshot(wp, patch_vtk): show(wp, interact=False, screenshot="img.png", trihedron=False, gradient=False) +class FakeImage(FakeWin2Img): + """ + Small solid image in place of the render window contents. + """ + + def __init__(self): + + self.source = vtkImageCanvasSource2D() + self.source.SetExtent(0, 9, 0, 4, 0, 0) + self.source.SetScalarTypeToUnsignedChar() + self.source.SetNumberOfScalarComponents(3) + self.source.FillBox(0, 9, 0, 4) + + def GetOutputPort(self, *args): + + return self.source.GetOutputPort() + + +def test_screenshot_to_memory(wp, patch_vtk, monkeypatch): + + monkeypatch.setattr(vis, "vtkWindowToImageFilter", FakeImage) + monkeypatch.setattr(vis, "vtkPNGWriter", vtkPNGWriter) + + buf = BytesIO() + show(wp, interact=False, screenshot=buf, trihedron=False, gradient=False) + png = buf.getvalue() + + assert png.startswith(b"\x89PNG\r\n\x1a\n") + # width and height from the IHDR chunk + assert int.from_bytes(png[16:20], "big") == 10 + assert int.from_bytes(png[20:24], "big") == 5 + + @mark.skip(reason="running only smoke test for now") def test_screenshot_no_patch(wp, tmpdir, cwd): From 2fdd1781ae5bf3f5793dc0305488e33196daa6e9 Mon Sep 17 00:00:00 2001 From: AU Date: Sun, 22 Nov 2026 12:24:59 +0100 Subject: [PATCH 2/2] Reorganize fixtures --- tests/test_vis.py | 51 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/tests/test_vis.py b/tests/test_vis.py index b9908c4dd..3384d1a1e 100644 --- a/tests/test_vis.py +++ b/tests/test_vis.py @@ -94,6 +94,24 @@ def Write(*args): pass +class FakeImage(FakeWin2Img): + """ + Small solid image in place of the render window contents. + """ + + def __init__(self): + + self.source = vtkImageCanvasSource2D() + self.source.SetExtent(0, 9, 0, 4, 0, 0) + self.source.SetScalarTypeToUnsignedChar() + self.source.SetNumberOfScalarComponents(3) + self.source.FillBox(0, 9, 0, 4) + + def GetOutputPort(self, *args): + + return self.source.GetOutputPort() + + @fixture def patch_vtk(monkeypatch): """ @@ -107,6 +125,16 @@ def patch_vtk(monkeypatch): monkeypatch.setattr(vis, "vtkPNGWriter", FakePNGWriter) +@fixture +def patch_vtk_inmemory(patch_vtk, monkeypatch): + """ + Fixture needed to test in-memory export. + """ + + monkeypatch.setattr(vis, "vtkWindowToImageFilter", FakeImage) + monkeypatch.setattr(vis, "vtkPNGWriter", vtkPNGWriter) + + def test_show(wp, assy, sk, patch_vtk): # simple smoke test @@ -160,28 +188,7 @@ def test_screenshot(wp, patch_vtk): show(wp, interact=False, screenshot="img.png", trihedron=False, gradient=False) -class FakeImage(FakeWin2Img): - """ - Small solid image in place of the render window contents. - """ - - def __init__(self): - - self.source = vtkImageCanvasSource2D() - self.source.SetExtent(0, 9, 0, 4, 0, 0) - self.source.SetScalarTypeToUnsignedChar() - self.source.SetNumberOfScalarComponents(3) - self.source.FillBox(0, 9, 0, 4) - - def GetOutputPort(self, *args): - - return self.source.GetOutputPort() - - -def test_screenshot_to_memory(wp, patch_vtk, monkeypatch): - - monkeypatch.setattr(vis, "vtkWindowToImageFilter", FakeImage) - monkeypatch.setattr(vis, "vtkPNGWriter", vtkPNGWriter) +def test_screenshot_to_memory(wp, patch_vtk_inmemory): buf = BytesIO() show(wp, interact=False, screenshot=buf, trihedron=False, gradient=False)