Просмотр исходного кода

moove save_screenshot to helper

Richard Köhl 1 год назад
Родитель
Сommit
a399fb6854
2 измененных файлов с 48 добавлено и 22 удалено
  1. 36 0
      helper.py
  2. 12 22
      screenshot.py

+ 36 - 0
helper.py

@@ -1,7 +1,10 @@
 import cv2
+import datetime
+import io
 import numpy as np
 import os
 import threading
+import subprocess
 from io import BytesIO
 from PIL import Image
 from ppadb.client import Client as AdbClient
@@ -213,6 +216,39 @@ def non_max_suppression(boxes, overlapThresh):
     return boxes[pick].astype("int")
 
 
+def save_screenshot(path="test"):
+    # Take a screenshot
+    result = capture_current_screen()
+
+    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
+    image = Image.open(io.BytesIO(result))
+    jpeg_filename = f"{path}/{timestamp}.jpg"
+
+    image = image.convert("RGB")  # Convert to RGB mode for JPEG
+    with open(jpeg_filename, "wb") as fp:
+        image.save(fp, format="JPEG", quality=85)  # Adjust quality as needed
+    print(f"snap: {jpeg_filename}")
+
+
+def save_screenshot2(path="test"):
+    proc = subprocess.Popen(
+        "adb exec-out screencap -p", shell=True, stdout=subprocess.PIPE
+    )
+    image_bytes = proc.stdout.read()
+    image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
+    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
+
+    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
+    jpeg_filename = f"{path}/{timestamp}.jpg"
+
+    cv2.imwrite(
+        jpeg_filename,
+        cv2.cvtColor(image, cv2.COLOR_RGB2BGR),
+        [int(cv2.IMWRITE_JPEG_QUALITY), 85],
+    )
+    print(f"snap: {jpeg_filename}")
+
+
 client = AdbClient(host="127.0.0.1", port=5037)
 device = client.device(android_address)
 

+ 12 - 22
screenshot.py

@@ -1,17 +1,17 @@
-import time
-import datetime
 import numpy as np
-import io
-from ppadb.client import Client as AdbClient
-from PIL import Image
+import os
+import time
 from helper import (
     cv2,
     find_template,
     tap as tap_helper,
     swipe as swipe_helper,
     capture_current_screen,
+    save_screenshot,
 )
 
+screenshot_address = os.getenv("ANDROID_ADDRESS")
+
 # templates
 close_sub_fights = cv2.imread("templates/close_sub_fights.jpg")
 close_fight = cv2.imread("templates/close_fight.jpg")
@@ -79,21 +79,6 @@ def non_max_suppression(boxes, overlapThresh):
     return boxes[pick].astype("int")
 
 
-def save_screenshot():
-    # Take a screenshot
-    result = capture_current_screen()
-
-    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
-    image = Image.open(io.BytesIO(result))
-    jpeg_filename = f"/mnt/t/nextcloud/InstantUpload/Herowars/{timestamp}.jpg"
-
-    image = image.convert("RGB")  # Convert to RGB mode for JPEG
-    with open(jpeg_filename, "wb") as fp:
-        image.save(fp, format="JPEG", quality=85)  # Adjust quality as needed
-    print(f"snap: {jpeg_filename}")
-    time.sleep(0.5)
-
-
 def tap(location):
     tap_helper(location)
     time.sleep(1)
@@ -131,12 +116,17 @@ def find_max_y_pair(coordinates):
     return f"{result[0]} {result[1]}"
 
 
+def snap():
+    save_screenshot(screenshot_address)
+    time.sleep(0.5)
+
+
 def take_fight_screenshots():
-    save_screenshot()
+    snap()
 
     # # if you desperately need submits
     # tap(damage_taken)
-    # save_screenshot()
+    # snap()
 
     tap_button(close_fight)
     time.sleep(1)