Skip to content

Commit 9880b38

Browse files
authored
Merge pull request #56 from Pbatch/pb_fast_screenshot
Fast screenshots
2 parents 5db62e3 + fe876d3 commit 9880b38

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

clashroyalebuildabot/screen.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import subprocess
32

43
import numpy as np
@@ -8,18 +7,11 @@
87

98

109
class Screen:
11-
@staticmethod
12-
def take_screenshot():
13-
"""
14-
Take a screenshot of the emulator
15-
"""
16-
screenshot_bytes = subprocess.check_output(['adb', 'exec-out', 'screencap', '-p'])
17-
if screenshot_bytes and len(screenshot_bytes) > 5 and screenshot_bytes[5] == 0x0d:
18-
screenshot_bytes = screenshot_bytes.replace(b'\r\n', b'\n')
19-
screenshot = io.BytesIO(screenshot_bytes)
20-
screenshot = Image.open(screenshot).convert('RGB')
21-
screenshot = screenshot.resize((SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT), Image.BILINEAR)
22-
return screenshot
10+
def __init__(self):
11+
# Physical size: 720x1280 -> self.width = 720, self.height = 1280
12+
window_size = subprocess.check_output(['adb', 'shell', 'wm', 'size'])
13+
window_size = window_size.decode('ascii').replace('Physical size: ', '')
14+
self.width, self.height = [int(i) for i in window_size.split('x')]
2315

2416
@staticmethod
2517
def click(x, y):
@@ -28,6 +20,15 @@ def click(x, y):
2820
"""
2921
subprocess.run(['adb', 'shell', 'input', 'tap', str(x), str(y)])
3022

23+
def take_screenshot(self):
24+
"""
25+
Take a screenshot of the emulator
26+
"""
27+
screenshot_bytes = subprocess.run(['adb', 'exec-out', 'screencap'], check=True, capture_output=True).stdout
28+
screenshot = Image.frombuffer('RGBA', (self.width, self.height), screenshot_bytes[12:], 'raw', 'RGBX', 0, 1)
29+
screenshot = screenshot.convert('RGB').resize((SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT), Image.BILINEAR)
30+
return screenshot
31+
3132

3233
def main():
3334
cls = Screen()

0 commit comments

Comments
 (0)