Skip to content

Commit

Permalink
Merge pull request #56 from Pbatch/pb_fast_screenshot
Browse files Browse the repository at this point in the history
Fast screenshots
  • Loading branch information
Pbatch authored Dec 8, 2022
2 parents 5db62e3 + fe876d3 commit 9880b38
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions clashroyalebuildabot/screen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import subprocess

import numpy as np
Expand All @@ -8,18 +7,11 @@


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

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

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


def main():
cls = Screen()
Expand Down

0 comments on commit 9880b38

Please sign in to comment.