Skip to content

Commit b4e92d9

Browse files
committed
use export_to_png() for screnshots
1 parent e6570d7 commit b4e92d9

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

pythonhere/magic_here/shortcuts.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# pylint: disable=invalid-name
33

44
from base64 import b64decode
5-
from io import StringIO
5+
from io import BytesIO, StringIO
66

7-
from IPython.display import Image, display
7+
from PIL import Image as PILImage
8+
from IPython.display import display
89
import click
910
from herethere.there.commands import there_group, there_code_shortcut
1011

@@ -65,4 +66,9 @@ def screenshot(ctx, width, output):
6566
output.write(data)
6667
output.close()
6768

68-
display(Image(data=data, width=width))
69+
img = PILImage.open(BytesIO(data)).convert("RGB")
70+
if width:
71+
height = int(width * img.size[1] // img.size[0])
72+
img = img.resize((width, height), PILImage.ANTIALIAS)
73+
74+
display(img)

pythonhere/window_here.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Utilities for working with Kivy window."""
22
from base64 import b64encode
33
import os
4+
from pathlib import Path
45
import time
56

67
from kivy.app import App
@@ -49,7 +50,8 @@ def load_kv_string(code: str, clear_style: bool):
4950

5051
def encoded_screenshot() -> str:
5152
"""Return base64 encoded displayed image."""
52-
path = Window.screenshot("screenshot_here.png")
53+
path = str(Path(f"screenshot_{time.time()}.png").resolve())
54+
Window.children[0].export_to_png(path)
5355
with open(path, "rb") as png_file:
5456
data = b64encode(png_file.read()).decode()
5557
os.remove(path)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"ipython",
3030
"ipywidgets",
3131
"nest_asyncio",
32+
"Pillow",
3233
],
3334
extras_require={
3435
"dev": [

0 commit comments

Comments
 (0)