Skip to content

Commit

Permalink
Merge pull request #341 from brainglobe/screenshot-types
Browse files Browse the repository at this point in the history
Added support for multiple screenshot file formats
  • Loading branch information
alessandrofelder committed Apr 25, 2024
2 parents f3103ba + 0fdfcce commit 60635e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
8 changes: 5 additions & 3 deletions brainrender/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,11 @@ def screenshot(self, name=None, scale=None):
self.render(interactive=False)

timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
name = name or f"brainrender_screenshot_{timestamp}"
if ".png" not in name:
name += ".png"
name = Path(name or f"brainrender_screenshot_{timestamp}")

# If no suffix is provided or it an unsupported format, default to .png
if name.suffix not in [".png", ".eps", ".pdf", ".svg", ".jpg"]:
name = name.with_suffix(".png")

scale = scale or settings.SCREENSHOT_SCALE

Expand Down
33 changes: 28 additions & 5 deletions tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shutil
from pathlib import Path

import pytest

Expand Down Expand Up @@ -101,12 +102,34 @@ def test_scene_slice():


@pytest.mark.parametrize(
"name, scale", [("test", 2), (None, None), (None, 1), ("test2", None)]
"name, scale, expected_suffix",
[
("test", 2, ".png"),
(None, None, ".png"),
(None, 1, ".png"),
("test2", None, ".png"),
("test.jpg", 1, ".jpg"),
("test.eps", 1, ".eps"),
("test.svg", 1, ".svg"),
("test.pdf", 1, ".pdf"),
("test.tiff", 1, ".png"),
],
)
def test_scene_screenshot(name, scale):
s = Scene(screenshots_folder="tests/screenshots")
s.screenshot(name=name, scale=scale)
shutil.rmtree("tests/screenshots")
def test_scene_screenshot(name, scale, expected_suffix):
screenshot_folder = Path.home() / "test_screenshots"
s = Scene(screenshots_folder=screenshot_folder)
out_path = s.screenshot(name=name, scale=scale)

assert Path(out_path).suffix == expected_suffix

# Vedo exports eps and svg files as gzipped files
# Append the .gz suffix to the expected path to check if file exists
if expected_suffix in [".eps", ".svg"]:
out_path += ".gz"

assert Path(out_path).exists()

shutil.rmtree(screenshot_folder)
del s


Expand Down

0 comments on commit 60635e2

Please sign in to comment.