Skip to content

Commit a54a570

Browse files
committed
Added an easy screenshot method to window commands
CURRENTLY DOES NOT WORK NEED TO DEBUG WITH @einarf
1 parent 2948d4b commit a54a570

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

arcade/window_commands.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
import gc
99
import os
10-
11-
import pyglet
12-
1310
from typing import (
1411
Callable,
1512
Optional,
1613
Tuple,
14+
Union,
1715
TYPE_CHECKING
1816
)
17+
from pathlib import Path
18+
19+
from PIL import Image
20+
import pyglet
21+
1922
from arcade.types import RGBA255, Color
23+
from arcade.resources import resolve_resource_path
2024

2125
if TYPE_CHECKING:
2226
from arcade import Window
@@ -71,6 +75,25 @@ def get_window() -> "Window":
7175
return _window
7276

7377

78+
def save_screenshot(location: Union[str, Path], *, window: Optional["Window"] = None):
79+
"""
80+
Quickly save the screen or a gl texture to a png image. Is not a fast operation so should be used sparingly.
81+
Currently only supports 3 and 4 component 8-bit float gl textures. This should be fine for most use cases.
82+
83+
:param location: The string path to save the image to.
84+
:param window: Optionally supply a specific arcade window. Defaults to the currently active screen.
85+
"""
86+
win: "Window" = window or get_window()
87+
88+
img = Image.frombytes(
89+
"RGBA",
90+
win.ctx.screen.size,
91+
win.ctx.screen.read(components=4)
92+
)
93+
94+
img.save(location, "PNG")
95+
96+
7497
def set_window(window: Optional["Window"]) -> None:
7598
"""
7699
Set a handle to the current window.

0 commit comments

Comments
 (0)