Created
June 22, 2023 17:43
-
-
Save OskarZyg/69f9b5c8e5a8b0978bb86ec8d2cccb80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import json | |
from typing import List, Tuple | |
import websockets | |
from PIL import ImageGrab | |
def get_colors(size: int = 10) -> List[Tuple[int, int, int]]: | |
image = ImageGrab.grab() | |
width, height = image.size | |
slice_size = width // size | |
colors = [] | |
for unscaled_x in range(size): | |
x = unscaled_x * slice_size | |
coordinates = (x, height - 10) | |
colors.append(image.getpixel(coordinates)) | |
return colors | |
def serialise_numbers(numbers: List[Tuple[int, int, int]]) -> str: | |
return json.dumps(numbers) | |
# return '\t'.join([','.join([str(number) for number in color]) for color in numbers]) | |
async def connect_to_websocket(): | |
uri = "ws://192.168.0.73:81/" # Replace with the actual WebSocket server URL | |
async with websockets.connect(uri) as websocket: | |
# Perform actions after connecting to the WebSocket server | |
print("Connected to the WebSocket server.") | |
while True: | |
# Receive data from the WebSocket server | |
message = await websocket.recv() | |
print("Received message:", message) | |
await websocket.send(serialise_numbers(get_colors(30))) | |
await asyncio.sleep(1 / 10) | |
asyncio.new_event_loop().run_until_complete(connect_to_websocket()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment