Skip to content

Commit 60635e2

Browse files
Merge pull request #341 from brainglobe/screenshot-types
Added support for multiple screenshot file formats
2 parents f3103ba + 0fdfcce commit 60635e2

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

brainrender/render.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,11 @@ def screenshot(self, name=None, scale=None):
347347
self.render(interactive=False)
348348

349349
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
350-
name = name or f"brainrender_screenshot_{timestamp}"
351-
if ".png" not in name:
352-
name += ".png"
350+
name = Path(name or f"brainrender_screenshot_{timestamp}")
351+
352+
# If no suffix is provided or it an unsupported format, default to .png
353+
if name.suffix not in [".png", ".eps", ".pdf", ".svg", ".jpg"]:
354+
name = name.with_suffix(".png")
353355

354356
scale = scale or settings.SCREENSHOT_SCALE
355357

tests/test_scene.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import shutil
2+
from pathlib import Path
23

34
import pytest
45

@@ -101,12 +102,34 @@ def test_scene_slice():
101102

102103

103104
@pytest.mark.parametrize(
104-
"name, scale", [("test", 2), (None, None), (None, 1), ("test2", None)]
105+
"name, scale, expected_suffix",
106+
[
107+
("test", 2, ".png"),
108+
(None, None, ".png"),
109+
(None, 1, ".png"),
110+
("test2", None, ".png"),
111+
("test.jpg", 1, ".jpg"),
112+
("test.eps", 1, ".eps"),
113+
("test.svg", 1, ".svg"),
114+
("test.pdf", 1, ".pdf"),
115+
("test.tiff", 1, ".png"),
116+
],
105117
)
106-
def test_scene_screenshot(name, scale):
107-
s = Scene(screenshots_folder="tests/screenshots")
108-
s.screenshot(name=name, scale=scale)
109-
shutil.rmtree("tests/screenshots")
118+
def test_scene_screenshot(name, scale, expected_suffix):
119+
screenshot_folder = Path.home() / "test_screenshots"
120+
s = Scene(screenshots_folder=screenshot_folder)
121+
out_path = s.screenshot(name=name, scale=scale)
122+
123+
assert Path(out_path).suffix == expected_suffix
124+
125+
# Vedo exports eps and svg files as gzipped files
126+
# Append the .gz suffix to the expected path to check if file exists
127+
if expected_suffix in [".eps", ".svg"]:
128+
out_path += ".gz"
129+
130+
assert Path(out_path).exists()
131+
132+
shutil.rmtree(screenshot_folder)
110133
del s
111134

112135

0 commit comments

Comments
 (0)