Skip to content

Commit 77b2bf2

Browse files
committed
resize to largest width supported
1 parent 9b6e924 commit 77b2bf2

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

src/goose/toolkit/screen.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
import re
31
import subprocess
42
import uuid
53

@@ -13,39 +11,27 @@ class Screen(Toolkit):
1311
"""Provides an instructions on when and how to work with screenshots"""
1412

1513
@tool
16-
def take_screenshot(self, display: int = 1, resize: bool = False, target_size: int = 4) -> str:
14+
def take_screenshot(self, display: int = 1) -> str:
1715
"""
18-
Take a screenshot to assist the user in debugging or designing an app. They may need help with moving a screen element, or interacting in some way where you could do with seeing the screen.
19-
Optionally, resize the image using the `sips` cli tool. You may do this fs there is potential for error in the payload size.
16+
Take a screenshot to assist the user in debugging or designing an app. They may need
17+
help with moving a screen element, or interacting in some way where you could do with
18+
seeing the screen.
2019
2120
Args:
2221
display (int): Display to take the screen shot in. Default is the main display (1). Must be a value greater than 1.
23-
resize (bool): Boolean parameter to resize the image or not. No resizing by default.
24-
target_size (int): Target file size in MB. Default is 4.
25-
Return:
26-
(str) a path to the screenshot file, in the format of image: followed by the path to the file.
2722
""" # noqa: E501
2823
# Generate a random tmp filename for screenshot
29-
filename = f"/tmp/goose_screenshot_{uuid.uuid4().hex}.png"
30-
screen_capture_command = ["screencapture", "-x", "-D", str(display), filename]
31-
resize_command_str = ""
24+
filename = f"/tmp/goose_screenshot_{uuid.uuid4().hex}.jpg"
25+
screen_capture_command = ["screencapture", "-x", "-D", str(display), filename, "-f", "jpg"]
3226

33-
subprocess.run(screen_capture_command)
27+
subprocess.run(screen_capture_command, check=True, capture_output=True)
3428

35-
if resize:
36-
# get current disk size and reduce pixels by fractional amount to target (not linear but approx)
37-
size = os.path.getsize(filename) / (1024**2)
38-
reduce_by = max(0, (size - target_size) / size)
39-
output = subprocess.run(["sips", "-g", "pixelWidth", filename], stdout=subprocess.PIPE).stdout.decode()
40-
current_pixel_width = int(re.search(r"pixelWidth:\s*(\d+)", output).group(1))
41-
42-
resize_command = ["sips", "--resampleWidth", str(int(current_pixel_width * reduce_by)), filename]
43-
subprocess.run(resize_command)
44-
resize_command_str = " ".join(resize_command)
29+
resize_command = ["sips", "--resampleWidth", "768", filename, "-s", "format", "jpeg"]
30+
subprocess.run(resize_command, check=True, capture_output=True)
4531

4632
self.notifier.log(
4733
Panel.fit(
48-
Markdown(f"```bash\n{' '.join(screen_capture_command)}\n{resize_command_str if resize else ''}"),
34+
Markdown(f"```bash\n{' '.join(screen_capture_command)}"),
4935
title="screen",
5036
)
5137
)

0 commit comments

Comments
 (0)